Insufficient buffer, but flawless allocation!? How's it possible?
Hello friends. Today I encountered a strange thing in C. Please look at the code below : char* x; char y[] = "123"; strcpy(x, y); fputs(x, stdout); Running this code will lead to a segment fault. The reason is clear and it is because we've not provided variable 'x' with an appropriate amount of buffer. This will be fixed using malloc() function: x = malloc(sizeof(y)); And it is because 'x' has sufficient buffer to store those characters. Now if I do this: x = malloc(0); it will still work, without any error. WHY ?? I passed no room to 'x' to have, in order to store any character! But it still works as expected! But I guess it should've given a segment fault just like previous time, can you clear this up to me ?? Can you let me know what's going on here ?