【发布时间】:2014-10-26 13:05:34
【问题描述】:
在我的代码中,我一直在尝试在字符串数组中查找特定字符串的长度。但是,当我这样做时,无论我尝试用什么方法查找特定字符串的长度,总是返回 -858993460
我一直在使用的代码是
void drawChoices(int x, std::string s [], int iChoice)
{
setCurPos(x, 1); //this function just sets the console cursor
for (int i = 0; i <= iChoice; i++)
{
color(15, 4); //this function just sets the console color
std::cout << "[" << s[i] << "]";
color(8, 8);
int asfa = sizeof s[i]; //this is the one causing trouble
std::cout << " \n";
}
}
我尝试使用sizeof、string::length 和strlen 在数组中查找字符串的长度,但结果相同。
【问题讨论】:
-
你是如何确定 asfa 有这个负值的?
-
-1 提供的代码没有说明所声称的行为(负大小)。
-
当您使用 Visual C++ 时,-858993460 是一个神奇的数字。转换成十六进制,就是0xcccccccc。这是在 Debug 构建中初始化所有变量的值。每当你在调试时看到它,你就会去“啊,我正在使用一个未初始化的变量!”让调试器告诉你哪一个是问题,你的 for(;;) 循环中的一个错误是值得注意的。
-
-858993460 = 0xCCCCCCCC 这意味着you've accessed uninitialized memory
标签: c++ string visual-studio-2013 sizeof stdstring