简体   繁体   English

为什么初始化结构后strcpy中存在分段错误?

[英]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.我似乎无法弄清楚为什么strcpy在这段代码中会出现分段错误。 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编辑: strcpy(message->buffer, "buf")应该是strcpy(message->buffer, buf)没有 "" 引号

EDIT: Thank you to the comments: This has been resolved by malloc'ing message->buffer to make space for buf:编辑:感谢您的评论:这已通过 malloc'ing message->buffer 为 buf 腾出空间来解决:

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

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当您声明存储数据的指针时,您可以直接在声明中分配(通常用于小字符串而不是大字符串),或者您应该使用动态 memory 分配函数分配 memory

#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;
}
message_t messageA = {"", 130};

Here, you initializing messageA.buffer = "" .在这里,您初始化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 .在这里,您正在修改字符串文字message->buffer That's why you got segmentation fault.这就是您遇到分段错误的原因。
Please visit this question Modifying a string literal请访问此问题Modifying a string literal

Try using message->buffer = strdup(buf) ;尝试使用message->buffer = strdup(buf) ; that does the malloc and strlen computation for you.为您执行mallocstrlen计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM