I_am_a_Goon
I_am_a_Goon

Reputation: 31

2 [main] hw3 10368 cygwin_exception::open_stackdumpfile: Dumping stack trace to hw3.exe.stackdump

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <utility>
using namespace std;

struct node
{
    int level = -1;
    int value = -5;
    node *left;
    node *right;
};

int array[100][100];
void storetree(node *root, int val);
void printtree();

int main()
{
    cout << " u there?" << endl;

    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            array[i][j] = -5;
        }
    }

    ifstream file1;
    ifstream file2;
    file1.open("tree1.txt");
    file2.open("graph1.txt");
    node root;
    node *root1;
    int val1;
    int randval;

    file1 >> val1;
    root.level = 0;
    root1->level = 0;
    root.value = val1;
    root1->value = val1;
    array[0][0] = val1;

    cout << "made it" << endl;

    root1->left->value = -5;     // <-- Error happens here
    root1->right->value = -5;
    root1->left->level = -5;
    root1->right->level = -5;

So my error happens when I access root1->left->value, and I get the stack dump error.

Is it impossible to access root1->left->value the way I've written it? Through print statements I've deduced that this is the error. I don't understand pointers well and would appreciate help. :)

Upvotes: 1

Views: 2387

Answers (1)

Scott Smith
Scott Smith

Reputation: 3986

I've annotated your source code below:

...

// Allocate an actual instance of your 'node' struct (local variable, on the stack)
node root;

// This allocates a POINTER variable, which just contains an address.
// The compiler knows that 'root1' will point at 'node' types.
// Note that you've only created a pointer variable here; NOT an actual node.
// Further, you haven't initialized it, so its value is "undefined"
// (you can't assume you know what its value is ... it could be anything).
node *root1;

...

// Set 'level' on the root node to zero
root.level = 0;    // This is OK, because root is an actual 'node' struct instance

// Set 'level' ON THE NODE THAT 'root1' POINTS TO to zero
root1->level = 0;  // This is not OK, because root1 is a wild (uninitialized) pointer

This is your first problem. You created a pointer, but you didn't point it at anything. The value of root1 (i.e., the address it refers to) is undefined (could be NULL, could be whatever was in memory where that variable resides)

It's good practice to initialize your local variables right when you define them. Uninitialized variables can have undefined values, and that can add countless hours of debugging to your life, where each run of your program does something different from the last one. Bleh.

If you're not ready to assign the actual value when you define a variable, set it to some known value like 0, nullptr, whatever. If you forget to set it later, your program will at least do the SAME wrong thing each time.

node *root1 = nullptr;  // (...or maybe your compiler uses "NULL" or "(void *)0"...)

It looks like you're going to be building up a tree of nodes based upon whatever you read in from the input file? If so, then you're almost certainly going to be dynamically allocating node structs, since you can't know how many you'll need ahead of time.

// Allocate and initialize a new node struct (on the heap)
root1 = new node();

// Set 'level' ON THE NODE THAT 'root1' POINTS TO to zero
root1->level = 0;  // Yay, now this works

The variable root1 now contains the address of the newly-allocated node struct. The rest of your code should work from there.

Just a reminder that a 'correct' program (e.g., one that doesn't leak memory) should ultimately call delete on every pointer returned by a call to new. Also, keep that in mind as you're building your tree of dynamically-allocated node objects; you'll need to call delete on each of them (except for root, which you didn't allocate dynamically) when you're all done.

Upvotes: 1

Related Questions