memset 導致的段錯誤(segmentation fault)


在調試Minixml庫時,定義了一個結構體:

struct ssid_info_s{

std::string wl_ssid_name;
std::string wl_ssid_mac;
std::string wl_wpa_psk;
bool wl_bss_enabled;
unsigned int wl_sta_num;
unsigned int wl_maxassoc;
bool wl_hide;
bool wl_radio;
bool wl_isolate;
unsigned int wl_pwr_percent;
unsigned int wl_bw;
unsigned int wl_band;
unsigned int wl_nmode;
unsigned int wl_auth_mode;
unsigned int wl_channel;
};

做類似以下操作:

void fun(){

struct ssid_info_s ssid_info;

memset(&ssid_info, 0, sizeof(structs sid_info_s));

printf("111111\n");}

沒有立即引起段錯誤(后面的printf仍然有輸出),反而在函數返回的時候出發了段錯誤。

這里涉及到sizeof(std::string )的問題,依賴於標准庫的在不同平台的實現,而這個求出來的大小不是我們期望的string對象的字符長度,所以導致memset引發段錯誤。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM