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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:52:19+00:00 2026-06-02T13:52:19+00:00

I got a segfault with this code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include

  • 0

I got a segfault with this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>

typedef struct _node
{
    char *buffer;           
    struct _node *next;     
    int node_count;        
} node;

typedef struct _list
{
    node *head;
    node *tail;
} list;

list *node_list;

int list_node_lookup(list *l, char *buffer)
{
    node *temp = l->head;
    while(temp)
    {
        if (strcmp(temp->buffer, buffer) == 0)
        {
            /* We got it earlier */
            temp->node_count++;

            return 1;
        }
        else
        {
            temp = temp->next;
        }

    }

    return 0;
}

int adding_to_list(list *l, char *buffer)
{
    int ret;
    char *tmp = (char *)malloc(sizeof(char)*(strlen(buffer) + 1));
    node *new_node = (node *)malloc(sizeof(struct _node));

    /* Empty list */
    if (l->head == NULL)
    {
        strcpy(tmp, buffer);
        new_node->buffer = tmp;
        new_node->node_count = 0;
        l->head = l->tail = new_node;
        l->head->next = NULL;
    }
    else
    {
        /* The list is not empty */
        ret = list_node_lookup(l, buffer);
        if (ret == 1)
        {
            fprintf(stdout, "Got it before\n");
        }
        else
        {
            strcpy(tmp, buffer);
            new_node->buffer = tmp;
            new_node->node_count = 0;
            l->tail->next = new_node;
            l->tail = new_node;
            new_node->next = NULL;
            fprintf(stdout, "Adding this cust : %s\n", buffer);
        }
    }

    return 0;
}

int main(int argc, char **argv)
{
    FILE    *cust;
    char    buf[BUFSIZ];
    DIR* cust_dir;
    struct dirent* input;

    node_list = (list *) malloc(sizeof(struct _list));
    if (node_list == NULL)
    {
            return 1;
    }
    node_list->head = node_list->tail = NULL;

    if (NULL == (cust_dir = opendir("cust_dir"))) 
    {
        return 1;
    }
    while ((input = readdir(cust_dir))) 
    {
            if (!strcmp (input->d_name, "."))
                continue;
            if (!strcmp (input->d_name, ".."))  
                continue;

            cust = fopen(input->d_name, "r");

            while (fgets(buf, BUFSIZ, cust) != NULL)
            {
                adding_to_list(node_list, buf);
            }
                    fclose(cust);
    }

    return 0;
}

When i test my code with a directory that contain this two files(they contain empty lines), i got strange output and seg fault.

I use this file twice (customers.txt and customers_copy.txt) in the same directory:

1
2
3
4    Kristina   Chung   H   Kristina H. Chung   Chung, Kristina H.
5    Paige  Chen    H   Paige H. Chen   Chen, Paige H.
6    Sherri Melton  E   Sherri E. Melton    Melton, Sherri E.
7    Gretchen   Hill    I   Gretchen I. Hill    Hill, Gretchen I.
8    Karen  Puckett U   Karen U. Puckett    Puckett, Karen U.
9    Patrick    Song    O   Patrick O. Song Song, Patrick O.
10    Elsie Hamilton    A   Elsie A. Hamilton   Hamilton, Elsie A.
11   
12    Hazel Bender  E   Hazel E. Bender Bender, Hazel E.
13

The first three lines are empty (when i use one file all is ok, but this multiple files i got a segfault).

Thanks for your help, to understand what’s wrong.

  • 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-02T13:52:21+00:00Added an answer on June 2, 2026 at 1:52 pm

    When you add a node to the list (to the tail of the list), you don’t set the next pointer of that node to NULL. That means that the next time you do a lookup, you will call strcmp with invalid pointers. This in itself could cause a segmentation fault.

    The simple solution is to set next to NULL as soon as you allocate the node.

    Also, if the node is already in the list, you allocate tmp but never use it. That would leak memory. This is similar to the comment by Greg Brown – you are also leaking file descriptors and file handles (you never close cust).

    [EDIT]

    The problem is that you are reading the directory “cust_dir”, so you are getting names of files that are in the directory (e.g. “customers.txt”). You then open the file without adding “cust_dir/” to the file name.

    So if by accident you also have the file in the current directory, it works. But otherwise, cust is NULL (because input->d_name is the name of a file inside “cust_dir”, not in the current directory), and you then try to read data from a NULL file pointer. This causes a segmentation fault.

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

Sidebar

Related Questions

So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen(lr.txt, r); fseek(fp, 0L,
Got this errors keeps appeared repeatedly Here is my line 20 on functions.php $wp_query->max_num_pages
Got a real simple question but can't figure out the solution. Got following code.
I've been debugging a segfault in some numerical code (C++, no dependencies on any
Let's hope I can dumb this down without leaving out crucial details... I've got
I got this message: expected 'void **' but argument is of type 'char **'
So, I've got a function that loads up a char** variable with some string

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.