why is p[20] valid and can hold values when :
i mean its out of the allocation range :confused:Code:int *p;
p = (int *)calloc(10, sizeof(int));
Printable View
why is p[20] valid and can hold values when :
i mean its out of the allocation range :confused:Code:int *p;
p = (int *)calloc(10, sizeof(int));
It may be able to hold values, but it isn't valid. Accessing memory outside of your address space results in undefined behavior. That means that it could work like you expect, or it could fry your monitor. You just don't know.
However, the tendancy towards the latter varies according to
- the larger the program becomes (larger programs die more frequently than small programs)
- the longer the program takes to run (long running programs die more frequently than short lived programs)
- the amount of time left before the deadline / release date (the closer the deadline, the more likely failure will occur)
This means that simple one-line tests of out of bound accesses to malloc'ed memory almost always succeed, which is a pity really because newbies don't get to see the real mistake until much later, when they've learnt some bad habit.
>However, the tendancy towards the latter varies according to
You forgot the most annoying one:
- the time and place that the program is run (programs die more frequently during demos and reviews)
to get this straight , i should always use allocated spaces even if unallocated spaces work right?? (just to make sure)
>i should always use allocated spaces even if unallocated spaes work right??
Yes.
Thanks for the quick help.
Prelude : do you live on the board or something?? lol , you are everywhere..
>do you live on the board or something??
Sadly, it seems that way.