0
Why it results all the time same whether I allocated only for 40 members?
#include <stdio.h> #include <stdlib.h> int main() { int *ptr; ptr = malloc(10*sizeof(*ptr)); /* a block of 10 ints */ if (ptr != NULL) { *(ptr+210) = 50; } printf("%d", *(ptr + 210)); return 0; }
1 Antwort
+ 2
You assign 50 at pointer offset 210 in a memory area outside array bounds, sometimes you dont crash, but you can corrupt memory.
It is similar to a dangling pointer, it point to memory that it does not own (or no longer owns).
For errors like this, a crash is best thing that can happen to alert you that something is wrong. but it does not happen always.