像这样使用 malloc 有什么问题吗?

发布于 2024-11-11 08:29:31 字数 1228 浏览 7 评论 0原文

我遇到以下代码的分段错误,有人可以帮助我理解为什么吗?

typedef struct ClientData {
        int _clientId;
        char _msg[200];
    } ClientData_t;

// in a function
char *id = malloc(50);
char *msg = malloc(sizeof(MESSAGE_LENGTH));
memset(id, 0, 50);
memset(msg, 0, MESSAGE_LENGTH);
strcpy(id, &(buffer[1]));
strcpy(msg, &(buffer[50]));
free(id);
printf("this message can be printed\n");
ClientData_t *newData = malloc(sizeof(ClientData_t));
// I got segmentation fault for this malloc here

第二次,我从上面删除了 free(id); 调用,并保留了其余部分,调用最后一个 malloc 后,我收到以下错误:

mainClient1: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Abort

最后,在更改前两个后,一切正常函数中的行:

char id[50];
char msg[MESSAGE_LENGTH];

这是为什么?什么可能导致断言失败?谢谢。

I got segmentation fault for the following code, could someone help me understand why?

typedef struct ClientData {
        int _clientId;
        char _msg[200];
    } ClientData_t;

// in a function
char *id = malloc(50);
char *msg = malloc(sizeof(MESSAGE_LENGTH));
memset(id, 0, 50);
memset(msg, 0, MESSAGE_LENGTH);
strcpy(id, &(buffer[1]));
strcpy(msg, &(buffer[50]));
free(id);
printf("this message can be printed\n");
ClientData_t *newData = malloc(sizeof(ClientData_t));
// I got segmentation fault for this malloc here

The second time, I removed free(id); call from above, and kept the rest, I got the following error once the last malloc is called:

mainClient1: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Abort

and finally, everything worked after I changed the first two lines in the function to:

char id[50];
char msg[MESSAGE_LENGTH];

Why is this? what could cause the assertion fail? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘世孤行 2024-11-18 08:29:31

如果 MESSAGE_LENGTH 是整数,则 sizeof( MESSAGE_LENGTH ) 与 MESSAGE_LENGTH 有很大不同。 (可能是 4 或 8。)您需要 malloc( MESSAGE_LENGTH ),而不是 malloc( sizeof( MESSAGE_LENGTH ))。

If MESSAGE_LENGTH is an integer, then sizeof( MESSAGE_LENGTH ) is very different from MESSAGE_LENGTH. (It is likely 4 or 8.) You want malloc( MESSAGE_LENGTH ), not malloc( sizeof( MESSAGE_LENGTH )).

奢欲 2024-11-18 08:29:31

char *msg = malloc(sizeof(MESSAGE_LENGTH));

可能没有按照您的想法进行。我假设 MESSAGE_LENGTH 是一些 #define,如果是这样,那么您可能会得到 sizeof(int) 左右,而不是分配 MESSAGE_LENGTH 字节块。

char *msg = malloc(sizeof(MESSAGE_LENGTH));

Is probably not doing what you're thinking. I'm assuming MESSAGE_LENGTH is some #define, and if so, then it's likely you're getting the sizeof(int) or so, rather than allocating a block of MESSAGE_LENGTH bytes.

空心空情空意 2024-11-18 08:29:31

某物的大小不是它的值:

pax$ cat qq.c
    #include <stdio.h>
    #define MSGLEN 50
    int main (void) {
        printf ("sizeof(MSGLEN) = %d\n", sizeof(MSGLEN));
        printf ("       MSGLEN  = %d\n", MSGLEN);
        return 0;
    }

pax$ gcc -o qq qq.c

pax$ ./qq
sizeof(MSGLEN) = 4
       MSGLEN  = 50

如果您需要 50 个字节,请使用 MSG_LEN,而不是它的大小。代码:

#define MESSAGE_LENGTH 50
char *msg = malloc(sizeof(MESSAGE_LENGTH));
memset(msg, 0, MESSAGE_LENGTH);

将分配四个字节(假设MESSAGE_LENGTH实际上计算为整数(在具有四字节整数的系统上(标准不强制这样做))),但尝试填充< /em> 五十个字节,不是一个好主意。

The size of something is not its value:

pax$ cat qq.c
    #include <stdio.h>
    #define MSGLEN 50
    int main (void) {
        printf ("sizeof(MSGLEN) = %d\n", sizeof(MSGLEN));
        printf ("       MSGLEN  = %d\n", MSGLEN);
        return 0;
    }

pax$ gcc -o qq qq.c

pax$ ./qq
sizeof(MSGLEN) = 4
       MSGLEN  = 50

If you want fifty bytes, use MSG_LEN, not its size. The code:

#define MESSAGE_LENGTH 50
char *msg = malloc(sizeof(MESSAGE_LENGTH));
memset(msg, 0, MESSAGE_LENGTH);

will allocate four bytes (assuming MESSAGE_LENGTH actually evaluates as an integer (on a system with four-byte integers (the standard doesn't mandate this))) but try to fill fifty bytes, not a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文