Confused
Confused

Reputation: 3

How do I fix the size of my array?

everyone. I am struggling with understanding why my numbers variable keeps outputting 1. I am trying to double the size anytime the number of elements is equal to the size of the array but I end up not getting anywhere near that part of my code.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int selection;
    int size = 2;
    // Initializing the array
    int *dArray;
    int v = 0;
    int i, j, temp;
    dArray = new int[size];
    dArray[0] = 2;
    dArray[1] = 3;
    int numbers = sizeof(dArray) / sizeof(dArray[0]);
    do {
        // Printing the menu
        cout << "1) Print Elements" << endl;
        cout << "2) Add Element" << endl;
        cout << "3) Delete Element" << endl;
        cout << "4) Return Size" << endl;
        cout << "5) Exit" << endl;
        cout << "Enter your selection number: ";
        cin >> selection;
        switch (selection)
        {
        case 1:
            // Outputting the elements
            for (int i = 0; i < size-1; i++) 
            {
                cout << dArray[i] << ", ";
            }
            cout << dArray[size - 1] <<endl;
            cout << numbers << endl;
        break;
        case 2:
            // Asking for another element
            cout << "What number shall be put into the array? \n";
            cin >> v;
            if (numbers== size)
            {
                // If the size is too small...
                int *nArray = new int[2 * size];
                for (int i = 0; i < size; ++i)
                {
                    nArray[i] = dArray[i];
                }
                delete[] dArray;
                dArray = nArray;
                // Finished creating a new array
                cout << "Array size expanded to " << 2 * size << endl;
                // Adding the element
                dArray[size] = v;
                size = 2 * size;
                // Sorting the elements
                for(i=0;i<numbers;i++)
                {
                    for(j=i+1;j<numbers;j++)
                    {
                        if(dArray[i]>dArray[j])
                        {
                            temp =dArray[i];
                            dArray[i]=dArray[j];
                            dArray[j]=temp;
                        }
                    }
                }
            }
            else
            {
                // Adding the element
                dArray[size] = v;
                size = 2 * size;
                // Sorting the elements
                for(i=0;i<numbers;i++)
                {
                    for(j=i+1;j<numbers;j++)
                    {
                        if(dArray[i]>dArray[j])
                        {
                            temp =dArray[i];
                            dArray[i]=dArray[j];
                            dArray[j]=temp;
                        }
                    }
                }
            }
        break;
        }
    } while (selection!= 5);
    cin.get();
    cin.ignore();
    return 0;

}

Does anyone know why the sizeof function keeps acting this way?

Upvotes: 0

Views: 130

Answers (3)

Pete Becker
Pete Becker

Reputation: 76345

sizeof(dArray) gives you the size of dArray. dArray is defined as int *dArray, so its size is sizeof(int*). That doesn't change, regardless of what the pointer points to. That's different from an actual array; your size code would work correctly with int dArray[3];.

To get the size of the array, just use your size variable; that's the number of int objects that were allocated.

Even better, use std::vector<int>; its size() member function tells you how many elements it has.

Upvotes: 1

DaveM
DaveM

Reputation: 1

int numbers = sizeof(dArray) / sizeof(dArray[0]);

Numbers is resolving to 1 because dArray is a pointer to the start of your array, not the array itself. When you call sizeof() you are getting the size of the pointer, not the array. On a 32 bit application, the pointer is 32 bits (4 bytes) and your first int is also 32 bits (4 bytes). So what this statement resolves to is

int numbers = 4 / 4;

leaving 1. If you instead declared an array of doubles then sizeof(dArray[0]) would be 8 but the size of the pointer would still be 4, so the statement would resolve to numbers = 4 / 8 = .5, but since numbers is an int it would just resolve to 0. So your code would still break, but in new exciting ways.

Interestingly enough, if you compiled this as a 64 bit application sizeof(dArray) would be 64 bits (8 bytes), so you would get numbers = 8 / 4 = 2 as you expected. Then you would double your array size and your code would break because numbers would resolve to 2 which would potentially be even more confusing, so be thankful you caught it now!

I agree with what others have said though. If you use a Vector you can just keep pushing values onto the end of it and let it worry about changing the size of the array!

Upvotes: 0

Drew Dormann
Drew Dormann

Reputation: 63795

This is not the size of your array. It's the size of a pointer, divided by the size of an int.

int numbers = sizeof(dArray) / sizeof(dArray[0]);

This is the size of the array in your program.

int numbers = size;

Upvotes: 1

Related Questions