-1

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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • The tone of this post is such that I considered voting to close, for being argumentative and unconstructive; perhaps you could make it a little less rant-ish and stick more to your question? – ptomato Sep 14 '11 at 15:54
  • API design is difficult (http://www.infoq.com/presentations/effective-api-design), but if something walks like a duck and talks like a duck but in fact is a pigeon, it is a severe failure to not document this fact in a easy accessible place (i.e. together with the function description and not on display somewhere else behind a tiger warning memory handling page"). – hlovdal Sep 15 '11 at 13:13
  • 1
    I don't wish to debate API design or the placement of the warning. I agree that it could be better and I understand your frustration. I was objecting to your antagonistic tone, and the long rambling bit before you asked your actual question. Stack Overflow frowns upon these things. However, I did not vote to close your post. I didn't even downvote it. I assumed you were venting some frustration, which we all need to do from time to time, and asked you to edit it. This request still stands. – ptomato Sep 15 '11 at 14:40

1 Answers1

4

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.

ptomato
  • 56,175
  • 13
  • 112
  • 165