Well. I ran your program through GDB:
As you might be able to see, start->cLeft and start->cRight are not initialized. Let's take a look at the code you initalize them in:Code:$ gdb tempjunior.exe GNU gdb 5.2.1 Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i686-pc-mingw32"... (gdb) break createTree Breakpoint 1 at 0x401648: file tempjunior.cpp, line 80. (gdb) r Starting program: C:\dwk\c/tempjunior.exe Breakpoint 1, createTree(node*, int*, int) (start=0x3d2548, numbers=0x22ff30, size=10) at tempjunior.cpp:80 80 cout << "createTree(" << start << ',' << numbers << ',' << size << " )\n"; (gdb) p start $1 = (node *) 0x3d2548 (gdb) p *$ $2 = {data = -1163005939, parent = 0xbaadf00d, cLeft = 0xbaadf00d, cRight = 0xbaadf00d} (gdb) n 82 if(size % 2 == 0) (gdb) 84 startPoint = size/2; (gdb) p size $3 = 10 (gdb) n 89 start->data = numbers[startPoint]; (gdb) p startPoint $4 = 5 (gdb) p numbers $5 = (int *) 0x22ff30 (gdb) p numbers[0] $6 = 0 (gdb) p numbers[3] $7 = 3 (gdb) p numbers[9] $8 = 9 (gdb) p numbers[10] $9 = 4007024 (gdb) p *start $10 = {data = -1163005939, parent = 0xbaadf00d, cLeft = 0xbaadf00d, cRight = 0xbaadf00d} (gdb) n 97 tempP = addPly(start, numbers, startPoint); (gdb) p *start $11 = {data = 5, parent = 0xbaadf00d, cLeft = 0xbaadf00d, cRight = 0xbaadf00d} (gdb) p start->cLeft $12 = (node *) 0xbaadf00d (gdb) p *$ $13 = {data = 775171635, parent = 0x69622f32, cLeft = 0x6f2f7374, cRight = 0x65727473} (gdb)
See, there's a problem with that code. You set either cRight or cLeft, but not both. I suggest setting them both to NULL at the beginning of that function.Code:struct node *addPly(node *parent, int numbers[], int cArrayPoint) { node *nLeft; node *nRight; nLeft = new node; nRight = new node; if(cArrayPoint-1 >= 0) { nLeft->data = numbers[cArrayPoint-1]; parent->cLeft = nLeft; }else{ parent->cLeft = NULL; delete nLeft; } if(cArrayPoint+1 <= 10) { nRight->data = numbers[cArrayPoint+1]; parent->cRight = nRight; }else{ parent->cRight = NULL; delete nRight; } return parent; }
Also, why new something and then immediately delete it? I'm sure you can think of a way around that . . . .Code:parent->cLeft = parent->cRight = 0;
After playing around with it a bit, I found the function call that segfaults:
I'm not sure what's happening and unfortunately I don't have enough time to debug it, but since the function call two calls previous had cLeft=NULL, I'm guessing you don't set something to NULL somewhere. Good luck.Code:Breakpoint 2, countNodes(node*) (startPoint=0xbaadf00d) at tempjunior.cpp:202 202 if(startPoint == NULL) (gdb) p *startPoint $15 = {data = 774843950, parent = 0x2e2e2f2e, cLeft = 0x2f2e2e2f, cRight = 0x6c636e69} (gdb) bt #0 countNodes(node*) (startPoint=0xbaadf00d) at tempjunior.cpp:202 #1 0x00401a1c in countNodes(node*) (startPoint=0x3d3b40) at tempjunior.cpp:208 #2 0x00401a31 in countNodes(node*) (startPoint=0x3d27a0) at tempjunior.cpp:209 #3 0x00401a1c in countNodes(node*) (startPoint=0x3d2700) at tempjunior.cpp:208 #4 0x00401a1c in countNodes(node*) (startPoint=0x3d2660) at tempjunior.cpp:208 #5 0x00401a1c in countNodes(node*) (startPoint=0x3d25c0) at tempjunior.cpp:208 #6 0x00401a1c in countNodes(node*) (startPoint=0x3d2570) at tempjunior.cpp:208 #7 0x00401a1c in countNodes(node*) (startPoint=0x3d2548) at tempjunior.cpp:208 #8 0x004015d8 in main () at tempjunior.cpp:64 (gdb)


.