Results 1 to 8 of 8

Thread: Arrays on the free store

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94

    Question Arrays on the free store

    Hey Guys

    I was just wondering if there is anyway to increase the size of an array on the freestore after it has been declared.

    For instance

    i have
    BYTE *pArray
    pArray = new BYTE[12];

    now i want to increase the size of the array to 13 or 14 here

    Cheers Guys
    Peter
    "Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
    -- Judas Priest

    My email is [email protected]

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well using the old C-style memory allocation functions, you could use malloc() to allocate and realloc() to resize allocated memory. I guess there must be something similar for the new and delete operators, but I really don't know what.

    There is a placement option in the syntax of the new operator, but that's only used in user-defined new operators (used in operator new() functions in classes you want to handle memory allocation for yourself).

    You might be better off doing one of the following:

    • use malloc(), realloc() and free() for dynamic memory allocation
    • make a wrapper class for your array and implement new and delete yourself
    • don't use an array, use the STL vector class


    Of these, the easiest will probably be to use the STL vector class. It's like a normal array but it's resizeable and some other cool stuff.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    // C
    BYTE* array;
    array = (BYTE*)malloc(10 * sizeof(BYTE));
    
    // resize to 12
    BYTE* temparray = array;
    array = (BYTE*)malloc(12 * sizeof(BYTE));
    memcpy(array, temparray, 10 * sizeof(BYTE));
    free(temparray);
    
    // C++
    BYTE* array = new BYTE[10];
    
    // resize
    BYTE* temparray = array;
    array = new BYTE[12];
    memcpy(array, temparray, 10 * sizeof(BYTE));
    delete[] temparray;
    This is it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well I wouldn't say that's "it". That's one way of getting around the problem, which is similar to the way the STL vector class achieves it. It's not the same as resizing the existing array.

    Still, it does achieve the same result Check out the vector class if you want to do this kind of thing a lot though, this kind of thing is the whole point in its existence.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Actually, the vector is more efficient because it only reallocates memory in blocks of some integer multiple of the element size.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by parksie
    Actually, the vector is more efficient because it only reallocates memory in blocks of some integer multiple of the element size.
    HOW does it do that?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The vector is initialised, and it's empty. You add one more item to it using push_back. It then allocates enough space for 5 items (may be some other value, I think you can choose in the constructor or template arguments), and sets the reserved size to 5. However, the USED size is only 1.

    It copies your data into the first element in the buffer.

    When you come to add another element, it knows it has space so it merely adds them to the memory it already has. If it runs out (already got 5 items) then it allocates more memory (with the excess of 5) and copies the data in, appending the new item.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by parksie
    If it runs out (already got 5 items) then it allocates more memory (with the excess of 5) and copies the data in, appending the new item.
    I knew it was something tricky! Hehe, Can't trust any vectors anymore, they take away all your memory
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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