Thread: Memory Pool Question

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008

    Memory Pool Question

    Using this simple algorithm when you append a character into a string will reduce calls to [m|re|c]alloc...
    typedef struct
        char *value;
        size_t length;
        size_t allocated;
    } string;
    void str_append(string *str, char c)
        if((str->length + 2) > str->allocated)
            str->allocated += 255;
            str->value = realloc(str->value, str->allocated);
        str->value[str->length] = c;
        str->value[++(str->length)] = 0;
    I want to implement this algorithm to make a simple memory pool. But the problem is, when I use realloc, the returned memory address might be changed is possible...
    function pool_request(MemoryPool pool_obj, number size)
        if pool_obj.length + size > pool_obj.allocated
            pool_obj.allocated += 255
            pool_obj.allocation = realloc(pool_obj.allocation, pool_obj.allocated)
        end if
        pool_obj.length += size
        return pointer operation:(pool_obj.allocation + (pool_obj.length - size))
    end function
    It seems the pseudocode above is impossible.

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    If realloc succeeds, it moves the data for you, although any pointers will be invalidated.
    So just change the base pointer and you should be fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 04:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM