【发布时间】:2015-01-01 06:05:59
【问题描述】:
我真的不明白为什么我的代码会出现这个问题
首先我创建了两个指向 char 的指针
char* finWord;
char* ignoredWord;
然后我将它们作为参数传递给其他函数
lowerCase(line.substr(0, endWord), ignoredWord);
toNormalWord(ignoredWord, finWord);
但是当我运行程序时,它会抛出一个分段错误,问题是 finWord 地址总是 0x1
这就是问题发生的地方
void toNormalWord (string src, char* des)
{
char c;
des[sizeof(src) + 1];
int position = 0;
if (isThere)
{
des[position] = c; //Here the gdb show me the following error 0x1 <error:
// Cannot access memory at address 0x1>
position++;
}
}
【问题讨论】:
-
我不认为
des [sizeof(src) + 1];正在做你认为它正在做的事情 -
除了创建指针之外,您还应该使指针指向某个地方。目前,它们只是垃圾,或者说是 0,也好不了多少。也就是说,它们指向的不是有效地址,而是 Nirvana,这就是您使用它们时的进程所在。
-
您的变量似乎未初始化。
标签: c++