Click here to Skip to main content
15,398,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried to debug my code using gdb i am getting error saying
Thread 1 received signal SIGSEGV, Segmentation fault

What I have tried:

here is my code
typedef struct list<br />
{<br />
    node *head;<br />
    node *tail;<br />
    int number_of_nodes;<br />
} List;<br />
<br />
typedef struct queue<br />
{<br />
    List *ptr_list;<br />
} Queue;<br />
<br />
void queue_initialize(Queue *queue_list)<br />
{<br />
//TODO<br />
    printf("hello inside queue_initialize\n");  <br />
    list_initialize(queue_list->ptr_list);<br />
    printf("hello after queue_initialize\n");<br />
    return;<br />
    }<br />
<br />
void list_initialize(List *ptr_list)<br />
{<br />
//TODO<br />
    printf("hello in list_initialize\n");<br />
    ptr_list->head=0;<br />
    printf("hello\n");<br />
    ptr_list->tail=0;<br />
    printf("hello\n");<br />
    ptr_list->number_of_nodes=0;<br />
    printf("hello after list_initialize\n");<br />
    <br />

when i call the function queue_initialize(Queue *queue_list) i get this output

hello before queue_initialize<br />
hello inside queue_initialize<br />
hello in list_initialize

but the expected output is

hello before queue_initialize<br />
hello inside queue_initialize<br />
hello in list_initialize<br />
hello <br />
hello<br />
hello after list_initialize

can anyone tell me whats wrong with this code? your effort is really appriciated please help me fast

i am pretty sure that error is here in these lines

ptr_list->head=0;<br />
ptr_list->tail=0;<br />
Updated 1-Oct-20 0:18am
KarstenK 1-Oct-20 4:04am
plz reformat the text it looks like copied from some HTML :-(

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

On Unix-like operating systems, a signal called SIGSEGV is sent to a process that accesses an invalid memory address.

For some details on Segmentation Fault[^].

Your case, most likely, your ptr_list does not have a valid value. Your queue setup looks incorrect and I am not sure if how is your Node defined (should be defined if you are getting some output).

In C, it is not the correct setup - you need to add struct keyword before the pointers for connecting them:
struct Node { 
    int data; 
    struct Node* next; 

Would suggest to read and learn about linked list.
Linked List Program in C - Tutorialspoint[^]
Linked List | Set 1 (Introduction) - GeeksforGeeks[^]
CPallini 1-Oct-20 6:18am
Sandeep Mewara 1-Oct-20 13:01pm
Thanks CPallini. :)
You should dynamically allocate memory for your objects. Try, as starting point:
#include <stdio.h>
#include <stdlib.h>
// I assume your definition of node is compatible with the following:
typedef struct node
  int value;
  struct node * next;
} Node;

typedef struct list
  Node *head;
  Node *tail;
  int number_of_nodes;
} List;

typedef struct queue
    List *ptr_list;
} Queue;

void list_initialize(List *ptr_list)
  printf("hello in list_initialize\n");
  printf("hello after list_initialize\n");
void queue_initialize(Queue *queue_list)
  queue_list->ptr_list = (List*) malloc(sizeof(List));
  // TODO: handle possible malloc failure
  printf("hello inside queue_initialize\n");
  printf("hello after queue_initialize\n");

int main()
  Queue queue;

  return 0;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900