So, I have some function returning a pointer, or NULL on error. Now I’d like to add another possible value for a different error/condition. I heard this mentioned in some other answer before – using malloc() to create a unique pointer that will serve as a possible value for such functions to return (so now the can return a proper pointer, NULL or 0xWhatever and you can be sure 0xWhatever won’t be used for anything else). So, naturally malloc(1) is probably a safe bet, but I was wondering if malloc(0) is also safe for this. Will a malloc(0) address possibly be used for something else? Can someone clarify on how this technique should work in general, and maybe what it is called?
So, I have some function returning a pointer, or NULL on error. Now I’d
Share
From the C99 standard:
You’re better off using
malloc(1). That’s guaranteed to return non-NULL if the memory is available.