c - Why am I not getting a memory exception here -
this question has answer here:
- how dangerous access array out of bounds? 10 answers
suppose have data structure
struct foo{ int a; int b; };
now create array of 2 items . this
struct foo* farry = (struct foo*) malloc(2 * sizeof(struct foo));
please correct me if wrong above create 2 slots having foo structure default initialized ? correct ? if if this
struct foo* farry = (struct foo*) malloc(2 * sizeof(struct foo)); farry[0].a =1; farry[1].a =2; farry[2].a =3; farry[3].a =4; farry[4].a =5; for(i=0 ; i<=4 ; i++) { printf("value %d \n",farry[i].a ); }
then why @ farry[2].a =3
not tell me memory error occured. instead prints 1,2,3,4,5
you access uncontrolled locations in heap in program , user space. nothing wrong point of view no memory error.
simply you're messing other static variables in heap. should mess program crash (for example if messing reason reaches program stack or variable being messed make loop wreak havoc). yes c
wild respect.
Comments
Post a Comment