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.
Posts
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.
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.