Search code examples
c++arraysdynamic-memory-allocation

keeping track of dynamic memory


#include <iostream>
#include <cctype> // for isdigit function
int main()
{
    int initial_size = 10;
    int* temp = new int[initial_size];
    int actual_size = 0;
    while ( 1 )
    {
        char ch;
        cin >> ch;
        if ( ch == 'E' )
            break;
        if ( isdigit(ch) ){
            cin.unget();    
            int n;
            cin >> n;       //If it's an integer, store it.
            actual_size++;  // Keep tracks of actual size.
            if ( actual_size <= initial_size )
                *temp++ = n; //Storing in temp array...

            else if ( actual_size > initial_size ){ //When there's not enough space in array..
                int old_initial_size = initial_size; 
                initial_size *= 2; //Doubling the size of initial_size after storing it in old_initial_size.
                int* hold = new int[old_initial_size]; 
                for ( int i = 0; i < old_initial_size; i++)
                    hold[i] = temp[i];   // From temp to hold.This is my attempt at keeping the values of temp array.

                temp = new int[initial_size]; // After passing every value in temp i resize it.

                for ( int i = 0; i < old_initial_size; i++)
                    temp[i] = hold[i];     //From hold to newly created temp.

                delete[] hold;  

            }
        }
    }
    int* actualArray = new int[actual_size];
    for ( int i = 0; i < actual_size; i++)
        actualArray[i] = temp[i];

    delete[] temp;  

    for ( int i = 0; i < actual_size; i++)
        cout << actualArray[i] << " ";

}

Here's what i want to do:

I want to get input from user till E is entered.If the input is an integer i want to store it in a pre-defined temporary dynamic array with size of 10.

While doing that i want to count the actual number of numeric inputs.If that exceeds the initial size of temporary array i want to double temporary array's size while keeping the old inputs inside of it.

And when the loop terminates i'd like to pass inputs in temporary array to my actual array which's size is exactly the same as numeric inputs.(actual_size)

You can see my attempt above.As output all i get is random numbers but atleast i get correct number of random numbers.

For example if i enter 3 4 E I get something like 13123213 1541321231


Solution

  • The statement *temp++ = n; is the root of your problem. You actually incrementing the pointer. So you can't use loop over temp after that. It's not pointing at the beginning of your memory. That's why you're seeing random numbers.

    I would do:

       temp[actual_size - 1] = n; //Storing in temp array...
    

    I should add that your re-allocation code is much more complicated that it needs. You just need a new array, memcpy the old value, set temp to the new array and deallocate the old one

    int *oldArray = temp;
    temp = new int[initial_size];
    memcpy(temp, oldArray, old_initial_size * sizeof(int));
    delete [] oldArray;
    temp[old_initial_size] = n;