开发者

Does g_strdup return NULL on memory allocation failure?

The glib documentation lacks many important things that I think API documentation absolutely should include. For instance the entry for g_malloc says nothing about that it will crash开发者_如何学运维 upon memory allocation failure (in direct contrast to the behaviour of the standard malloc, which the name implies that it mimics). Only if you happen to notice that there also is a variant named g_try_malloc and read its description you will be informed that g_try_malloc

Attempts to allocate n_bytes, and returns NULL on failure. Contrast with g_malloc(), which aborts the program on failure.

Now for the question, glib have a function g_strdup which also does not mention anything about possibly returning NULL. I assume that it will not since it is implied that it will be using g_malloc internally. Will it?


The documentation does say it, though. Check the introductory section to the "Memory Allocation" page in the GLib manual:

If any call to allocate memory fails, the application is terminated. This also means that there is no need to check if the call succeeded.

This goes for any library call that allocates memory, and therefore for g_strdup() too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜