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?
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.
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.
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();
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.
精彩评论