指针内容更改为0x1

时间:2014-11-07 19:23:47

标签: 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(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来调试。

1 个答案:

答案 0 :(得分:2)

可能因为您正在重新定义mallocprintf可能会在malloc内的某个地方进行调用。 您不能插入自己的uint8_t st[1000000000]。从技术上讲,代码会调用未定义的行为,并且可能发生任何事情。

其次,文件范围数组{{1}}可能会超出C实现的功能。