Dynamic array definition in C++

Hello-- could someone please explain to me why the following C++ code is printing 2 and not 5?

# include <iostream>
using namespace std;

int main(){

    int *foo;
    foo = new int[5];

    cout << sizeof(foo) / sizeof(foo[0]) << endl;

    delete[] foo;

    return 0;

}


Thank you!
jt9812Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Because sizeof is not returning the length of the array but rather the size of the type:

http://en.cppreference.com/w/cpp/language/sizeof

In this case the size of a pointer (which is 8 on a 64-bit system) and the size of an integer (which is 4).

-saige-
ZoppoCommented:
Just as addition: What you try only works with statically initialized arrays, i.e.:

    int foo[] = { 1, 2, 3, 4, 5 };
    cout << sizeof(foo) / sizeof(foo[0]) << endl;

This should output 5.
sarabandeCommented:
to add to above comments:

note, it is an inheritance of c that pointers to the first array element can be used like arrays but actually are bearing no information of the size of the array. things become worse if you pass an array to a function and the array turned to a pointer (silently).

int f(int array[10])   // the 10 was ignored and could (should) omitted
{
      return sizeof(array)/sizeof(array[0]));  // returns pointer size divided by int size
}

int arr[10] = 0´;
size_t num_elements = sizeof(arr)/sizeof(arr[0]);  // ok
size_t size = f(arr);   // size = 2

Open in new window


though f explicitly used array syntax for the argument, the array was turned to pointer.

to come out of this, you have two choices:

1. use an array which is a member of a struct

struct Array10
{
     int arr[10];
};

int f(Array10 array)
{
       return sizeof(array.arr)/sizeof(array.arr[0]));  // returns 10
}

Open in new window


2. use std::vector instead of c arrays
int f(std::vector<int>& array)
{
       return array.size();
}

std::vector<int> array(10);
int num = f(array); // returns 10

Open in new window


note, the internal array of the std::vector is guaranteed to be a normal c array. so, you can use a std::vector<int> even if a function wants a c array:

void g(int arr[], int numitems)
{
     ....
}

std::vector<int> array(10);
g(&array[0], array.size());

Open in new window


Sara

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.