C++内存分配与链表实现
我正在编写软件来模拟“首次适应”内存分配模式。
基本上,我分配一个大的 X MB 内存块,并在根据模式请求块时将其细分为块。
我使用一个名为“node”的链表作为每个内存块的标头(这样我们就可以找到下一个块,而无需繁琐地循环遍历每个地址值。`
head_ptr = (char*) malloc(total_size + sizeof(node));
if(head_ptr == NULL) return -1; // Malloc Error .. :-(
node* head_node = new node; // Build block header
head_node->next = NULL;
head_node->previous = NULL;
// Header points to next block (which doesn't exist yet)
memset(head_ptr,head_node, sizeof(node));
但是
最后一行返回:
error: invalid conversion from 'node*' to 'int'
我明白为什么这是无效的。但如何将我的节点放入新分配的内存的指针位置?
I'm writing software to simulate the "first-fit" memory allocation schema.
Basically, I allocate a large X megabyte chunk of memory and subdivide it into blocks when chunks are requested according to the schema.
I'm using a linked list called "node" as a header for each block of memory (so that we can find the next block without tediously looping through every address value.
head_ptr = (char*) malloc(total_size + sizeof(node));
if(head_ptr == NULL) return -1; // Malloc Error .. :-(
node* head_node = new node; // Build block header
head_node->next = NULL;
head_node->previous = NULL;
// Header points to next block (which doesn't exist yet)
memset(head_ptr,head_node, sizeof(node));
`
But this last line returns:
error: invalid conversion from 'node*' to 'int'
I understand why this is invalid.. but how can I place my node into the pointer location of my newly allocated memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,并不是将
head_node
复制到head_ptr
(您正在考虑memcpy
),它用于初始化内存(清除为 0、标记为已释放等...)。
在这种情况下,您可以简单地将
head_ptr
转换为node*
:现在您不必
删除 head_node
或复制值完全为head_ptr
。So, it's not to copy the
head_node
intohead_ptr
(you are thinking ofmemcpy
),it's for initializing memory (clearing to 0, marking as freed, etc...).
In this case, you could simply cast the
head_ptr
to anode*
:And now you don't have to
delete head_node
, or copy the values tohead_ptr
at all.阅读文档。
memset
接受一个 int (但解释为作为unsigned char
)作为第二个参数。这指定要设置内存区域的前 n 字节的值,其中 n 是第三个参数。您可以使用
memcpy
,这将将内存的一个区域复制到另一区域。尝试:编辑:另一种选择是使用 head_ptr 的指针转换来设置前一个和下一个值,如 Simon 所建议的。
Read the docs.
memset
takes an int (but interpreted as aunsigned char
) as a second parameter. This specifies the value to set the firstn
bytes, wheren
is the third parameter, of the memory region to.You could use
memcpy
, which will copy one region of memory to another. Try:EDIT: Another alternative is to use a pointer cast of head_ptr to set the previous and next values, as suggested by Simon.
如果我正确理解你的问题,你想将你的节点对象构造到 head_ptr 分配和指向的内存中。如果需要调用节点构造函数,可以通过 placement new 运算符如下:
如果您确实需要确保也调用析构函数,那么您必须手动调用删除:
If I understand your question correctly, you want to construct your node object into the memory allocated and pointed to by head_ptr. If you need the node constructor to be called, you do this via the placement new operator like so:
If you do need to make sure that the destructor is called as well, then you have to call delete manually:
你不能这样分配指针。第二个参数是要重复的
int
。来自memset(3):
You cannot assign pointers like that. Second argument is
int
to be repeted.From
memset(3)
: