Heap vs Stack

Nov 17, 2015 at 11:28am
I'm writing a recursive function (for IDS/DFS algorithm), and using dynamic stack to save the nodes, which being initialized everytime I enter the function after some time I run out of memory and dont get an answer. Should I change my dynamic stack to regular stack, I mean from

 
  stack<node> *s = new stack<node>;

To
 
  stack<node> s;
Nov 17, 2015 at 11:32am
If you create something with new you have to destroy it with delete when you are finished using it, otherwise you will have a memory leak.

Yes, you should avoid dynamic allocations, and only use it when you have to.
Nov 17, 2015 at 11:36am
Thanks i forgot about the delete, but if the dynamic stack is empty, does it still take space on heap?
Nov 17, 2015 at 11:37am
Yes.
Nov 17, 2015 at 3:15pm
deleting the allocated data is one thing. I also wonder if your recursion code runs too deep and broke the stack?
Topic archived. No new replies allowed.