在C中初始化数据结构来管理内存池
我正在为库编写一个简单的函数,它将接受其他函数管理的内存大小作为参数。
我有一个数据结构,保存用户初始化的这个大内存池的信息。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
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;
toptr->size = size;
. You also need to returnptr
, or store it somewhere. Your function returnsint
, but you don't return anything.has_initialized
seems unnecessary -- you know you've initialized because your memory pool (theptr
value you will return) isn'tNULL
. 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.