C++内存分配与链表实现

发布于 2024-08-27 04:10:46 字数 635 浏览 4 评论 0原文

我正在编写软件来模拟“首次适应”内存分配模式。

基本上,我分配一个大的 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 技术交流群。

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

发布评论

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

评论(4

夏了南城 2024-09-03 04:10:46
memset(void* memory, int value, size_t size)

因此,并不是将 head_node 复制到 head_ptr (您正在考虑 memcpy),
它用于初始化内存(清除为 0、标记为已释放等...)。

在这种情况下,您可以简单地将 head_ptr 转换为 node*

node* head_node = (node*)head_ptr;

现在您不必删除 head_node 或复制值完全为 head_ptr

memset(void* memory, int value, size_t size)

So, it's not to copy the head_node into head_ptr (you are thinking of memcpy),
it's for initializing memory (clearing to 0, marking as freed, etc...).

In this case, you could simply cast the head_ptr to a node*:

node* head_node = (node*)head_ptr;

And now you don't have to delete head_node, or copy the values to head_ptr at all.

回忆躺在深渊里 2024-09-03 04:10:46

阅读文档。 memset 接受一个 int (但解释为作为 unsigned char)作为第二个参数。这指定要设置内存区域的前 n 字节的值,其中 n 是第三个参数。

您可以使用 memcpy,这将将内存的一个区域复制到另一区域。尝试:

memcpy(head_ptr, head_node, sizeof(node));

编辑:另一种选择是使用 head_ptr 的指针转换来设置前一个和下一个值,如 Simon 所建议的。

Read the docs. memset takes an int (but interpreted as a unsigned char) as a second parameter. This specifies the value to set the first n bytes, where n is the third parameter, of the memory region to.

You could use memcpy, which will copy one region of memory to another. Try:

memcpy(head_ptr, head_node, sizeof(node));

EDIT: Another alternative is to use a pointer cast of head_ptr to set the previous and next values, as suggested by Simon.

一绘本一梦想 2024-09-03 04:10:46

如果我正确理解你的问题,你想将你的节点对象构造到 head_ptr 分配和指向的内存中。如果需要调用节点构造函数,可以通过 placement new 运算符如下:

node* head_node = new(head_ptr) node;

如果您确实需要确保也调用析构函数,那么您必须手动调用删除:

head_node->~node();

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:

node* head_node = new(head_ptr) node;

If you do need to make sure that the destructor is called as well, then you have to call delete manually:

head_node->~node();
栩栩如生 2024-09-03 04:10:46

你不能这样分配指针。第二个参数是要重复的int

来自memset(3):

SYNOPSIS
     #include <string.h>

     void *
     memset(void *b, int c, size_t len);

DESCRIPTION
     The memset() function writes len bytes of value c (converted to
     an unsigned char) to the byte string b.

RETURN VALUES
     The memset() function returns its first argument.

You cannot assign pointers like that. Second argument is int to be repeted.

From memset(3):

SYNOPSIS
     #include <string.h>

     void *
     memset(void *b, int c, size_t len);

DESCRIPTION
     The memset() function writes len bytes of value c (converted to
     an unsigned char) to the byte string b.

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