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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-09-2011, 09:36 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2011
Posts: 2
Rep:
|
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;
}
|
|
|
|
02-09-2011, 10:30 PM
|
#2
|
|
Member
Registered: Sep 2009
Location: Chennai
Distribution: Ubuntu 11.10 and back track 4
Posts: 45
Rep:
|
Have you tried to examine the code using gdb (GNU debugger).
Look into it, debuggers are must for a programmer.
hope it helps
|
|
|
|
02-09-2011, 10:36 PM
|
#3
|
|
Member
Registered: Sep 2009
Location: Chennai
Distribution: Ubuntu 11.10 and back track 4
Posts: 45
Rep:
|
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.
|
02-09-2011, 10:50 PM
|
#4
|
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,946
|
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.
|
02-10-2011, 03:00 AM
|
#5
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
|
02-10-2011, 10:46 PM
|
#6
|
|
LQ Newbie
Registered: Feb 2011
Posts: 2
Original Poster
Rep:
|
Thank you guys it really helped , @PTrenholme yeah you were exactly right about the bug. Thanks once again.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:37 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|