char双指针程序中的访问冲突

Sha*_*ala 0 c++ pointers char

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

cni*_*tar 7

你没有分配内存pipes[0],pipes[1]等等.

既然这是C++而不是C,你考虑使用new而不是malloc?或者也许使用a vectorstrings?

  • @ShaileshTainwala C++中的等效代码根本不使用new.您使用字符串向量,并且`withStrings`函数根本不存在. (2认同)