这段代码为我提供了运行时异常:第一个strcpy命令中的Unhandled exception at 0x00401189 in ControlFileChanges.exe: 0xC0000005: Access violation writing location 0xbaadf00d

char** withStrings(string s1, string s2, string s3, string s4, string s5)
{
char** pipes;
pipes = (char**) malloc(sizeof(*pipes)*5);

strcpy(pipes[0],s1.c_str());
strcpy(pipes[1],s2.c_str());
strcpy(pipes[2],s3.c_str());
strcpy(pipes[3],s4.c_str());
strcpy(pipes[4],s5.c_str());

return pipes;
}


知道可能是什么问题吗?
(这是我在实际代码中使用类似逻辑的示例)。

最佳答案

您没有为pipes[0]pipes[1]等分配内存。

由于这是C ++而不是C,因此您是否考虑使用new而不是malloc?或者使用vectorstring

关于c++ - char双指针程序中的访问冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11735022/

10-11 01:11