Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Segmentation fault when trying to print out a linked list
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="gravity black" data-source="post: 1312866" data-attributes="member: 219087"><p>Hey everyone, this is my first time posting, so hopefully i won't commit some kind of egregious faux pas. </p><p></p><p>Anyways, I'm trying to create and read back a simple linked list in C++. So far, I think I've built it and filled it with 10 different arrays of characters of about 22 characters each. (They are simple questions written in english.) My program compiles fine, and it runs, but only to a certain point. When it gets to the following line:</p><p></p><p></p><p>[CODE]</p><p>cout << current->info << " "<<endl; //outputs the 'info' component of the </p><p> //current node</p><p>[/CODE]</p><p></p><p>...it stops and says "Segmentation fault". I'm not sure what it could be, though I'm new at this so it may be obvious. Previous to this line, I have the following lines:</p><p></p><p>[CODE]</p><p>head = buildlist(); //calls a function to build the list, and sets head</p><p> //equal to the first node. (i think)</p><p>current = head; //sets current equal to head.</p><p>[/CODE]</p><p></p><p>The other, lesser problem that is probably the cause of this is that when I print out the various 'link' values as they're built, they have the following values:</p><p></p><p>first = 0x100100080</p><p>first->link = 0x10010090</p><p>first->info = This is statement 1.</p><p>second = 0x10010090</p><p>second->link = 0x1001000a0</p><p>second->info = This is statement 2.</p><p>third = 0x1001000a0</p><p>third->link = 0x1001000b0</p><p>third->info = This is statement 3.</p><p>.</p><p>.</p><p>.</p><p>last = 0x100100110</p><p>last->link = 0</p><p>last->info = This is statement 10.</p><p></p><p></p><p>...Now all this seems fine and good, but after this is done and I set 'head' equal to the function, and then ask it to print out 'head', it gives me this:</p><p></p><p>head= 0x7fff70464b00</p><p>head->link= 0x7fff704614a0</p><p>head->info = ?핂?</p><p></p><p>...The info here is really very weird, and it's obviously not equivalent to the array of characters "This is statement 1." I feel that this might be because I'm setting the 'head' wrong. I want it to be equal to the first node of the list, but I'm not sure if setting it equal to the function call that builds the entire list is the way to do that.</p><p></p><p>The way I have the program written, it's supposed to iterate over each item in the list and print out the info in each. (10 items in all). It [should] do this by first setting 'current = head', and then after each iteration setting 'current = current->link'.</p><p></p><p>For some reason, though, it only iterates two complete times, giving me garbled output each time, and then on the third time, it gives me a Segmentation fault instead of printing the info. It looks something like this:</p><p></p><p>now, we're inside the final while loop</p><p>current= 0x7fff70464b00</p><p>current->link= 0x7fff704614a0</p><p>current->info= ?핂?</p><p></p><p>current= 0x7fff704614a0</p><p>current->link= 0x7fff8295ed7e</p><p>current->info= L?Iz??</p><p></p><p>current= 0x7fff8295ed7e</p><p>current->link= 0xe589485500000000</p><p>Segmentation fault</p><p></p><p>... So anyone have any ideas? I apologize if this is too much information, but I wanted to be as thorough as possible without simply posting my code, (it's for a school assignment, and I wanted to keep everything on the "up and up"). That being said, if there's other information anyone needs to make a guess at what's going on, please just let me know.</p><p></p><p>Also, I'm running this on a 2008 intel-based Macbook, OS X Snow Leopard (Ver. 10.6.8)</p></blockquote><p></p>
[QUOTE="gravity black, post: 1312866, member: 219087"] Hey everyone, this is my first time posting, so hopefully i won't commit some kind of egregious faux pas. Anyways, I'm trying to create and read back a simple linked list in C++. So far, I think I've built it and filled it with 10 different arrays of characters of about 22 characters each. (They are simple questions written in english.) My program compiles fine, and it runs, but only to a certain point. When it gets to the following line: [CODE] cout << current->info << " "<<endl; //outputs the 'info' component of the //current node [/CODE] ...it stops and says "Segmentation fault". I'm not sure what it could be, though I'm new at this so it may be obvious. Previous to this line, I have the following lines: [CODE] head = buildlist(); //calls a function to build the list, and sets head //equal to the first node. (i think) current = head; //sets current equal to head. [/CODE] The other, lesser problem that is probably the cause of this is that when I print out the various 'link' values as they're built, they have the following values: first = 0x100100080 first->link = 0x10010090 first->info = This is statement 1. second = 0x10010090 second->link = 0x1001000a0 second->info = This is statement 2. third = 0x1001000a0 third->link = 0x1001000b0 third->info = This is statement 3. . . . last = 0x100100110 last->link = 0 last->info = This is statement 10. ...Now all this seems fine and good, but after this is done and I set 'head' equal to the function, and then ask it to print out 'head', it gives me this: head= 0x7fff70464b00 head->link= 0x7fff704614a0 head->info = ?핂? ...The info here is really very weird, and it's obviously not equivalent to the array of characters "This is statement 1." I feel that this might be because I'm setting the 'head' wrong. I want it to be equal to the first node of the list, but I'm not sure if setting it equal to the function call that builds the entire list is the way to do that. The way I have the program written, it's supposed to iterate over each item in the list and print out the info in each. (10 items in all). It [should] do this by first setting 'current = head', and then after each iteration setting 'current = current->link'. For some reason, though, it only iterates two complete times, giving me garbled output each time, and then on the third time, it gives me a Segmentation fault instead of printing the info. It looks something like this: now, we're inside the final while loop current= 0x7fff70464b00 current->link= 0x7fff704614a0 current->info= ?핂? current= 0x7fff704614a0 current->link= 0x7fff8295ed7e current->info= L?Iz?? current= 0x7fff8295ed7e current->link= 0xe589485500000000 Segmentation fault ... So anyone have any ideas? I apologize if this is too much information, but I wanted to be as thorough as possible without simply posting my code, (it's for a school assignment, and I wanted to keep everything on the "up and up"). That being said, if there's other information anyone needs to make a guess at what's going on, please just let me know. Also, I'm running this on a 2008 intel-based Macbook, OS X Snow Leopard (Ver. 10.6.8) [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Segmentation fault when trying to print out a linked list
Top