question on array in c++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a question about array in vc++. I initialized an array from 0 to
480 using memset. and I just realized that in one instance of my code i am
putting a value in array position -2, which the array does not have. will
this action cause a problem if i try to use delete to deallocate the array
from memory?? thank you in advance! help is greatly appreciated!
 
Hi, I have a question about array in vc++. I initialized an array from 0 to
480 using memset. and I just realized that in one instance of my code i am
putting a value in array position -2, which the array does not have. will
this action cause a problem if i try to use delete to deallocate the array
from memory?? thank you in advance! help is greatly appreciated!

Writing outside an array will corrupt whatever object is stored there, if
any. It could be part of the heap's internal fields, it could be another
object, it could be unallocated memory, or it could be inaccessible memory,
in which case, you'll get an access violation. So don't ever write outside
an array's storage.

That said, if you have a pointer p to an element inside an array, and that
element is at index 2 or beyond, then p[-2] is a valid way to refer to the
item 2 positions before *p.
 
Back
Top