C++ malloc():内存损坏(fast)、std::vector和std::stringstream
所以。。。有个奇怪的。在下面的代码中,我收到一个C++ malloc():内存损坏(fast)、std::vector和std::stringstream,c++,stdvector,stringstream,C++,Stdvector,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(toS
*** 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;
}
但当我发送它时,该功能会按预期工作
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, ' ');
FILE*fssock=fdopen(fd,“r+”);
//逐字符读取,直到找到\n或命令字符串已满。
while(commandLineLength0){
如果(newChar='\n'&&prevChar='\r'){
打破
}
命令行[commandLineLength]=newChar;
commandLineLength++;
prevChar=newChar;
}
//在字符串末尾添加空字符
//消除最后一个\r\n
命令行长度--;
命令行[commandLineLength]=0;
printf(“收到:%s\n”,命令行);
字符串输入(命令行);
std::vector bySpace=stringSplitter(输入“”);
向量拆分(字符串str,字符分隔符){
向量内部;
stringstream ss(str);//将字符串转换为流。
弦托克;
while(getline(ss、tok、分隔符)){
内部。推回(tok);
}
返回内部;
}
此外,在将输出发送到文本文件后,我在该文件上运行了一个cat-a
,没有发现意外字符。我认为问题不在这段代码中。在这里发布更多代码。以何种方式调用此函数。从哪里获取数据?不幸的是,我不能发布太多代码。。。使用gdb进行回溯后,该函数工作正常。该错误来自于对while循环的额外运行。我将向您展示调用它的位置。我会将其放入调试器中,并验证commandLineLength--;命令行[commandLineLength]=0
正在做您认为应该做的事情,特别是如果读取
在开始时的“第二次”迭代失败,从而将commandLineLength
保留为零,从而让您有效地这样做:commandLine[-1]=0代码>。那太糟糕了。这就是调试器的用途。你必须详细说明你想用这个答案表达什么以及为什么。就目前而言,它没有任何用处。它取代了不应调用内存错误的stringSplitter。您既没有解释原始版本导致内存错误的原因,也没有解释版本不导致内存错误的原因。只是我们彼此了解,我不认为函数的任何一个版本都会导致错误,而是因为原始海报太懒了,无法真正提取一个最小的示例来证明问题在其他地方。
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, ' ');
vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}