Sponsored Content
Operating Systems OS X (Apple) Segmentation fault when reading out a linked list Post 302565868 by gravity black on Wednesday 19th of October 2011 03:02:33 AM
Old 10-19-2011
Question Segmentation fault when reading out a linked list

Hey everyone, this is my first time posting, so hopefully i won't commit some kind of egregious faux pas. Smilie

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

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

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)
 

10 More Discussions You Might Find Interesting

1. Programming

segmentation fault

sometimes for this code i get a segmentation fault for codes llike this : int main{ int * a= 0; int b; a = (int*)malloc(sizeof(int)); ///some code using these variable but no freeing of a if(a){ free(a); a = 0; } return... (3 Replies)
Discussion started by: wojtyla
3 Replies

2. Linux

Segmentation fault

Hi, on a linux Red HAT(with Oracle DB 9.2.0.7) I have following error : RMAN> delete obsolete; RMAN retention policy will be applied to the command RMAN retention policy is set to redundancy 2 using channel ORA_DISK_1 Segmentation fault What does it mean ? And the solution ? Many thanks. (0 Replies)
Discussion started by: big123456
0 Replies

3. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

4. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

5. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

6. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

7. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

8. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

9. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

10. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
All times are GMT -4. The time now is 08:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy