5 ms·
Null is not a valid pointer, though. It's a valid pointer value, but it's undefined to dereference such a pointer. If the purpose of malloc is to return an addr
by scott_s 11y ago
Null is not a valid pointer, though. It's a valid pointer value, but it's undefined to dereference such a pointer. If the purpose of malloc is to return an address to usable memory, then null it outside of that range. Values outside of the legal range of a function are fine for error conditions. Because of that, null seems like a reasonable return value for error on malloc: I could not allocate memory for you, which I indicate by returning an address that you are not supposed to dereference.
However, I do agree it would be better if there was some other kind of error condition, preferably something like the option type from various other languages, or just an easy way to return multiple values like in Go. But C is not that language.
- jzwinck 11y agoC can return a struct from a function. That's multiple return values.
- scott_s 11y agoYeah, but it's cumbersome because you have to define that struct elsewhere, and C does not have elegant mechanisms for destructing in-place.
- ratboy666 11y agoHow about #include <stdio.h> typedef struct mystruct { int x; char *p; } mystruct; mystruct func(int n) { return (mystruct) { n, NULL }; } int main(void) { mystruct v = func(1); printf("%d\n", v.x); printf("%d\n", func(2).x); return 0; } Works fine with gcc 5.3.1. Multiple destructuring is (maybe) problematic, but if you really want this.
- scott_s 11y agoThat has both downsides I mentioned.
- dragontamer 11y ago> Null is not a valid pointer, though. There are embedded systems out there (that have C compilers) that have addresses at 0 actually. 8051 C programmers know what I'm talking about. IIRC, 0 is totally an address when inside the Linux kernel as well. What else do you call the first block of memory on a system? Just because in userland the Linux Kernel pages memory to a very high-numbered region does not mean that under all circumstances "0" is an invalid address. The entire concept of "null == 0 == invalid" is an innate falsehood, an abstraction brought about by the malloc function (that a lot of other libraries have decided to copy). But really, what do YOU call the first physical byte of RAM? Most systems I know of... from 8051 all the way to even Linux Kernel... calls it 0.
- zAy0LfpBZLC8mAC 11y agoNobody says that NULL has to be 0. C's NULL is just a pointer value that can be compared for equality but can not be used for pointer arithmetic or be dereferenced. Implementations commonly reserve some address (often 0) to represent that pointer value, as that is just the most efficient way to do it (rather than making pointers larger than the normal word size to be able to represent the sentinel value)--also, object sizes are limited to one less than the number of possible addresses anyway, simply because you can have objects that are 0-sized.
- avian 11y agoFrom the point of view of C, it seems that NULL is indeed always defined as 0. At least according to the C FAQ: http://c-faq.com/null/machnon0.html http://c-faq.com/null/machnon0.html It might be that the internal representation used by the compiler is different, but from C a NULL pointer will always be equal to 0. See also: http://c-faq.com/null/ptrtest.html http://c-faq.com/null/ptrtest.html
- zAy0LfpBZLC8mAC 11y agoWell, yes, but from the view of C, pointer values are arbitrary anyway. There is no guarantee that a particular pointer's value has any particular relationship to which RAM cell of your hardware you are accessing.