The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ Help!!!

JeiceJeice regular
edited September 2008 in Help / Advice Forum
I call this method in the main method but I get the runtime error: "Segmentation Fault"

The error is caused by the line in green.

Now, the messed up thing I don't get is, if I remove the declarations that are in red, this code will work. I'm using the g++ compiler, and when I type in "g++ -E test.cc" I don't see anything that the preprocessor added, so I have no clue why adding the declarations gives me a runtime error.

struct Node {
string fileName;
Node *link;
};

void ProcessOneFile(istream &in, ostream &out, Node *&flist) {

string line;
char ch; //used to see if the next char == #
char s[11]; //array of chars to hold the string: #include "
char fName[256]; //fileName
string file;
ifstream includeFile;


out << flist->fileName << endl;

Node *temp = flist;
Node *curr = flist;

Node *nextNode; // new node to be added

nextNode->fileName = "f2";
nextNode->link = NULL;

temp->link = nextNode;

curr = curr->link;


flist = flist->link;
out << flist->fileName << endl;

} // end ProcessOneFile

All this code is doing is adding a new node to flist. Please help.

Jeice on

Posts

  • khainkhain Registered User regular
    edited September 2008
    You never assign the nextNode pointer to anything so when you attempt to do the assignment nextNode->fileName = "f2"; your doing it to a random block of memory that your pointer happens to point to.

    khain on
  • AngelHedgieAngelHedgie Registered User regular
    edited September 2008
    Basically, while you're creating the pointer for the node, you're never initializing it, so when you try to push data to it, the node has no clue where to put it.

    AngelHedgie on
    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum / Steam: noxaeternum
  • lizard eats flieslizard eats flies Registered User regular
    edited September 2008
    yup, what khain said... need to have nextNode point to something real.

    now as to why it only seg faults when the area in red is not commented.
    c++ lets you point anything to anywhere, and doesnt really care. It will only seg fault if you are pointing to some segment of memory that is outside of your program. So all you can really say about the code is that it will give you unexpected results. My guess is since you are instantiating a bunch of memory in those strings and char arrays, its causing the nextNode pointer to point to something outside of your programs memory space.
    In otherwords you just happen to be getting lucky when you dont have that block in there, but its still an error.

    lizard eats flies on
  • Bob SappBob Sapp Registered User regular
    edited September 2008
    Since Node is a structure I wouldn't have nextNode be a pointer at all. I mean, you could because structures are equivalent to classes in C++, but I wouldn't recommend it. This is how I would do it (spoilered if you want to do it yourself without help):
    Node nextNode; // new node to be added

    nextNode.fileName = "f2";
    nextNode.link = NULL;

    temp->link = &nextNode;

    Bob Sapp on
    fizzatar.jpg
  • khainkhain Registered User regular
    edited September 2008
    Bob Sapp wrote: »
    Since Node is a structure I wouldn't have nextNode be a pointer at all. I mean, you could because structures are equivalent to classes in C++, but I wouldn't recommend it. This is how I would do it (spoilered if you want to do it yourself without help):
    Node nextNode; // new node to be added

    nextNode.fileName = "f2";
    nextNode.link = NULL;

    temp->link = &nextNode;

    This doesn't work, or rather it will kind of work until you accidentally overwrite portions of your list since the memory was de-allocated when you exited the function. You need to either statically allocate or dynamically allocate memory for your node to use.

    khain on
  • Bob SappBob Sapp Registered User regular
    edited September 2008
    khain wrote: »
    Bob Sapp wrote: »
    Since Node is a structure I wouldn't have nextNode be a pointer at all. I mean, you could because structures are equivalent to classes in C++, but I wouldn't recommend it. This is how I would do it (spoilered if you want to do it yourself without help):
    Node nextNode; // new node to be added

    nextNode.fileName = "f2";
    nextNode.link = NULL;

    temp->link = &nextNode;

    This doesn't work, or rather it will kind of work until you accidentally overwrite portions of your list since the memory was de-allocated when you exited the function. You need to either statically allocate or dynamically allocate memory for your node to use.

    Whoops. I don't know what I was thinking there, must have been thinking in Java.

    Bob Sapp on
    fizzatar.jpg
Sign In or Register to comment.