I was running this code to expect an error but to my surprise, it didn't. Even if I initialize a string*(5 bytes)* that is greater than what was allocated using malloc (4bytes).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char * name = malloc(4);
name = "hello";
printf("%s\n",name);
return 0;
}
This is the output
$ sample.c
$ ./a.out
hello
What might be the underlying details behind this and also why it is not showing segmentation fault as I didn't free the memory?