变量值自身变化

chr*_*ris 5 c++ const-cast

之前在编程时我一直很困惑,但这一点很重要。基本上我在一个 for 循环中设置值,并在接下来的迭代中更改为下一个的值。

for (int i = 0; i < 2; ++i)
{
    for (int j = 0; j < numWords[i]; ++j) //numWords [0] = 9, numWords [1] = 7
    {
        stb[i][j].word = const_cast<char*>(is (j + 1,1).c_str()); //is(int,length[opt]) converts int to string, c_str() returns const char *, but I need char *
        cout << is(j+1,1) << ' ' << stb[i][j].word << '\n';
    }
}

for (int i = 0; i < 2; ++i)
{
    for (int j = 0; j < numWords [i]; ++j)
    {
        cout << stb[i][j].word << ' ';
    }
    cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
1 1
2 2 
3 3
4 4
5 5 
6 6
7 7 
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7

我现在唯一的猜测是 const 的东西,但是为什么它会不断更改所有以前的数组元素是没有意义的......

Man*_*rse 3

这很简单。您的程序有未定义的行为(如果我的假设is()是正确的)。

is(int, length)std::string按值返回 a 。string您可以使用获得指向其中某些内部结构的指针c_str()然后,该字符串在完整表达式的末尾被破坏。此破坏会使您从 获得的指针无效c_str()

这意味着您用指向无效内存的指针填充数组。然后,您可以读取这些指针以打印出数组的内容。从无效内存中读取会导致未定义的行为。

对观察到的行为的可能解释是:

每个返回stringis重用相同的内存。在第一个循环中,您在内存被另一个调用覆盖之前从内存中读取is,因此您获得了正确的值。在第二个循环中,您在内存被覆盖后从内存中读取数据,从而获得数组中的最终值。