c - Access beyond indexes of allocated memory for pointer -
this question has answer here:
- no out of bounds error 7 answers
#include <stdio.h> #include <stdlib.h> int main() { char *p = malloc(10); int i; for(i=0;i<15;i++) { p[i]='c'; printf("index:%d %c\n",i,p[i]); } return 0; }
i'm not sure why in above code, allocated memory of 10 still able access 15th index of pointer.
i'm not sure why thought because pointer points random chunk of memory , i'm overwriting part of memory, i'm allocating specific amount of memory i'm not sure why work.
can confirm?
i'm not sure why in above code, allocated memory of 10 still able access 15th index of pointer.
accessing memory beyond requested cause undefined behavior. should consider unlucky if program not crash. behave strangely @ inopportune moment.
Comments
Post a Comment