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
new operator question
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.
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
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.
==========================================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.
There is no memory leaks if operator= is written(properly ), but the data is still deleted.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement