看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Linux 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) gcc x64 4.8.3 或4.8.5 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 使用void** 與void*結果不一致 不曉得是compiler bug或是shared memory的address是否有地雷 餵入的資料(Input): 可以使用 python -c 'print("\x55\xAA\x55\xAA")' > struct.bin 產生測資 檔名一定要struct.bin 預期的正確結果(Expected Output): 執行以下兩行程式片段應該一樣結果 以下是call兩個不同函數 load_table_from_shm_voidpp((void**)&g_test_struct); load_table_from_shm_voidp((void*)g_test_struct); 錯誤結果(Wrong Output): load_table_from_shm_voidp((void*)g_test_struct); 執行到read會取得errno=14 bad address 用gdb看bt, 傳入的void*會是(void*)0 程式碼(Code):(請善用置底文網頁, 記得排版) gist https://gist.github.com/swt02026/d6bafca53b375e62d807c33f33a0b694 補充說明(Supplement): 因為是公司的程式碼 所以我修改成最小能重現相同狀況的程式碼 編譯參數要加上 -std=gnu11 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.42.220 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1503932763.A.ECC.html
CoNsTaR: 那你知道 void 和 void* 的差異嗎?08/28 23:57
CoNsTaR: * 的意義是什麼?08/28 23:57
james732: 你為什麼會覺得void*跟void**是一樣的東西...?08/29 00:02
alan23273850: 不過他在預期結果那裏(void**)後面有一個&耶08/29 00:35
Lipraxde: **指到*的位置,你雖然去了*的位置,但是沒把*存起來。08/29 07:25
Lipraxde: 試試看存起來再取&08/29 07:25
function名稱好像取的不是很好 推文好像都以為是同一個function.... ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 07:31:50 ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 07:33:19
Lipraxde: 抱歉我眼殘的很嚴重08/29 07:53
這是我的問題 大家都用手機 取太像會分不清 ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 08:01:29
Lipraxde: 我知道了啦,你把g_test_struct用傳值的方式丟到load_ta08/29 08:24
Lipraxde: ble_from_shm_voidp裡面。gey_shm裡面雖然有對g_test_st08/29 08:24
Lipraxde: ruct做修改,可是你的table沒有被改到08/29 08:24
soheadsome: 可是我get shm的時候 是直接用全域變數08/29 08:37
喔喔 我了解了 我傳入的時候g_test_struct 還沒指到shm 傳入table就會是還沒初始化的舊值 感謝<(_ _)> ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 08:47:12
Lipraxde: 你用全域變數沒錯,改的是全域的g_test_struct,但是區 08/29 08:47
Lipraxde: 域的table沒改到 08/29 08:47