why is p[20] valid and can hold values when :
i mean its out of the allocation rangeCode:int *p; p = (int *)calloc(10, sizeof(int));
why is p[20] valid and can hold values when :
i mean its out of the allocation rangeCode:int *p; p = (int *)calloc(10, sizeof(int));
My Tutorials :
- Bad programming practices in : C
- C\C++ Tips
(constrcutive criticism is very welcome)
- Brain Cell
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.
My best code is written with the delete key.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>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)
My best code is written with the delete key.
to get this straight , i should always use allocated spaces even if unallocated spaces work right?? (just to make sure)
Last edited by Brain Cell; 03-23-2004 at 10:12 AM.
My Tutorials :
- Bad programming practices in : C
- C\C++ Tips
(constrcutive criticism is very welcome)
- Brain Cell
>i should always use allocated spaces even if unallocated spaes work right??
Yes.
My best code is written with the delete key.
Thanks for the quick help.
Prelude : do you live on the board or something?? lol , you are everywhere..
My Tutorials :
- Bad programming practices in : C
- C\C++ Tips
(constrcutive criticism is very welcome)
- Brain Cell
>do you live on the board or something??
Sadly, it seems that way.
My best code is written with the delete key.