Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:58:22+00:00 2026-06-12T03:58:22+00:00

typedef struct { uint32_t field_id; uint16_t length; }entry_t; struct node { entry_t *sp_entry; struct

  • 0
typedef struct 
{
     uint32_t field_id;
     uint16_t length;
}entry_t;


struct node
{
        entry_t *sp_entry;
        struct node *next;
}*head;

I have a function called add() to add entry to the linked list.

void add( entry_t *entry )
{
        struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->sp_entry = entry;
        if (head== NULL)
        {
                head=temp;
                head->next=NULL;
        }
        else
        {
                temp->next=head;
                head=temp;
        }
}

Please not that the “value” that is stored in the linked list node is itself a pointer to a structure.I am getting a segmentation fault at

temp->sp_entry = entry;

which is probably because I am not allocation memory for entry_t structure.What I want to know is that is this a usual use case? If yes how do I do it.Do I have to do

temp->sp_entry = malloc(sizeof (entry_t));

before making an assignment? Also is there a more elegant way to achieve this?

Additional information.

when I run gdb I get

p *temp
$3 = {sp_entry = 0x0, next = 0x3e64656269}      

the sp_entry looks like it is a null pointer.This is printed after malloc in add() function.And also my code has been combined with “-g -O0 -Wall”.There has been no warnings

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:58:23+00:00Added an answer on June 12, 2026 at 3:58 am

    Your code looks okay. temp->sp_entry = entry; should not segfault since you allocated temp on the previous line.

    Pointer errors can be insidious. The line that crashes isn’t necessarily the line at fault. add() looks correct, so I suspect there’s an error earlier in your program’s execution that doesn’t cause your program to crash immediately.

    When I run gdb I get:

    p *temp
    $3 = {sp_entry = 0x0, next = 0x3e64656269}      
    

    The sp_entry looks like it is a null pointer.

    Not a problem. You haven’t initialized temp->sp_entry or temp->next yet so their values are meaningless.

    What’s important is temp‘s value. It seems to be a valid pointer since gdb can print *temp. This is really where the segfault is occurring? I would have expected gdb to complain about temp being a pointer to an invalid memory location and refuse to print *temp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a structure as shown below. typedef struct { attribute_code_t field_id; uint8_t instance_num;
I have a structure: typedef struct stock { const char* key1p2; // stock code
I have a struct typedef struct { int id; string name; string address; string
I have a structure typedef struct store { char name[11]; int age; } store;
I have a C struct like this: typedef struct { my_domain_type_t type; my_domain_union_t u;
I have a to deal with a structure of a lib : typedef struct
typedef struct child_list {int count; char vo[100]; child_list*next;} child_list; typedef struct parent_list { char
I have a C structure like this : typedef struct ip_esp_private { /* keep
I saw some C code like this: // A: typedef uint32_t in_addr_t; struct in_addr

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.