开发者

What does 'const gchar *' mean, and does that memory need to be freed after use?

I have the following code:

int main()
{

const gchar *wew = gtk_entry_get_text(GTK_ENTRY(gtkentry开发者_开发知识库widget));

return 0;
}

gtk_entry_get_text() returns a const gchar*, so does wew need to be deallocated or not, and why?


A gchar is just a typedef for the C type char. You must not deallocate this specific pointer. Per the documentation:

Returns : a pointer to the contents of the widget as a string. This string points to internally allocated storage in the widget and must not be freed, modified or stored.


gtk_entry_get_text returns a const gchar* instead of a gchar* to prevent you from trying to free the memory. The documentation of the function even tells you so. Of course if you cast the returned value to gchar*, you're able to free it, but that's just because the C language doesn't prevent you from doing silly things.

For exemple, as that memory chunk is internally used by your GtkEntry, if you free that memory chunk, and later call gtk_entry_set_text, your program will crash. This is because the memory where it tries to write is unallocated...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜