Reputation: 7
I have this code:
char *test = malloc(sizeof(char) * 1);
test[0] = 't';
test[1] = 'e';
test[2] = 's';
test[3] = 't';
test[4] = '\0';
printf("%s", test);
I malloc my test with one sizeof char. But when I run this program, my program don't segfault. Why ? ..
Thanks you !
Upvotes: 1
Views: 32
Reputation: 1772
In brief, malloc is not guaranteed to catch all memory misbehaviors of your program and throw a segmentation fault.
In fact, people would argue that this is just the best case scenario, where in the worst case you will be overwriting other data of your program.
See duplicate post for more detail.
Upvotes: 0