4 Replies - 17084 Views - Last Post: 21 December 2012 - 07:13 PM Rate Topic: -----

#1 Java Student   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 498
  • Joined: 05-February 10

Deleting a pointer's target array element

Posted 21 December 2012 - 05:25 PM

In the code
SOME_CLASS* class_arr = new SOME_CLASS[4];



I know i can delete the entire pointer's target by
delete class_arr


, but why cant i delete the second element in the pointer's target's array
delete (class_arr+2)
, for example?


My guess is that this method of allocating the memory will allocate memory consequtively and form a certain continuity between the 4 class objects on the heap, and breaking this contunity between each element is not allowed, but i really have no clue.

Thanks!
and Happy Holidays.

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting a pointer's target array element

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Deleting a pointer's target array element

Posted 21 December 2012 - 06:31 PM

First of all, it should be delete [] class_arr, not delete class_arr. If it has been allocated with new[] it needs to be deleted with delete[]. Otherwise the behavior is undefined.

To actually answer your question:

Answer from a practical/implementation-minded POV: Yes, the memory is consecutive. More importantly the book keeping done by `new` and `delete` keeps track of that memory as one single chunk of memory, not four independent consecutive ones. There simply is no facility to delete just part of one such chunk - as far as the system is concerned that'd be like deleting half of an object.

Answer from a language lawyer POV: The standard only defines what happens when you call delete[] on a pointer that has been returned by new[], or delete on one that has been returned by new. class_arr has been returned by new[], so it can be deleted by delete []. class_arr + 2 has not been returned by either new or new[], so it can not be deleted by either delete or delete[] (without invoking undefined behavior).

This post has been edited by sepp2k: 21 December 2012 - 06:32 PM

Was This Post Helpful? 2
  • +
  • -

#3 Java Student   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 498
  • Joined: 05-February 10

Re: Deleting a pointer's target array element

Posted 21 December 2012 - 06:43 PM

O ok, thanks!

Another question, when i delete a pointer's target (ei: delete [] class_arr; ) should i ALWAYS follow it by setting the pointer to NULL (ei: class_arr = NULL; )?

Also, is it necessary to set all pointers to NULL after you've deleted them at the end-of-program clean-up?
Was This Post Helpful? 0
  • +
  • -

#4 BlueMelon   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Deleting a pointer's target array element

Posted 21 December 2012 - 07:04 PM

I usually set them to NULL to make the debug process more visual. Also it can prevent errors/bugs if a double delete every happens
Was This Post Helpful? 1
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Deleting a pointer's target array element

Posted 21 December 2012 - 07:13 PM

Quote

My guess is that this method of allocating the memory will allocate memory consequtively and form a certain continuity between the 4 class objects on the heap, and breaking this contunity between each element is not allowed

As sepp2k said, any run time allocating implementation will have some way of book keeping the heap memory. In some malloc implementations, the book keeping info is stored as bytes directly before the requested memory on the run time heap. This makes deleting the memory very simple. When calling delete, the pointer will simply be decremented to the book keeping info, the book keeping information is be pulled from the memory, then the memory will be deallocated. If you were using such an implementation of malloc, can you see how passing a pointer into a chunk of dynamic memory could be dangerous.

Quote

Also, is it necessary to set all pointers to NULL after you've deleted them at the end-of-program clean-up?

No it is not necessary, you can have a pointer point anywhere you like; however, you can only dereference memory that you own.

This post has been edited by jjl: 21 December 2012 - 07:17 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1