I am reading in values from a file, and am not sure what's wrong with my code? My main issue is within the area in bold, can anyone tell me what's wrong?
On the first iteration you set lastInt to anotherInt. lastInt->nextNode is presumably a null or invalid pointer. Then, rather than storing fileInt (which by the way the loop doesn't even touch, nor does it touch intInputFile) in lastInt->intValue, you move lastInt to the next node, which isn't guaranteed to exist, and assign to that the integer.
- anotherInt gets created. (line 21)
- intLast gets set to anotherInt (line 24)
- lastInt gets set to anotherInt (line 25)
- lastInt gets set to anotherInt->nextNode (line 31 -- PROBLEM)
This is a problem because you never initialized anotherInt->nextNode. Depending on what you're doing in intNode's constructor, you're either dereferencing a null pointer (bad), or dereferencing an uninitialized pointer (worse). Each have potential to crash.
It seems to me that line 31 should be inside the else clause.
Thanks. I'm confused as to how to fix it, I've tried intializing lastInt->nextNode to null but it still doesn't work. Can you explain how I would fix the problem?