GDB:在结构中显示不正确的值

GDB: Displaying incorrect values in struct

我正在尝试实现 malloc 函数,看起来 gdb 从这个 struct:

给我一些奇怪的值
struct MemoryBlock {

    struct MemoryBlock * next;
    size_t               size;
    signed char          is_free;
} startBlock;

这就是我用 gdb:

调试它的函数
struct MemoryBlock * create_new_block(size_t size)
{

    struct MemoryBlock * ret_block;

    // add some space for the struct block
    size += sizeof(struct MemoryBlock);

    ret_block = (void *) sbrk(size);

    // test first, if we can allocate that much of ram
    if (ret_block == (void *) -1)
        return NULL;

    ret_block->size    = size - sizeof(struct MemoryBlock);
    ret_block->is_free = 0;
    ret_block->next    = NULL;

    return ret_block; // HERE'S the breakpoint
}

所以问题来了(我在断点return ret_block):

如果我想查看 ret_block 指针中的值类型,我得到的是:

(gdb) p (struct MemoryBlock) ret_block
 = {next = 0x555555559000, size = 140737488347680, is_free = -53 '3'}

size 很好,因为如果我将它转换为十进制系统,那么我将得到 3 的预期结果。 (函数的参数 size 目前是 3

但我很惊讶 nextis_free 不是 0 因为最后三行应该都设置为 0。

所以我查了一下内存中的内容:

如您所见,每个值都正确存储在我的堆中。但是,如果我这样做 p (struct MemoryBlock) ret_block 为什么会得到这些值?

如果您需要整个代码

#include <unistd.h>
#include <stdio.h>

/* ============
 * Structs 
 * ============ */
struct MemoryBlock {

    struct MemoryBlock * next;
    size_t               size;
    signed char          is_free;
} startBlock;

/* ==============
 * Functions 
 * ============== */
struct MemoryBlock * create_new_block(size_t size);
void * malloc(size_t size);

/* ==================
 * Main Programm 
 * ================== */
int main()
{

    char * buffer;
    char * b2;
    unsigned short index;

    // The start of my heap :D
    startBlock.is_free = 0;
    startBlock.size    = 0;

    buffer = malloc(3);
    b2 = malloc(3);
    
    // ----- ERROR -----
    if (buffer == NULL || b2 == NULL)
        return 1;
    // ----- ERROR -----
    
    // fill the buffers with random stuff
    for (index=0; index<2; index++) {
        buffer[index] = 'a';
        b2[index] = 'b';
    }

    buffer[index] = '[=13=]';
    b2[index] = '[=13=]';
    puts(buffer);
    puts(b2);

    return 0;
}

struct MemoryBlock * create_new_block(size_t size)
{

    struct MemoryBlock * ret_block;

    // add some space for the struct block
    size += sizeof(struct MemoryBlock);

    ret_block = (void *) sbrk(size);

    // test first, if we can allocate that much of ram
    if (ret_block == (void *) -1)
        return NULL;

    ret_block->size    = size - sizeof(struct MemoryBlock);
    ret_block->is_free = 0;
    ret_block->next    = NULL;

    return ret_block;
}

void * malloc (size_t size)
{

    struct MemoryBlock * ret_block;
    struct MemoryBlock * prev_block;

    prev_block = &startBlock;
    ret_block = startBlock.next;

    // go through the linked lists and look if you can find a suitable block
    while (ret_block != NULL && (ret_block->size < size || !ret_block->is_free)) 
    {
        prev_block = ret_block;
        ret_block = ret_block->next;
    }

    // couldn't find a suitable block => create a new one
    if (ret_block == NULL) {
        ret_block = create_new_block(size);

        if (ret_block == NULL)
            return NULL;
    }

    prev_block->next = ret_block;
    ret_block->is_free = 0;
    return ret_block;
}

好吧,我的一位朋友告诉我我的问题...选角错误!解决方法如下:

(gdb) p * ret_block
 = {next = 0x0, size = 3, is_free = 0 '[=10=]0'}

一颗星星就足以获得想要的输出...