[問題] 有關memcpy和unsign char的使用
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
是眾多行程式碼中的一段
拿掉這段就不會有事
否則會出現segment fault
餵入的資料(Input):
使用memset在記憶體當中填入零
預期的正確結果(Expected Output):
應該要印出東西
錯誤結果(Wrong Output):
segment fault
程式碼(Code):(請善用置底文網頁, 記得排版)
unsigned char *h_data,*h_result;
int strlen=8;
size_t strsize=strlen * sizeof(unsigned char);
h_data =(unsigned char*)malloc(strsize);
memset(h_data,0,strlen);
memcpy(h_result,h_data,strlen+1);//<----我覺得是造成segment fault的關鍵
cout<<"h_result="<<h_result<<endl;
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.133.41.64
→
03/19 21:27, , 1F
03/19 21:27, 1F
→
03/19 21:29, , 2F
03/19 21:29, 2F
→
03/19 21:32, , 3F
03/19 21:32, 3F
→
03/19 21:48, , 4F
03/19 21:48, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章