TryIt()
函数会导致 SIGSEGV 错误,但仅限于 GCC 而不是 Visual C:
string strs[] =
{
"str1",
"str2",
"str3",
""
};
void Tryit()
{
int cnt = 0;
while ( strs[cnt] != "" )
cnt++;
}
最佳答案
发布的代码是正确的,假设 TryIt()
在构造 strs
之后被调用。由于您提到它是从全局对象的构造函数调用的,因此可能在构造 strs
之前调用它。解决方法是将 strs
包装在一个函数中并调用此函数来获取数组的基址:
std::string* strs() {
static std::string rc[] p {
...
};
return rc;
}
关于c++ - 为什么此代码会导致 SIGSEGV(gcc 编译器、cygwin 下的 Android NDK 工具链)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004377/