[問題] GTK語法中的 g_new() function

看板C_and_CPP作者 (時光暫停)時間15年前 (2009/08/04 16:29), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
大家午安: 我剛在trace 別人寫的 gtk程式時遇到一個問題, 程中使用了以下這段程式碼我不太懂他的意思 struct BrowserData* data = g_new0 (struct BrowserData, 1); 我去查一下gtk api中的使用原型 g_new() 定義與說明如下 #define g_new0(struct_type, n_structs) Allocates n_structs elements of type struct_type, initialized to 0's. The returned pointer is cast to a pointer to the given type. If n_structs is 0 it returns NULL. 照上面的意思 程式中宣告成 g_new0 (struct BrowserData, 1); 意思是否為分配 BrowserData 資料結構中的element 給 data ? 這一個部份我不太能了解!! 因為trace code時 我發現 data這變數可以操作 BrowserData 這資料結構中的所有的element 非只有一個 我想要請問一下大家~我是不是誤解 g_new()這函式的意思 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.72.235 ※ 編輯: ppp168p 來自: 140.116.72.235 (08/04 16:30)

08/04 16:35, , 1F
ABC *data = (ABC *)malloc(sizeof(ABC) * 1);
08/04 16:35, 1F

08/04 16:36, , 2F
memset(data, 0, sizeof(ABC));
08/04 16:36, 2F

08/04 19:04, , 3F
我了解意思了~謝謝樓上^^
08/04 19:04, 3F
文章代碼(AID): #1AT_5tOY (C_and_CPP)