在C中初始化数据结构来管理内存池

发布于 2024-10-20 20:53:25 字数 695 浏览 4 评论 0原文

我正在为库编写一个简单的函数,它将接受其他函数管理的内存大小作为参数。

我有一个数据结构,保存用户初始化的这个大内存池的信息。

typedef struct memBlock{
    struct memBlock* next;
    unsigned int size;  // Size of this block
    unsigned int is_used;  // bool 0 = not used 1 = used
}  memBlock;

我也有这个函数,我试图弄清楚如何初始化这个数据结构以及分配足够的空间来最初管理?

int initialize_memory(unsigned long size){

    memBlock *ptr; // the beginning of our whole memory to be handled

    ptr = malloc(size); // this is the ptr to the original memory first allocated.
    ptr->next = NULL;
    ptr->size = NULL;
    ptr->is_used = 0;

    has_initialized = 1; // the memory has been initialized
}

请帮忙

i am writing a simple function for a library, that will take in as a parameter the size of memory to be managed by my other functions.

i have a data structure that holds the information of this large memory pool initialized by the user.

typedef struct memBlock{
    struct memBlock* next;
    unsigned int size;  // Size of this block
    unsigned int is_used;  // bool 0 = not used 1 = used
}  memBlock;

I also have this function that i am trying to figure out how to initialize this data structure as well as allocate enough space to be managed initially?

int initialize_memory(unsigned long size){

    memBlock *ptr; // the beginning of our whole memory to be handled

    ptr = malloc(size); // this is the ptr to the original memory first allocated.
    ptr->next = NULL;
    ptr->size = NULL;
    ptr->is_used = 0;

    has_initialized = 1; // the memory has been initialized
}

please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-10-27 20:53:26

ptr->size = NULL; 更改为 ptr->size = size;。您还需要返回ptr,或将其存储在某个地方。您的函数返回 int,但您不返回任何内容。 has_initialized 似乎没有必要——您知道您已经初始化,因为您的内存池(您将返回的 ptr 值)不是 NULL。如果您需要更多帮助,则必须进行更多解释。

附录:你需要决定memBlock.size是分配空间的大小还是memBlock表示的内存块的大小...如果是后者,那么你需要通过减去memblock本身占用的空间来计算超出您分配的空间量: ptr->size = size - sizeof(struct memBlock); 您还需要一种方法来寻址内存池...因为它紧跟在 memBlock 之后,它的地址是 (ptr + 1)&ptr[1] (如果你不明白,请查找“C 中的指针算术”)。

PS你在评论中写道“本质上我还有另一个函数,它的作用类似于‘malloc’来保留一定数量的字节,但首先会检查这个数据结构以查看我的池中是否有可用的内存”

你为什么要这样做那?考虑到技能水平和投入的时间,malloc 已经比您的函数更好地管理内存,并且没有必要在其之上分层另一个内存分配器。除非这是一个编写内存分配器的学校项目,在这种情况下你应该预先说明这一点。

Change ptr->size = NULL; to ptr->size = size;. You also need to return ptr, or store it somewhere. Your function returns int, but you don't return anything. has_initialized seems unnecessary -- you know you've initialized because your memory pool (the ptr value you will return) isn't NULL. If you need more help than that, you're going to have to explain more.

Addendum: You need to decide whether memBlock.size is the size of the allocated space or the size of the memory block represented by the memBlock ... if the latter, then you need to account for the space occupied by the memblock itself by subtracting that off the amount of space you allocated: ptr->size = size - sizeof(struct memBlock); You also need a way to address your memory pool ... since that immediately follows the memBlock, its address is (ptr + 1) or &ptr[1] (if you don't understand that, look up "pointer arithmetic in C").

P.S. You wrote in a comment "Essentially i also have another function that will act like 'malloc' to reserve a number of bytes but will first check this data structure to see if any memory is available from my pool"

Why do you want to do that? malloc already manages memory far better than your function will, considering the skill level and time invested, and there's no point in layering another memory allocator on top of it. Unless this is a school project to write a memory allocator, in which case you should say that up front.

甩你一脸翔 2024-10-27 20:53:26
typedef struct memBlock {
   unsigned int size;
   unsigned int initialized;
   void* block;
} memBlock;

memBlock* new_memBlock(unsigned int size)
{
    memBlock* memblock;

    memblock = malloc(sizeof(memBlock));

    if (memblock)
    {
        memblock->size = size;
        memblock->block = malloc(size);

        if (memblock->block)
            memblock->initialized = 1;
    }

    return memblock;
}

void free_memBlock(memBlock** memblock)
{
    if (*memblock)
    {
        free(*memblock->block)
        *memblock->block = 0;
    }

    free(*memblock);
    *memblock = 0;
}

void main()
{
    memBlock* memblock = new_memBlock(1024);

    if (memblock && memblock->initialized)
        printf("Initialized\n");
    else
        printf("Not initialized\n");

    free_memBlock(&memblock);
}
typedef struct memBlock {
   unsigned int size;
   unsigned int initialized;
   void* block;
} memBlock;

memBlock* new_memBlock(unsigned int size)
{
    memBlock* memblock;

    memblock = malloc(sizeof(memBlock));

    if (memblock)
    {
        memblock->size = size;
        memblock->block = malloc(size);

        if (memblock->block)
            memblock->initialized = 1;
    }

    return memblock;
}

void free_memBlock(memBlock** memblock)
{
    if (*memblock)
    {
        free(*memblock->block)
        *memblock->block = 0;
    }

    free(*memblock);
    *memblock = 0;
}

void main()
{
    memBlock* memblock = new_memBlock(1024);

    if (memblock && memblock->initialized)
        printf("Initialized\n");
    else
        printf("Not initialized\n");

    free_memBlock(&memblock);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文