CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Jan 2012
    Location
    United States
    Posts
    12

    Question 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:
    voidpBuffer 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
    {
        
    NodepNode;
        
    size_t nodeSize;
        
    NodeHeaderpNext;

    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
  •  





Click Here to Expand Forum to Full Width

Featured