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 8035003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:11:02+00:00 2026-06-05T02:11:02+00:00

I just wrote a simple linked list, however when iterating through the list via

  • 0

I just wrote a simple linked list, however when iterating through the list via add() and display() the program seg faults.

#include <stdlib.h>
#include <stdio.h>

typedef struct entry {
    void *value;
    struct entry *next;
} entry;

typedef struct list {
    entry *items;
} list;

list *create(void) {
    list *l;

    l = malloc (sizeof(list));
    l->items = malloc(sizeof(entry*));
    l->items->next = NULL;

    return l;
}

void add(list *l, void *value) {
    entry *temp, *last, *new;

    for (temp = l->items; temp != NULL; temp = temp->next) {
        last = temp;
    }

    new = malloc(sizeof(*new));

    new->value = value;
    new->next = NULL;

    last->next = new;
}

void display(list *l) {
    entry *temp;

    for (temp = l->items; temp != NULL; temp = temp->next) {
        printf("%s\n", temp->value);
    }
}

int main(void) {
    list *l = create();

    add(l, "item1");
    add(l, "item2");
    add(l, "item3");
    add(l, "item4");

    display(l);

    return 0;
}

I have tested the code on a few machines and it works on a few and doesn’t on others. I’m clueless about the source of the error.

  • 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-05T02:11:04+00:00Added an answer on June 5, 2026 at 2:11 am

    In addition to the wrong size passed to malloc that FatalError mentioned, you allocate memory for l->items

    list *create(void) {
        list *l;
    
        l = malloc (sizeof(list));
        l->items = malloc(sizeof(entry*));
        l->items->next = NULL;
    
        return l;
    }
    

    but don’t ever set l->items->value to anything, so it’s an uninitialised pointer and when you try to dereference it when printing

    void display(list *l) {
        entry *temp;
    
        for (temp = l->items; temp != NULL; temp = temp->next) {
            printf("%s\n", temp->value);
        }
    }
    

    in the first iteration of the loop, that can easily lead to a segfault even if the insufficient allocation size didn’t cause one before.

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

Sidebar

Related Questions

I just wrote my first OpenMP program that parallelizes a simple for loop. I
I just wrote a simple C++ program in Visual Studio 2010 and I use
HI i wrote a simple block collecting program that was working just fine and
I just wrote a program to calculate employee's pay rates. To me, the program
I'm trying to write a simple geometry shader what just passes through vertices before
I just wrote a simple code where I want a textfield and a button
I just wrote a very simple Expect script for wrapping around rsync, but it
I just wrote a simple extension that has permission to *.google.com and changes some
I'm trying to write a simple program in VC++ which will just initialize the
I just wrote a simple DirectShow Filter (which inherits from CTransformFilter). But I want

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.