C 指针内容更改为0x1

C 指针内容更改为0x1,c,pointers,stack,C,Pointers,Stack,我正在使用静态数组编写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(

我正在使用静态数组编写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);
   }



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

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


其次,一个文件作用域数组
uint8\u t st[100000000]
可能超出了C实现的能力。

如果您想研究如何编写malloc函数,请将其称为其他函数,如
my\u malloc
,Unix系统的libc是专门为那些想要实现自己的
malloc
的程序编写的。显然,只有重新实现所有相应的函数,这才有效。遗憾的是,glibc没有正确地支持这一点,因此导致了错误。
init
0x7f2ce1c984e0 
0x7f2ce1c984e0 
33 0x7f2d1d644ed8 
1 0x7f2ce1c984e0 
2 0x1 
3 0x1 
4 0x1 
5 0x1 
6 0x1 
7 0x1 
0x1 
find free!
0x1 
0x7f2ce1c984e0