473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

segmentation fault

I have a C program which creates a doubly linked list, by having nodes store a single int as data, a pointer to the previous node, and a pointer to the next node. The problem is that when I run the program I receive a segmentation fault when I try to print the data in the head node. Through debugging I have noticed that the head node does not stay constant, it advances itself to the next node as new nodes are added.

Here is the function in which I add a new node to the end of the list.

Expand|Select|Wrap|Line Numbers
  1. void addnode(item *curr, item *head, item *tail)
  2. {
  3.     if(head == NULL)
  4.     {
  5.         curr->prev = NULL;
  6.         head = curr;
  7.     }//end if
  8.     else
  9.     {
  10.         tail->next = curr;
  11.         curr->prev = tail;
  12.     }//end else
  13.     curr->next = NULL; //set the current nodes  next to NULL
  14.     tail = curr; //new tail is the current node
  15.     printf("head: %d\n", head->val);
  16.     printf("curr: %d\n", curr->val);
  17. }//end addnode
Any suggestions?
Oct 28 '07 #1
2 1054
Ganon11
3,652 Expert 2GB
When do you initialize head, tail, or curr? Outside the function? In particular, I don't think tail is being properly allocated, as you assume that any list with a head present already has a tail - yet you also assume that, if no head is present, you only need to set the head.

Basically, for a list of 1 element, that element is both the head and the tail. Your code should reflect this.
Oct 28 '07 #2
mattmao
121 100+
Hi.

Keep it simple man. Your could divide this problem into two steps. First, create a head sentinel as well as a tail sentinel, two nodes that would always be there. The second step would be just inserting nodes in the middle, which is some sort like this: break the chain, add a new node, relink the chain.

I found it is a good approach to this adding a node functionality, anyway, you don't need to consider the different cases anymore, even there is no node before, you just "insert" your first node between the head and the tail sentinels.
Oct 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.