如何修复:C++ 中的双重释放或损坏(出)中止(核心转储)
How to fix: double free or corruption (out) Aborted (core dumped) in C++
我最近一直在处理我的 C++ 项目,我 运行 遇到了可执行文件的问题:
std::string commanddata;
std::string input;
std::string cmdname;
int commandlength = 0;
if (commandlength == 0){}else{} // This is so that G++ doesn't complain that I have got a variable and never used it (I'm compiling at -Werror)
while (true)
{
input = DS_IO.kscanf(">>>"); // This is a specific function to get input from the user
// Record the command length
int commandlength = sizeof(input);
// Make sure that the user doesn't just send us nothing
if (commandlength <= 0)
{
continue;
}
else
{
// Get the first word from the command
std::stringstream sstream(input);
sstream >> cmdname;
// Loops through the input string to remove everything up to and including a space delimiter
for (int i = 0; i <= commandlength; i++)
{
if (input[i] == 32)
{
commanddata = input;
commanddata.erase(input.begin(), input.begin() + i);
break;
}
else
{
continue;
}
}
// print out the data to make sure it's all working
std::cout << "What you entered: " << input << std::endl;
std::cout << "The command name: " << cmdname << std::endl;
std::cout << "The command metadata: " << commanddata << std::endl;
}
}
代码工作正常,但我的问题是一旦到达循环末尾,它就会崩溃并给出此错误:
预期行为:
chemeriov@mikumputer:~/dev/DigitalSledgehammer$ ./a.out
>>>echo Hello world
What you entered: Hello world
The command name: echo
The command metadata: Hello world
>>>
经验行为:
chemeriov@mikumputer:~/dev/DigitalSledgehammer$ ./a.out
>>>echo Hello world
What you entered: Hello world
The command name: echo
The command metadata: Hello world
double free or corruption (out)
Aborted (core dumped)
chemeriov@mikumputer:~/dev/DigitalSledgehammer$
如何解决这个问题?
sizeof(input)
是一个常量,比如16字节。要查找字符串的长度,请调用 input.length()
方法。
非常感谢@Soonts,他解决了我的问题!
另一个问题,commanddata.erase( input.begin()
显然是错误的,一个容器不能擦除其他不相关容器内的范围。正确的用法是 something.erase(something.begin(), ..
你可以清楚地看出我是个菜鸟:)
我最近一直在处理我的 C++ 项目,我 运行 遇到了可执行文件的问题:
std::string commanddata;
std::string input;
std::string cmdname;
int commandlength = 0;
if (commandlength == 0){}else{} // This is so that G++ doesn't complain that I have got a variable and never used it (I'm compiling at -Werror)
while (true)
{
input = DS_IO.kscanf(">>>"); // This is a specific function to get input from the user
// Record the command length
int commandlength = sizeof(input);
// Make sure that the user doesn't just send us nothing
if (commandlength <= 0)
{
continue;
}
else
{
// Get the first word from the command
std::stringstream sstream(input);
sstream >> cmdname;
// Loops through the input string to remove everything up to and including a space delimiter
for (int i = 0; i <= commandlength; i++)
{
if (input[i] == 32)
{
commanddata = input;
commanddata.erase(input.begin(), input.begin() + i);
break;
}
else
{
continue;
}
}
// print out the data to make sure it's all working
std::cout << "What you entered: " << input << std::endl;
std::cout << "The command name: " << cmdname << std::endl;
std::cout << "The command metadata: " << commanddata << std::endl;
}
}
代码工作正常,但我的问题是一旦到达循环末尾,它就会崩溃并给出此错误:
预期行为:
chemeriov@mikumputer:~/dev/DigitalSledgehammer$ ./a.out
>>>echo Hello world
What you entered: Hello world
The command name: echo
The command metadata: Hello world
>>>
经验行为:
chemeriov@mikumputer:~/dev/DigitalSledgehammer$ ./a.out
>>>echo Hello world
What you entered: Hello world
The command name: echo
The command metadata: Hello world
double free or corruption (out)
Aborted (core dumped)
chemeriov@mikumputer:~/dev/DigitalSledgehammer$
如何解决这个问题?
sizeof(input)
是一个常量,比如16字节。要查找字符串的长度,请调用 input.length()
方法。
非常感谢@Soonts,他解决了我的问题!
另一个问题,commanddata.erase( input.begin()
显然是错误的,一个容器不能擦除其他不相关容器内的范围。正确的用法是 something.erase(something.begin(), ..
你可以清楚地看出我是个菜鸟:)