Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
It is trivial enough to test: #include <stdlib.h> #include <stdio.h> #include <errno.h> #define KNOWN_GARBAGE ((int*)~0u) int main (void) { int* ptr = KNOWN_GARBAGE; ...
Answer
#2: Post edited
I'm not sure if "give me a list" type of questions are ideal for this site. That being said, it is trivial enough to test:- ```c
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #define KNOWN_GARBAGE ((int*)~0u)
- int main (void)
- {
- int* ptr = KNOWN_GARBAGE;
- ptr = malloc(0);
- int errno_changed = errno;
- if(ptr == NULL)
- puts("Returned NULL.");
- else if(ptr == KNOWN_GARBAGE)
- puts("Didn't modify the pointer, non-conforming?");
- else
- puts("Returned non-zero, modified the pointer.");
- if(errno_changed)
- printf("Weird use of errno detected, error code 0x%X\n", errno_changed);
- }
- ```
- Then try it with whatever compiler, version, standard lib and system you want. The vast majority of gcc-like compilers + libc flavours appear to return a new non-zero address.
- The setting `errno` part appears to be some old Unix gunk from the 90s according to `man`(?), so you may have to find some sufficiently obscure computer for that, I guess.
- ---
- Related to this question: C no longer has standard support for `realloc(ptr, 0)` since C23 likely comes with major defects here. See [realloc(ptr, 0) in C23 - now what?](https://stackoverflow.com/questions/78691087/reallocptr-0-in-c23-now-what)
- It is trivial enough to test:
- ```c
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #define KNOWN_GARBAGE ((int*)~0u)
- int main (void)
- {
- int* ptr = KNOWN_GARBAGE;
- ptr = malloc(0);
- int errno_changed = errno;
- if(ptr == NULL)
- puts("Returned NULL.");
- else if(ptr == KNOWN_GARBAGE)
- puts("Didn't modify the pointer, non-conforming?");
- else
- puts("Returned non-zero, modified the pointer.");
- if(errno_changed)
- printf("Weird use of errno detected, error code 0x%X\n", errno_changed);
- }
- ```
- Then try it with whatever compiler, version, standard lib and system you want. The vast majority of gcc-like compilers + libc flavours appear to return a new non-zero address.
- The setting `errno` part appears to be some old Unix gunk from the 90s according to `man`(?), so you may have to find some sufficiently obscure computer for that, I guess.
- ---
- Related to this question: C no longer has standard support for `realloc(ptr, 0)` since C23 likely comes with major defects here. See [realloc(ptr, 0) in C23 - now what?](https://stackoverflow.com/questions/78691087/reallocptr-0-in-c23-now-what)
#1: Initial revision
I'm not sure if "give me a list" type of questions are ideal for this site. That being said, it is trivial enough to test: ```c #include <stdlib.h> #include <stdio.h> #include <errno.h> #define KNOWN_GARBAGE ((int*)~0u) int main (void) { int* ptr = KNOWN_GARBAGE; ptr = malloc(0); int errno_changed = errno; if(ptr == NULL) puts("Returned NULL."); else if(ptr == KNOWN_GARBAGE) puts("Didn't modify the pointer, non-conforming?"); else puts("Returned non-zero, modified the pointer."); if(errno_changed) printf("Weird use of errno detected, error code 0x%X\n", errno_changed); } ``` Then try it with whatever compiler, version, standard lib and system you want. The vast majority of gcc-like compilers + libc flavours appear to return a new non-zero address. The setting `errno` part appears to be some old Unix gunk from the 90s according to `man`(?), so you may have to find some sufficiently obscure computer for that, I guess. --- Related to this question: C no longer has standard support for `realloc(ptr, 0)` since C23 likely comes with major defects here. See [realloc(ptr, 0) in C23 - now what?](https://stackoverflow.com/questions/78691087/reallocptr-0-in-c23-now-what)