考慮下面的代碼:訪問衝突寫入靜態全局變量?
#include "stdafx.h"
#include "string.h"
static char *myStaticArray[] = {"HelloOne", "Two", "Three"};
int _tmain(int argc, _TCHAR* argv[])
{
char * p = strstr(myStaticArray[0],"One");
char hello[10];
memset(hello,0,sizeof(hello));
strncpy(hello,"Hello",6);
strncpy(p,"Hello",3); // Access Violation
return 0;
}
我正好在點得到一個訪問衝突,當它試圖寫入myStaticArray的地址[0]。 爲什麼這是一個問題?
背景:我移植舊的C++到C#,主要是一個C#開發人員,所以請原諒我的無知!這段代碼顯然是不能在舊的生成問題,所以我很困惑...
這不是C++。它基本上是C,與良好的C++代碼根本沒有任何關係。 – Puppy