malloc():内存损坏(快速),std :: vector和std :: stringstream

时间:2015-04-25 22:20:09

标签: 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(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, ' ');

1 个答案:

答案 0 :(得分:-1)

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;
}