Allocation from "static array"

This is a discussion on Allocation from "static array" within the C Programming forums, part of the General Programming Boards category; Hi all, let's say we have a fixed array: Code: char pool[10000]; And now we want to "allocate" (not using ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question Allocation from "static array"

    Hi all,
    let's say we have a fixed array:
    Code:
    char pool[10000];
    And now we want to "allocate" (not using "malloc", just take the addresses from this static array) e.g. 5 bytes and save them in some "char*", so that after the allocation:
    Code:
    char* newArray;
    
                .
                .   // Do that allocation (but how?)
                .
    
    newArray[0] = 'a';   // Can be. Allocated address: "&pool[0]"
    newArray[1] = 'b';   // Can be. Allocated address: "&pool[1]"
    newArray[2] = 'c';   // Can be. Allocated address: "&pool[2]"
    newArray[3] = 'd';   // Can be. Allocated address: "&pool[3]"
    newArray[4] = 'e';   // Can be. Allocated address: "&pool[4]"
    // newArray[5] = 'x';   CAN'T BE, we have allocated just 5 bytes

    So does anybody know how to do that allocation to solve my problem?

    Thanks.
    Petike

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're going to need to keep track of "objects" within the pool. For example how much you've allocated, and the "start" address within the pool.

    Code:
    struct poolObject_s
    {
        void * ptr;
        size_t size;
    };
    Then when you have to allocate, you find the first (or otherwise if you have a more advanced algo') free contiguous space that's large enough. Perhaps some sort of table would do well here

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Problem? You haven't shown that simply using malloc is a problem. Care to elaborate on that?
    If you are going to use some kind of pool allocator, then what you use or how it works is entirely dependent on how flexible it needs to be. E.g. Will everything allocated from it be of the same size or type? Does it need to be threadsafe? etc...
    There's almost certainly no need to write it yourself. There are pre-made ones out there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think what Petike is charged to do is to create an alternative implementation of malloc.

    It is not too hard to do - you need to implement some way to add the memory back when freeing, and to be able to find the first free block that is suitable size.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't see how the static array though will be connected with dynamically allocating memory from the heap.
    Do you actually want to do your own malloc() or more a "virtual" malloc(), thus statically allocating int pool[] and then when your call your virtual_mallo() to be able to make some space valid to read/write?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    I don't see how the static array though will be connected with dynamically allocating memory from the heap.
    Do you actually want to do your own malloc() or more a "virtual" malloc(), thus statically allocating int pool[] and then when your call your virtual_mallo() to be able to make some space valid to read/write?
    Yes, I think the original post wants to use the array as a heap.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Thank you all very much for your answers.
    Petike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 05:43 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21