views:

128

answers:

3
int Size(struct node* node)
{
   if(node == NULL)
   {
      return 0;
   }
   else if(node != NULL)
   {
      return (Size(node->left) + 1 + Size(node->right));
   }
}

Hi, can anybody please post the stack trace for the following piece of code.

Lets say if we insert the values 2, 1, 10, 5... Then what could be the stack representation during the recursion process.. Please, its very urgent, and its very confusing too...

+1  A: 

I'll teach you how to fish... here is how you do it: http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c

Lirik
It is of no use.. I am working on Linux.. i have heard of gdb tool, but then i am not able to install it on my machine(which i downloaded from gnu website). During MAKE it is throwing some errors, at the end.. So if any help in this case, or explanation for the program, then it will be highly appreciated..Thankx..
AGeek
@RBA try to install it via packet manager(apt,yum,...) and you can use eclipse ide or netbeans ide to make debugging process much easy
Maksim Burnin
There should be a gdb package in your distribution. Install that, rather than trying to build gdb from source.
Andrew McGregor
tried installing yum too.. but then no success..:-)How does this apt command workk.. can u give me the complete command for this..
AGeek
@RBA I'm not a unix user, but the second answer suggests that you should be able to do it with glibc http://www.gnu.org/software/libc/manual/html_node/Backtraces.htmlI think the installation problem should be another question, perhaps on www.superuser.com
Lirik
@RBA you should be logined as root to be able to install something via yum. then "yum install gdb"
Maksim Burnin
yum install gdb not working... firstly yum was not found, i download the zip file from internet, and then installed the same.. but now its giving some error,, some package file missing.. not compatible with python.. How cann i do this...
AGeek
A: 

Try using gdb and see the backtrace/bt command for gdb.

Karan
+3  A: 

Why not simply use printf? One when entering and one when leaving the function:

int Size(struct node* node)
{
    printf("Enter %d\n", ( node ? node->value : -1 ));
    ...
    printf("Leave %d\n", ( node ? node->value : -1 ));
}
Secure
This idea is pretty kool.. but where to put this "Leave" printf statement, in my code.. I want to understand the flow of the programm that i have posted.. plz little bit guidance..:-)Anyways Thankx for that idea..:-)
AGeek
Put it before each return. You have to restructure the function a bit and use a temporary variable for the second return, of course. Since you're already working with recursion, I'm sure you know how to do this. And be prepared to answer the question what the ?: is doing when your tutor asks you about it. Or instead use ifs to check the NULL case.
Secure
Hmm thnx for the reply.. I am working in an IT Company, and just improving upong my data structure concepts,, so no point comes of tutor as such!! Anywayz thanx a lot for that stuff.. :-) Keep Rocking!!
AGeek