strcpy System.AccessViolationException

时间:2012-07-24 17:49:36

标签: c++ c++-cli

当我到达strcpy线时,我正在

An unhandled exception of type 'System.AccessViolationException' occurred. 
Additional information: Attempted to read or write protected memory. 
This is often an indication that other memory is corrupt.


char* str; char* out;
str = (char*) Marshal::StringToHGlobalAnsi(Parms["AVI"]).ToPointer();  
strcpy(out, str);  
Marshal::FreeHGlobal(IntPtr(str));

3 个答案:

答案 0 :(得分:2)

您需要分配内存并将out指向该内存。就像现在一样,out指向一些随机存储位置。

答案 1 :(得分:0)

设置str后,但在将其复制到out之前:

char* out = (char*)malloc((strlen(str) + 1) * sizeof(char));

if (out != NULL) {
    strcpy(out, str);
}

因为你需要某种缓冲区来复制。完成后一定要自由(退出)。

答案 2 :(得分:0)

How to convert from System::String* to Char* in Visual C++

“方法3”看起来非常简单。无需手动释放内存。

相关问题