Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Dynamic Memory... 1

Status
Not open for further replies.

Savaal

Programmer
Mar 9, 2004
2
US
I have a pointer that points to a node with dynamic memory allocated. Suffice it to say that my destructor for the node pointers does it's job perfectly, but if I need to delete the entire thing, my compiler spits out nullReferencePointer errors. I have an array of 100 pointers to nodes in a hash table setup. If I have data in tablepointer[50] I would think to call delete on tablepointer[50], and with that, I would assume that the rest of the pointers down the line would have their destructors called, and be safely deallocated. Is this correct? Why am I getting errors?

Savaal
 
If you want to delete a dynamically allocated array you have to do something like this

int *array = new int[100];
delete [] array;

I've never done it, but I believe you can do this as well to delete specific items

int *array = new int[100];
delete array + i; // deletes the ith element (the rest are left alone)

This is the same thing as
delete &array;

Just saying delete array[50] is no good because array[50] is the value, not the pointer.
 
>I have an array of 100 pointers

Ie some thing like
Code:
SomeThing* array[100];
...
array[50] = new SomeThing;

> I would think to call delete on tablepointer[50],
delete array[50]; will only delete that element (and its dtor will be called).

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
thanks. It seems that my destructor was in ill-repair. I fixed the problem, and your code was correct. Thanks, Per.

Savaal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top