gpt4 book ai didi

c - 指针内容变为 0x1

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:38 25 4
gpt4 key购买 nike

我正在使用静态数组编写一个 malloc 函数

代码在这里

#define h_footer(h_d_r)  ((footer*)(((void*)(h_d_r)) + (h_d_r)->size+ FOOTER_SIZE))
#define f_header(f_t_r) ((header*)(((void*)(f_t_r)) - (f_t_r)->size- FOOTER_SIZE))
#define flag(fhdr) ((fhdr)->flag)
#define mark(fhdr) ((fhdr)->flag=1)
#define unmark(fhdr) ((fhdr)->flag=0)

#define next(fhdr) ((header*)((fhdr)->next))
#define prev(fhdr) ((header*)((fhdr)->prev))


#define MAX_STACK_SIZE 1000000000

int8_t st[MAX_STACK_SIZE];

typedef struct Header
{
size_t size;
size_t flag;
void* prev;
void* next;
} header;

typedef struct Footer
{
size_t size;
size_t flag;
} footer;

size_t FOOTER_SIZE=sizeof(footer);
size_t HEADER_SIZE=sizeof(header);

header* start_hdr;
footer* end_ftr;

size_t blk_size;
size_t occupied_length=0;
void *malloc(size_t size)
{
//puts("malloc!");
//printf("MSG:malloc size %zx\n",size);
//printf("MSG:malloc size %zx %zx %zx %zx",sizeof(void*),sizeof(char),sizeof(size_t),sizeof(header));

if(size<HEADER_SIZE+FOOTER_SIZE)
{
size=HEADER_SIZE+FOOTER_SIZE;
}
static char flag=1;
header* cur;
header* record;
footer* cur_f;
footer* record_f;
// size_t temp;
extern int8_t st[MAX_STACK_SIZE];



//initialize the header and footer;
if(flag)
{
puts("init");
start_hdr=st;
mark(start_hdr);
start_hdr->size= 0;


//initialize the whole block
header* freeblk_hd=(void*) (st+HEADER_SIZE);
freeblk_hd->size=MAX_STACK_SIZE-HEADER_SIZE-FOOTER_SIZE-sizeof(size_t)-FOOTER_SIZE;//for safety
//unmark(freeblk_hd);

//footer* freeblk_ft;

//freeblk_ft=h_footer(freeblk_hd);
//freeblk_ft->size=MAX_STACK_SIZE-HEADER_SIZE-FOOTER_SIZE-sizeof(size_t)-FOOTER_SIZE;//for safety
//unmark(freeblk_ft);

// start_hdr->next= (void*)freeblk_hd;
// printf("%p \n",start_hdr);
// start_hdr->prev= (void*)freeblk_hd;
//freeblk_hd->next=start_hdr;
//freeblk_hd->prev=start_hdr;

printf("%p \n",start_hdr);

end_ftr=(void*)freeblk_ft+FOOTER_SIZE;
printf("33 %p \n",end_ftr);
end_ftr->size = 0;
mark(end_ftr);

flag=0;

printf("1 %p \n",start_hdr);
printf("2 %p \n",start_hdr);
printf("3 %p \n",start_hdr);
printf("4 %p \n",start_hdr);
printf("5 %p \n",start_hdr);
printf("6 %p \n",start_hdr);
printf("7 %p \n",start_hdr);
}

printf("%p \n",start_hdr);
puts("find free!");
printf("%p \n",start_hdr);
printf("%p \n",st);

cur=next(start_hdr);
puts("find free!");
record=start_hdr;


puts("find free!");
//printf("\nptr ustart:%p, end block: %pthen\n",start_hdr, end_ftr);
while(cur!=start_hdr)
{

if( cur->size >= size && (record->size > cur->size || record->size==0) )
{
record=cur;
}
//printf("%p %zx\n", cur, cur->size);
cur=next(cur);
}

//printf("get idear %p", record);

if(record!=start_hdr)
{


if(record->size > size+HEADER_SIZE+FOOTER_SIZE)
{
//puts("split!");
cur=(void*)record+ size+FOOTER_SIZE*2;
record_f=(void*)record+size+FOOTER_SIZE;

//this block will set all the information for the cur blk
//2 size, 2 flag

cur->size=record->size-size-FOOTER_SIZE*2;
cur_f=h_footer(cur);
cur_f->size=cur->size;
unmark(cur);
unmark(cur_f);


// debuging line
//if(cur_f!=h_footer(record)){puts("footer mistake");exit(0);}

//this block will set all the information for the record blk
//2 size, 2 flag
record->size=size;
record_f->size=size;
mark(record);
mark(record_f);


// this block will replace the record blk with curblk

cur->next=record->next;
cur->prev=record->prev;
next(cur)->prev=(void*) cur;
prev(cur)->next=(void*) cur;

//return ptr
//printf(" malloc ptr %p\n",(void*)record+ FOOTER_SIZE);
return (void*) record+ FOOTER_SIZE;

}
else
{

//puts("needn't split!");
next(record)->prev=record->prev;
prev(record)->next=record->next;
record_f=h_footer(record);

mark(record);
mark(record_f);
// debuging line
//if(record->size!=record_f->size){puts("footer mistake");exit(0);}
//printf(" malloc ptr %p, actual malloc size %zx\n",(void*)record+ FOOTER_SIZE,record->size);
return (void*) record+ FOOTER_SIZE;

}
}
else
{
printf("MEMORY ERROR: exceed maximam allocation bound");
exit(0);
}



}

但是运行以段错误结束

有输出

init
0x7f2ce1c984e0
0x7f2ce1c984e0
33 0x7f2d1d644ed8
1 0x7f2ce1c984e0
2 0x1
3 0x1
4 0x1
5 0x1
6 0x1
7 0x1
0x1
find free!
0x1
0x7f2ce1c984e0

我想知道为什么指针值会变成0x1,以及如何修复它。我不熟悉 gdb,所以我使用 print 进行调试。

最佳答案

可能是因为您正在重新定义 malloc,它可能在 printf 的深处某处被调用。您不允许插入自己的 malloc。从技术上讲,代码会调用未定义的行为,任何事情都可能发生。

其次,文件作用域数组 uint8_t st[1000000000] 可能超出您的 C 实现的能力。

关于c - 指针内容变为 0x1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808563/

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