gpt4 book ai didi

c - valgrind memcheck 分配字符串内存时出错

转载 作者:行者123 更新时间:2023-11-30 20:11:49 26 4
gpt4 key购买 nike

运行我的程序,出现此错误:

==3205== Invalid write of size 8
==3205== at 0x40167C: push (load.c:75)
==3205== by 0x401725: load_tetrimino (load.c:112)
==3205== by 0x40112F: main (main.c:130)
==3205== Address 0x544e718 is 24 bytes after a block of size 16 in arena "client"
==3205==

valgrind: m_mallocfree.c:303 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed. valgrind: Heap block lo/hi size mismatch: lo = 80, hi
= 88401728. This is probably caused by your program erroneously writing past the end of a heap block and corrupting heap metadata. If you fix any invalid writes reported by Memcheck, this assertion failure will probably go away. Please try that before reporting this as a bug.


host stacktrace:
==3205== at 0x38082F78: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x38083094: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x38083221: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x380909D4: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x3807C683: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x3807AF03: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x3807F13A: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x3807A49A: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x38057FEE: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==3205== by 0x802C8C764: ???
==3205== by 0x802BA9F2F: ???
==3205== by 0x8020082CF: ???
==3205== by 0x401674: push (load.c:75)
==3205== by 0x8020082CF: ???
==3205== by 0x1BFF: ???
==3205== by 0x38C4F9FF: ???
==3205== by 0x804C2BFFF: ???
==3205== by 0x12C05: ???

sched status: running_tid=1

Thread 1: status = VgTs_Runnable (lwpid 3205)
==3205== at 0x401684: push (load.c:75)
==3205== by 0x401725: load_tetrimino (load.c:112)
==3205== by 0x40112F: main (main.c:130)

这段代码:

int                             push(t_node **head, t_node **tail,                                       
struct dirent *file)
{
t_node *new;
if ((new = malloc(sizeof(t_node *))) == NULL)
return (1);
if ((new->tetrimino.name = malloc(12 + my_strlen(file->d_name))) == NULL)
return (1);
exit(0);

打印 my_strlen(d_name) 没有给出错误,因此它不是来自于此

结构:

typedef struct          s_tetrimino 
{
int id;
int sundial;
int color;
int height;
int width;
int end;
char **shape;
t_coord coord;
char *name;
} t_tetrimino;

谢谢

最佳答案

分配

new = malloc(sizeof(t_node *))

为节点指针分配内存,但这

new = malloc(sizeof(*new))

为一个节点struct分配内存。

编辑:如以下代码所示:

#include <stdio.h>

typedef struct {
double a;
double b;
double c;
} t_node;

int main(void)
{
t_node *new;
printf("%zu\n", sizeof(t_node *));
printf("%zu\n", sizeof(*new));
return 0;
}

程序输出(32位编译器)

4
24

关于c - valgrind memcheck 分配字符串内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39046160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com