0

I'm trying to write a function that takes an array of any length as an argument and prints out each element in a new line, but since sizeof() returns its size in bytes, I tried dividing the size of the array by the size of a single element. When I tried running the programme passing an array with 9 elements, it only printed out the first two.

The function:

void PrintArray(int anArray[])
{
    using namespace std;

    int nElements = sizeof(anArray) / sizeof(anArray[0]);
    for (int nIndex=0; nIndex < nElements; nIndex++)
        cout << anArray[nIndex] << endl;
}

In order to find out what was wrong, I commented out the loop and added the statement

cout << sizeof(anArray) << " " << sizeof(anArray[0]) << endl;

and it printed out 8 4. How is it even possible for a 9 element array to be 8 bytes long? Does something happen to it when it's passed as an argument?

(Also, I have no clue how vectors work. I got started with C++ 3 days ago).

reggaelizard
  • 811
  • 2
  • 12
  • 31
  • 2
    http://stackoverflow.com/questions/1461432/what-is-array-decaying – DCoder Mar 11 '14 at 18:07
  • possible duplicate of [How do I find the length of an array?](http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) – juanchopanza Mar 11 '14 at 18:09
  • @DCoder Ah, that makes sense. I'm gonna try to figure out how to pass it by reference. – reggaelizard Mar 11 '14 at 18:11
  • Passing by reference won't change anything. For what you want to have, there's std::vector (and this one can - should - be passed by reference in this case). – Fabio Ceconello Mar 11 '14 at 18:13

5 Answers5

2

You can rely on sizeof to give you the size of an array, only within the scope of declaration.

For example:

void func()
{
    int array[10];
    int size = sizeof(array); // size = sizeof(int) * 10
    // The size of 'int' depends on your compiler
}

void func(int array[10])
{
    int size = sizeof(array); // size = sizeof(int*)
    // The size of 'int*' is the same as the size of any other type of pointer
    // It is typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system
}

In order to calculate the number of items in a given array (again, only in the scope of declaration), use:

int numOfItems = sizeof(array)/sizeof(*array);
barak manos
  • 29,648
  • 10
  • 62
  • 114
0

In C and C++ arrays don't exist as function arguments, they are converted to pointers; doesn't matter how you declare it. Use std::vector instead.

The reason for the result you got is that you're obtaining sizeof() of a pointer, which will always return the native word size (4 for 32 bit machines, 8 for 64 bit) no matter what the pointed type.

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
0

anArray is a pointer, and pointers are 8 bytes on your system. int is 4 bytes on your system.

You have no way to determine the length of the array in this case; you'll need the caller (all the way up the line, if necessary) to pass it to you. Better yet -- use a C++ container (vector, etc.)

The only time sizeof() will work the way you expect is if the local definition of the variable indicates the array size.

mah
  • 39,056
  • 9
  • 76
  • 93
0

When an array is passed by value as an argument of a function it is implicitly converted to a pointer to its furst element.

So these declarations are equivalent

void PrintArray( int anArray[] );
void PrintArray( int *anArray );

that is inside the body of the function parameter anArrayis a local variable with type int *. Pointers do not have information whether they point to a separate object or an object of some sequence.

Inside the function body operator

sizeof(anArray)

will return the size of an object of type int * that for your platform is equal to 8.

Usually such functions are declared as having two parameters: the pointer to the first element of the array and the size of the array

void PrintArray( const int anArray[], int n )
{
    using namespace std;

    for ( int i = 0; i < n; i++ )
        cout << anArray[i] << endl;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

It is better practice to use a vector in this situation unless there is some specific reason you need to use an array. For example, if you have a vector of integers, the following will work:

#include <iostream>
#include <vector>

using namespace std;

void PrintVector(vector<int>& aVector)
{
  for (auto i : aVector)
    cout << i << endl;
}

int main (int argc, char* argv[])
{
  vector<int> v = {1, 37, 898, 463, 32, 2, 5};
  PrintVector(v);
}

When executed, it will loop through all of the contents of the vector and print them.

dusty909
  • 107
  • 6