Archived

This topic is now archived and is closed to further replies.

Bleakcabal

new operator question

Recommended Posts

I have the following code in my function, then I do a number of operations. I need to do it again to add a new node. Can I do it ? Will I loose all the information on my other nodes ? If so, what is the best way to approach this problem. TreePointer = new s_Node[NodeNumber]; // dynamicly generate array of nodes WHO DO THEY THINK THEY''RE FOOLING : YOU ?
GARAL website

Share this post


Link to post
Share on other sites
yep, you will loose the stuff stored in TreePointer.
what you do tell the compiler in the code you written:

>>new s_Node[NodeNumber];
allocate memory for NodeNumber of s_Node''s on the heap.
it returns the adress where the allocated memory starts.
>>TreePointer =
Set TreePointer to the adress returned by new.
This will of course overwrite the value previously stored in TreePointer, which gives you a nice little memory leak

Take a look at the Vector from STL if you want a resizeable array.

Share this post


Link to post
Share on other sites
Or you could write your own linked list class. I''d recommend this one, because you''ll learn more and have much more fun

==========================================
In a team, you either lead, follow or GET OUT OF THE WAY.

Share this post


Link to post
Share on other sites
"This will of course overwrite the value previously stored in TreePointer, which gives you a nice little memory leak "

There is no memory leaks if operator= is written(properly ), but the data is still deleted.


Share this post


Link to post
Share on other sites
Jaxks:
Would you expect someone who asks about the how the new operator works, having overridden his operators?

not..
so the data aint deleted, and you have a memory leak.

Share this post


Link to post
Share on other sites