Faris Durrani
Faris Durrani

Reputation: 37

Why is there a segmentation fault in strcpy after initializing a struct?

I can't seem to figure out why strcpy makes a segmentation fault in this code. It should be straightforward:

    typedef struct message {
        char *buffer;
        int length;
    } message_t;

    int main () {
        char* buf = "The use of COBOL cripples the mind; its "
                    "teaching should, therefore, be regarded as a criminal "
                    "offense. -- Edsgar Dijkstra";
        message_t messageA = {"", 130};
        message_t *message = &messageA;
        strcpy(message->buffer, "buf");
        printf("Hello\n");
    }

EDIT: strcpy(message->buffer, "buf") is supposed to be strcpy(message->buffer, buf) without the "" quotes

EDIT: Thank you to the comments! This has been resolved by malloc'ing message->buffer to make space for buf:

    message_t messageA = {"", 130};
    message_t *message = &messageA;
    message->buffer = malloc(122);
    strcpy(message->buffer, buf);
    printf("Hello\n");

Upvotes: 0

Views: 63

Answers (3)

Vercingatorix
Vercingatorix

Reputation: 1884

Try using message->buffer = strdup(buf); that does the malloc and strlen computation for you.

Upvotes: 0

Krishna Kanth Yenumula
Krishna Kanth Yenumula

Reputation: 2567

message_t messageA = {"", 130};

Here, you initializing messageA.buffer = "". It is a string literal. So, you cannot modify the string stored in it. If you try to modify it, you will get segmentation fault.

message_t *message = &messageA;
strcpy(message->buffer, buf);

Here, you are modifying the string literal message->buffer. That's why you got segmentation fault.
Please visit this question Modifying a string literal

Upvotes: 1

csavvy
csavvy

Reputation: 821

some points to be noted here.

when you declare pointers to store data you either assign directly at the declaration (usually used for small strings not big strings) or you should allocate memory using dynamic memory allocation functions

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

typedef struct message {
    char *buffer;
    int length;
} message_t;

int main () {
    char* buf = "The use of COBOL cripples the mind; its "
                "teaching should, therefore, be regarded as a criminal "
                "offense. -- Edsgar Dijkstra";
    message_t messageA = {"", 130};
    message_t *message = &messageA;

    //allocate memory to buffer length of buf, + 1 for \0 character
    message->buffer = malloc( strlen(buf) + 1 );
    // check allocated memory is success or not
    if ( message->buffer )
    {
        strcpy(message->buffer, buf);
        printf("buffer = %s\n",message->buffer );
        //free the malloc'ed memory to avoid memory leaks
        free(message->buffer);
        //make the pointer NULL, to avoid dangling pointer
        message->buffer = NULL;
    }
    else
    {
        printf("malloc failed\n");
    }
    printf("Hello\n");
    return 0;
}

Upvotes: 2

Related Questions