成功分配指针后,无法将值分配给结构指针

我正在编写一个早期的内核堆,以便在最终的堆可操作之前管理一些内存分配。当我给我的struct指针分配一个地址(没有被其他任何东西使用)时,当我试图给struct成员赋值时,问题就来了。

在回答之前,请记住,我没有任何类型的C库可供使用。使用QEMU和gdb通过远程进行调试。

//The struct
typedef struct mem_block{
    mblock_t *prev;
    boolean used;
    size_t size;
    mblock_t *next;
} mblock_t;

//The file-local pointer(before its init)
mblock_t *eheap=NULL;


//Function that assigns the values
mblock_t *init_early_heap(address_t start, size_t size){
    if(eheap==NULL){
        if(size < sizeof(mblock_t)){
            PANIC("Available memory below EHEAP requirement");
        }
        eheap = (mblock_t*)HEAP_START_ADDRESS;
        eheap->prev = NULL;
        eheap->next = NULL;
        eheap->size = (size_t)(size - sizeof(mblock_t));
        eheap->used = false;
        print("\nCreated Early Heap at ");
        print_hex_long(eheap);
        print("\nSize in bytes: ");
        print(itos(eheap->size));NL;
    }
    return eheap;
}

在函数返回后,当eheap->size为0(函数中的size参数不是0)时,我得到一个指向地址0xC0000000的指针和未接触的成员。

转载请注明出处:http://www.sywsjj.net/article/20230330/1315829.html