I'm trying my best to learn linked lists but am having a difficult time understanding them exactly any way debugger returns SIGSEGV

Debug Info:

Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb 6.7.50.20071127
Child process PID: 1572
Program received signal SIGSEGV, Segmentation fault.
At C:/Documents and Settings/anarionist/Desktop/rlgc/testing/main.cpp:21

NOTE: i am using gcc with codeblocks
code:

#include <iostream>
using namespace std;


    typedef struct node *p;
    typedef struct data * d;
    struct node{
        p next;
        d dat;
        };
        struct data{
            int id;
    };

int main(){
    int x;
    p root;
    d base;
        base->id=0;
    if(x!=20){
        root->next=new node; // i believe this is my problem but im not sure how to fix it. this is also my first time using the -> which i understand to be "points to"? 
        base->id=base->id++;
        cout<<base->id;
        x++;
    }
}

Recommended Answers

All 2 Replies

I'm trying my best to learn linked lists but am having a difficult time understanding them exactly any way debugger returns SIGSEGV

#include <iostream>
using namespace std;


    typedef struct node *p;
    typedef struct data * d;
    struct node{
        p next;
        d dat;
        };
        struct data{
            int id;
    };

int main(){
    int x;
    p root = new node();
    d base = new node();
        base->id=0; //first error was here
    if(x!=20){
        root->next=new node; // i believe this is my problem but im not sure how to fix it. this is also my first time using the -> which i understand to be "points to"? 
        base->id=base->id++;
        cout<<base->id;
        x++;
    }
}

You had initialized the pointer for root->next but you hadn't initialized root nor base yet.

-> is just shorthand for dereferencing a pointer to a struct or object and subsequently calling a member function or accessing a member variable. So if you had a class MyClass which had a (public) member named mydata and a method named getData and you had MyClass * instance in your program you could call (*instance).getData() or in shorthand instance->getData() .Same goes for (*instance).mydata versus instance->mydata .

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.