因此...有一个奇怪的。在下面的代码中,我收到一个malloc():内存损坏(快速),std :: vector和std :: stringstream
*** Error in `./a.out': malloc(): memory corruption (fast): 0x00000000011165f0 ***
在下面的函数的while循环行。
std::vector<std::string> stringSplitter(const std::string & toSplit, char split) {
std::stringstream stream(toSplit);
std::vector<std::string> toReturn;
std::string current;
while (std::getline(stream, current, split))
toReturn.push_back(current);
return toReturn;
}
我已经能够通过发送它像
SEND allenh1 passwd sup?
重现错误,但功能按预期工作时,我把它
COMMAND allenh1 passwd room1
任何想法?
这个函数的调用来自于以下几行的另一个功能:
FILE * fssock = fdopen(fd, "r+");
// Read character by character until a \n is found or the command string is full.
while (commandLineLength < MaxCommandLine &&
read(fd, &newChar, 1) > 0) {
if (newChar == '\n' && prevChar == '\r') {
break;
}
commandLine[ commandLineLength ] = newChar;
commandLineLength++;
prevChar = newChar;
}
// Add null character at the end of the string
// Eliminate last \r
commandLineLength--;
commandLine[ commandLineLength ] = 0;
printf("RECEIVED: %s\n", commandLine);
std::string input(commandLine);
std::vector<std::string> bySpace = stringSplitter(input, ' ');
此外,将输出发送到一个文本文件后,我跑了'猫-a'上该文件并没有发现无意的字符。 – allenh1
我不认为这个问题出现在这段代码中。在此发布更多代码。你用哪种方式调用这个函数。从哪里获取数据? – grisha
不幸的是,我不能发布太多的代码......使用gdb进行回溯后,该功能正常工作。错误来自while循环的额外运行。 – allenh1