Advertisement

new operator question

Started by January 26, 2001 05:57 PM
3 comments, last by Bleakcabal 24 years ago
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
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
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.
Jonas Meyer Rasmussenmeyer@diku.dk
Advertisement
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.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
"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.


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.
Jonas Meyer Rasmussenmeyer@diku.dk

This topic is closed to new replies.

Advertisement