[問題] void*與void**差異?

看板C_and_CPP作者 (師大狗鼻哥)時間7年前發表 (2017/08/28 15:06), 7年前編輯推噓3(3010)
留言13則, 5人參與, 最新討論串1/1
開發平台(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

08/28 23:57, , 1F
那你知道 void 和 void* 的差異嗎?
08/28 23:57, 1F

08/28 23:57, , 2F
* 的意義是什麼?
08/28 23:57, 2F

08/29 00:02, , 3F
你為什麼會覺得void*跟void**是一樣的東西...?
08/29 00:02, 3F

08/29 00:35, , 4F
不過他在預期結果那裏(void**)後面有一個&耶
08/29 00:35, 4F

08/29 07:25, , 5F
**指到*的位置,你雖然去了*的位置,但是沒把*存起來。
08/29 07:25, 5F

08/29 07:25, , 6F
試試看存起來再取&
08/29 07:25, 6F
function名稱好像取的不是很好 推文好像都以為是同一個function.... ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 07:31:50 ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 07:33:19

08/29 07:53, , 7F
抱歉我眼殘的很嚴重
08/29 07:53, 7F
這是我的問題 大家都用手機 取太像會分不清 ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 08:01:29

08/29 08:24, , 8F
我知道了啦,你把g_test_struct用傳值的方式丟到load_ta
08/29 08:24, 8F

08/29 08:24, , 9F
ble_from_shm_voidp裡面。gey_shm裡面雖然有對g_test_st
08/29 08:24, 9F

08/29 08:24, , 10F
ruct做修改,可是你的table沒有被改到
08/29 08:24, 10F

08/29 08:37, , 11F
可是我get shm的時候 是直接用全域變數
08/29 08:37, 11F
喔喔 我了解了 我傳入的時候g_test_struct 還沒指到shm 傳入table就會是還沒初始化的舊值 感謝<(_ _)> ※ 編輯: soheadsome (223.137.244.234), 08/29/2017 08:47:12

08/29 08:47, , 12F
你用全域變數沒錯,改的是全域的g_test_struct,但是區
08/29 08:47, 12F

08/29 08:47, , 13F
域的table沒改到
08/29 08:47, 13F
文章代碼(AID): #1Pf35RxC (C_and_CPP)