|
-
February 4th, 2012, 01:26 PM
#1
C - Realloc, Linked Lists, and pointers
So I'm creating a simple memory buffer in C. Lets say I create the buffer of memory:
PHP Code:
void* pBuffer = malloc(1024);
This buffer will be used to hold a bunch of structs.
PHP Code:
struct Node { ...Bunch of PODs }
And of course each Node will have a header struct in order to link everything in the buffer.
PHP Code:
struct NodeHeader { Node* pNode; size_t nodeSize; NodeHeader* pNext; }
When I run out of free buffer space, I'll call realloc on pBuffer to make the buffer larger. Now I know its best to assume it'll always assign a whole new block of memory and copy everything over. Which will make any pointer, pointing into the original buffer location unsafe. But what about pointers within in the buffer? Will I need to manually go back through and reassign all my struct NodeHeader pointers? Or will what they are pointing to also be updated in the realloc?
Keep in mind, its paramount that I always have everything stored in continuous segment of memory. So just doing another malloc and linking the two blocks of memory together isn't an option.
Thanx in advance.
Last edited by JaySoyer; February 4th, 2012 at 01:28 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|