New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

Start a Discussion

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Segmentation fault when trying to print out a linked list

Post Reply New Thread Subscribe

Thread Tools
gravity black

Member Since: Oct 19, 2011
Posts: 3
gravity black is on a distinguished road

gravity black is offline
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:

cout << current->info << " "<<endl; //outputs the 'info' component of the 
                                                    //current node 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:

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

xstep's Avatar
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
I see you got it resolved over at Macrumors

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
gravity black

Member Since: Oct 19, 2011
Posts: 3
gravity black is on a distinguished road

gravity black is offline
QUOTE Thanks

Post Reply New Thread Subscribe

« mark message as unread applescript | How to install gfortran on OSX 10.7 (Lion) »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Starter
Last Post
Help - cannot print after upgrade to 10.5 (usb hp laserjet 1012) giulio OS X - Operating System 6 03-02-2012 11:08 AM
Printing Problem For To Do List In Apple Mail sand2347 Switcher Hangout 2 06-27-2010 11:00 AM
To do programs Albert Reeder Switcher Hangout 11 11-23-2008 07:48 PM
Slow Print Dialog Mikail OS X - Operating System 3 08-22-2008 12:36 PM
Trendnet Print Server Number6 Other Hardware and Peripherals 0 04-07-2007 10:11 AM

All times are GMT -4. The time now is 10:11 PM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.

Welcome to

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!

(4 digit year)

Already a member?