473,382 Members | 1,369 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,382 software developers and data experts.

segFault in linked-list

Hi,

Could anyone please show me why the following segfault occurs
at line 54 in the following code?

Expand|Select|Wrap|Line Numbers
  1.      1  #include <stdio.h>
  2.      2  #include <stdlib.h>
  3.      3
  4.      4  void printHello()
  5.      5  {   printf("Hello ");  }
  6.      6
  7.      7  void printThere()
  8.      8  {   printf("There\n");  }
  9.      9
  10.     10  void (*FuncPtrArry[2])() = {&printThere,  &printHello};  // Init function pointer array
  11.     11
  12.     12  typedef struct mystruct
  13.     13  {
  14.     14     struct mystruct *next;
  15.     15     void (* funcPtr)();
  16.     16  } NodeStruct;
  17.     17
  18.     18  NodeStruct *topNode = NULL;
  19.     19  NodeStruct *lastNode = NULL;
  20.     20  int count = 2;
  21.     21
  22.     22  void buildlist(void)
  23.     23  {
  24.     24     for (/* count already init to 2 */ ;count > 0; count--)
  25.     25     {
  26.     26          if (topNode == (NodeStruct *)NULL)
  27.     27          {
  28.     28             topNode = (NodeStruct *)malloc(sizeof(NodeStruct));
  29.     29             lastNode = topNode;
  30.     30
  31.     31             if (lastNode == (NodeStruct *)NULL)
  32.     32                  return; /*ERROR */
  33.     33          }
  34.     34          else
  35.     35          {
  36.     36             lastNode -> next = (NodeStruct *)malloc(sizeof(NodeStruct));
  37.     37             if (lastNode -> next == NULL)
  38.     38                  return; /*ERROR */
  39.     39          }
  40.     40
  41.     41          lastNode = lastNode -> next;
  42.     42          lastNode -> funcPtr = FuncPtrArry[count - 1];
  43.     43          lastNode -> next = (NodeStruct *)NULL;
  44.     44     }  // end of for loop
  45.     45  }
  46.     46
  47.     47  int main()
  48.     48  {
  49.     49     buildlist();
  50.     50
  51.     51     while(topNode)
  52.     52     {
  53.     53          (*(topNode->funcPtr)) ();
  54.     54          topNode = topNode->next;
  55.     55     }
  56.     56  }
  57.  

Thanks in advance

Phil
Feb 6 '12 #1

✓ answered by Banfa

Line 42 you set lastNode = lastNode -> next; but lastNode -> next is only set in the else clause of the the if statement so on the first iteration when topNode is created this line causes an error.

Move line 42 into the else clause of the if statement.

3 1750
Output of debugging session:
------------------------------------------
Breakpoint 1, main () at linked_list_net.cc:51
51 while(topNode)
(gdb) n
53 (*(topNode->funcPtr)) ();
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb)
Feb 6 '12 #2
Banfa
9,065 Expert Mod 8TB
Line 42 you set lastNode = lastNode -> next; but lastNode -> next is only set in the else clause of the the if statement so on the first iteration when topNode is created this line causes an error.

Move line 42 into the else clause of the if statement.
Feb 6 '12 #3
@Banfa
Thank you, Banfa

P.
Feb 6 '12 #4

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

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
4
by: Jim Strathmeyer | last post by:
Under what circumstances would closing a istream object (such as 'in.close()') SEGFAULT?
1
by: David Douthitt | last post by:
I have a binary statically linked application that was compiled against Red Hat 7.3 (presumably glibc 2.2.5). The application SegFaults when run in a Red Hat Advanced Server environment - with...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
14
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.