LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-09-2011, 10:36 PM   #1
radhikamlk
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Segmentation fault in C program


I am getting this error "segmentation fault " when I am trying to run it , I don't know what is wrong with the code. This is the program -

#include <stdio.h>

typedef struct node
{
int val;
struct node* next;
} node;

int ll_equal(const node* a, const node* b)
{
while (a != NULL)
{
if (a->val != b->val)
return 0;
a = a->next;
b = b->next;
}
/* lists are equal if a and b are both null */
return a == b;
}

int main(int argc, char** argv)
{
int i;
node nodes[10];

for (i=0; i<10; i++)
{
nodes[i].val = 0;
nodes[i].next = NULL;
}

nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[3];

printf("equal test 1 result = %d\n", ll_equal(&nodes[0], &nodes[0]));
printf("equal test 2 result = %d\n", ll_equal(&nodes[0], &nodes[2]));

return 0;
}
 
Old 02-09-2011, 11:30 PM   #2
retxed
Member
 
Registered: Sep 2009
Location: Chennai
Distribution: Ubuntu 11.10 and back track 4
Posts: 45

Rep: Reputation: 9
Have you tried to examine the code using gdb (GNU debugger).
Look into it, debuggers are must for a programmer.

hope it helps
 
Old 02-09-2011, 11:36 PM   #3
retxed
Member
 
Registered: Sep 2009
Location: Chennai
Distribution: Ubuntu 11.10 and back track 4
Posts: 45

Rep: Reputation: 9
I think the problem is in the if conditional in the II_equal() funtion.

Code:
Breakpoint 1, ll_equal (a=0xbfd47ad0, b=0x0) at dd.c:13
13      if (a->val != b->val)
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x080483d4 in ll_equal (a=0xbfd47ad0, b=0x0) at dd.c:13
13      if (a->val != b->val)
(gdb) print a->val
$1 = 0
(gdb) print b->val
Cannot access memory at address 0x0
(gdb)
Note:: the last line shows the reason for segmentation fault.
 
1 members found this post helpful.
Old 02-09-2011, 11:50 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
And that problem is, I think, because your code assumes that b is not null if a is not null.
 
1 members found this post helpful.
Old 02-10-2011, 04:00 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-10-2011, 11:46 PM   #6
radhikamlk
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you guys it really helped , @PTrenholme yeah you were exactly right about the bug. Thanks once again.
 
  


Reply

Tags
segfault, [c]


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is my program giving me segmentation fault? Ahmad Mujtaba Programming 5 12-25-2010 08:33 AM
Getting Segmentation Fault when im trying to start this program :/ Fronix Linux - Server 5 12-30-2008 01:34 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
C++ Program, Segmentation Fault Fireball7 Programming 6 12-07-2005 05:22 PM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 01:47 PM


All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration