Thread: Need help with segmentation fault

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    47

    Need help with segmentation fault

    Hello,

    The following code results in a segmentation fault when line 36 is executed
    Code:
    strcpy(node->summary_record->c_detail1, summary_record->c_detail1);
    The malloc at line 24 is successful
    Code:
    summary_t *node = malloc(sizeof(summary_t) * 1);
    and lines 34 and 35 are also successful
    Code:
    node->prev = NULL;
    node->next = NULL
    I'm guessing that the issue must be this line
    Code:
    summary_t *node = malloc(sizeof(summary_t) * 1);
    And that the
    Code:
    sizeof(summary_t)
    is not allocating the memory for the
    Code:
    summary_record_t
    .

    But if I'm correct, I don't know why this is happening or how to resolve?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct record
    {
        char        c_detail1[15];
        char        c_detail2[100];
        float       f_value1;
        float       f_value2;
    } summary_record_t;
    
    typedef struct summary
    {
        summary_record_t  *summary_record;
        struct summary_t  *prev;
        struct summary_t  *next;
    } summary_t;
    
    summary_t *add_at(int pos, summary_t *summary_list, summary_record_t *summary_record)
    {
        summary_t *node = malloc(sizeof(summary_t) * 1);
        if (node == NULL)
        {
            printf("malloc failed\n");
        }
        else
        {
            printf("malloc succeeded\n");
        }
    
        node->prev = NULL;
        node->next = NULL;
        strcpy(node->summary_record->c_detail1, summary_record->c_detail1);
        strcpy(node->summary_record->c_detail2, summary_record->c_detail2);
        node->summary_record->f_value1 = summary_record->f_value1;
        node->summary_record->f_value2 = summary_record->f_value2;
    }
    
    summary_t *add_end(summary_t *summary_list, summary_record_t *summary_record)
    {
        add_at(-1, summary_list, summary_record);
        return;
    }
    
    int main()
    {
        summary_t *summary_list = malloc(sizeof(summary_t) * 1);
        summary_list = NULL;
    
        summary_record_t *summary_record = malloc(sizeof(summary_record_t) * 1);
    
        strcpy(summary_record->c_detail1,"some detail");
        strcpy(summary_record->c_detail2,"another detail");
        summary_record->f_value1 = 200;
        summary_record->f_value2 = 250;
        summary_list = add_end(summary_list,summary_record);
    
        return 0;
    }
    Thanks

    VW

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're ignoring things.
    Code:
    $ gcc -g -Wall foo.c
    foo.c: In function ‘add_end’:
    foo.c:43:5: warning: ‘return’ with no value, in function returning non-void
         return;
         ^
    foo.c: In function ‘add_at’:
    foo.c:38:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    47
    Hello Salem,

    I was trying to post an extract of the code that demonstrates the issue

    This example updated to address the warnings

    The issue remains

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct record
    {
        char        c_detail1[15];
        char        c_detail2[100];
        float       f_value1;
        float       f_value2;
    } summary_record_t;
    
    typedef struct summary
    {
        summary_record_t  *summary_record;
        struct summary_t  *prev;
        struct summary_t  *next;
    } summary_t;
    
    void add_at(int pos, summary_t *summary_list, summary_record_t *summary_record)
    {
        summary_t *node = malloc(sizeof(summary_t) * 1);
        if (node == NULL)
        {
            printf("Malloc failed in: add_at\n");
            getchar();
        }
        node->prev = NULL;
        node->next = NULL;
        strcpy(node->summary_record->c_detail1, summary_record->c_detail1);
        strcpy(node->summary_record->c_detail2, summary_record->c_detail2);
        node->summary_record->f_value1 = summary_record->f_value1;
        node->summary_record->f_value2 = summary_record->f_value2;
    
        return;
    }
    
    void add_end(summary_t *summary_list, summary_record_t *summary_record)
    {
        add_at(-1, summary_list, summary_record);
        return;
    }
    
    int main()
    {
        summary_t *summary_list = malloc(sizeof(summary_t) * 1);
        summary_list = NULL;
    
        summary_record_t *summary_record = malloc(sizeof(summary_record_t) * 1);
    
        strcpy(summary_record->c_detail1,"some detail");
        strcpy(summary_record->c_detail2,"another detail");
        summary_record->f_value1 = 200;
        summary_record->f_value2 = 250;
        add_end(summary_list,summary_record);
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2020
    Posts
    47
    Hello,

    I've figured out a work-around.

    I was right the original malloc statement was not allocating space for the "subsidiary" structure (summary_record)

    So the work-around was to add an additional malloc statement (see below)

    This doesn't seem right to me though. The record structure is a part of the summary structure and I still don't understand why the initial malloc statement is behaving as it is.

    If someone can shed light on that, would be appreciated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct record
    {
        char        c_detail1[15];
        char        c_detail2[100];
        float       f_value1;
        float       f_value2;
    } summary_record_t;
    
    typedef struct summary
    {
        summary_record_t  *summary_record;
        struct summary_t  *prev;
        struct summary_t  *next;
    } summary_t;
    
    void add_at(int pos, summary_t *summary_list, summary_record_t *summary_record)
    {
        summary_t *node = malloc(sizeof(summary_t) * 1);
        if (node == NULL)
        {
            printf("Malloc failed in: add_at\n");
            getchar();
        }
        node->summary_record = malloc(sizeof(summary_record_t) * 1);
        if (node->summary_record == NULL)
        {
            printf("Malloc failed in: add_at: node->summary_record\n");
            getchar();
        }
        node->prev = NULL;
        node->next = NULL;
        strcpy(node->summary_record->c_detail1, summary_record->c_detail1);
        strcpy(node->summary_record->c_detail2, summary_record->c_detail2);
        node->summary_record->f_value1 = summary_record->f_value1;
        node->summary_record->f_value2 = summary_record->f_value2;
    
        printf("summary_record->c_detail1: %s\n",node->summary_record->c_detail1);
        return;
    }
    
    void add_end(summary_t *summary_list, summary_record_t *summary_record)
    {
        add_at(-1, summary_list, summary_record);
        return;
    }
    
    int main()
    {
        summary_t *summary_list = malloc(sizeof(summary_t) * 1);
        summary_list = NULL;
    
        summary_record_t *summary_record = malloc(sizeof(summary_record_t) * 1);
    
        strcpy(summary_record->c_detail1,"some detail");
        strcpy(summary_record->c_detail2,"another detail");
        summary_record->f_value1 = 200;
        summary_record->f_value2 = 250;
        add_end(summary_list,summary_record);
    
        return 0;
    }
    Thanks
    VW

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be aware that this:
    Code:
    summary_t *summary_list = malloc(sizeof(summary_t) * 1);
    summary_list = NULL;
    is equivalent to:
    Code:
    summary_t *summary_list = NULL;
    except that your code also has a memory leak.

    Quote Originally Posted by VeeDub
    This doesn't seem right to me though. The record structure is a part of the summary structure and I still don't understand why the initial malloc statement is behaving as it is.
    You are mistaken: the record structure is not part of the summary structure. Rather, a pointer to the record structure is part of the summary structure. That's why you need to either allocate memory to copy over the record structure, or assign the pointer to assume ownership of the existing record structure.
    Last edited by laserlight; 08-08-2020 at 01:17 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You seem to have forgotten most of what you learnt.
    Error: assignment of i_value in read-only object

    Study post #21 in detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault
    By browser in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 03:13 PM
  3. Segmentation Fault
    By ammalik in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2006, 05:06 PM
  4. Segmentation Fault??
    By SteveP4444 in forum C Programming
    Replies: 5
    Last Post: 04-01-2006, 02:24 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread