resizing array pointed to by pointer

This is a discussion on resizing array pointed to by pointer within the C++ Programming forums, part of the General Programming Boards category; I have a pointer to an array: Code: T *array = new T[capacity]; I want to double the array size ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    resizing array pointed to by pointer

    I have a pointer to an array:

    Code:
    T *array = new T[capacity];
    I want to double the array size when the array becomes to small. This is how I'm doing it, but its not working correctly. For some reason it gets rid of the first element:

    Code:
    template<typename T>
    void ArrayStack<T>::doubleArraySize() {
        capacity *= 2;
        T *tempArray = array;
        array = new T[capacity];
        
        for (int i=0; i<=tos; i++) {
            array[i] = tempArray[i];
        }
        array = tempArray;
        delete[] tempArray;
    }
    Can anyone point out what I'm doing wrong?


    When I print it should say:

    3
    2
    1

    instead:

    3
    2
    some address

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    One problem is that you are accessing the original array out of bounds (assuming that tos is the size variable) since the loop condition is i <= tos instead of i < tos. The next problem is that since you assign tempArray to array... the new array is lost, and then on the last line you destroy the original array, so in the end you have no arrays left.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Thanks. That makes a lot of sense and not sure how I missed some of it.

    I've been playing with a few things that work, but not sure which is the best. This is what I've had that works, but relies on temporary storage:

    Code:
    template<typename T>
    void ArrayStack<T>::doubleArraySize() {
        capacity *= 2;
        T tempArray[tos+1];
        for (int i=0; i<=tos; i++) {
            tempArray[i] = array[i];
        }
        
        array = new T[capacity];
        for (int i=0; i<=tos; i++) {
            array[i] = tempArray[i];
        }
    }
    -This works, but i'm not sure how I should release the other memory when i reassign the pointer. Also, i'm not sure if this is the ideal way to do what I'm wanting to do.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    This looks like the syntax to create a variable length array, but variable length arrays do not exist in standard C++:
    Code:
    T tempArray[tos+1];
    Basically, use new[] to create a new array that is twice the current capacity. Copy over the current elements into that new array. Destroy the old array with delete[] and then assign the pointer to the new array to the member pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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