user1000107
user1000107

Reputation: 415

initialize array passed by pointer

The function cannot initialize an array because sizeof() returns bytes of an int pointer not the size the memory pointed by myArray.

 void assignArray(int *myArray)
 {
     for(int k = 0; k < sizeof(myArray); ++k)
     {
         myArray[k] = k;
     }
 }

Are there other problems ?

Thanks

Upvotes: 0

Views: 269

Answers (3)

Shahbaz
Shahbaz

Reputation: 47493

There are two types of arrays you should be able to distinguish. One looks like this:

type name[count];

This array is of type type[count] which is a different type for each count. Although it is convertable to type *, it is different. One difference is that sizeof(name) gives you count*sizeof(type)

The other type of array looks like this:

type *name;

Which is basically just a pointer that you could initialize with an array for example with malloc or new. The type of this variable is type * and as you can see, there are no count informations in the type. Therefore, sizeof(name) gives you the size of a pointer in your computer, for example 4 or 8 bytes.

Why are these two sizeofs different, you ask? Because sizeof is evaluated at compile time. Consider the following code:

int n;
cin >> n;
type *name = new type[n];

Now, when you say sizeof(name), the compiler can't know the possible future value of n. Therefore, it can't compute sizeof(name) as the real size of the array. Besides, the name pointer might not even point to an array!

What should you do, you ask? Simple. Keep the size of the array in a variable and drag it around where ever you take the array. So in your case it would be like this:

void assignArray(int *myArray, int size)
{
    for(int k = 0; k < size; ++k)
    {
        myArray[k] = k;
    }
}

Upvotes: 0

Peter Alexander
Peter Alexander

Reputation: 54270

Well no, there are no other problems. The problem you stated is the only thing stopping you from initialising the array.

Typically, this is solved by simply passing the size along with the pointer:

void assignArray(int* myArray, std::size_t mySize)
{
    for (std::size_t k = 0; k < mySize; ++k)
        myArray[k] = k;
}

Note that I've used std::size_t for the size because that is the standard type for storing sizes (it will be 8 bytes of 64-bit machines, whereas int usually isn't).

In some cases, if the size is known statically, then you can use a template:

template <std::size_t Size>
void assignArray(int (&myArray)[Size])
{
    for (std::size_t k = 0; k < Size; ++k)
        myArray[k] = k;
}

However, this only works with arrays, not pointers to allocated arrays.

int array1[1000];
int* array2 = new int[1000];
assignArray(array1); // works
assignArray(array2); // error

Upvotes: 6

Mooing Duck
Mooing Duck

Reputation: 66912

I don't see other problems. However, you probably wanted this:

template<int sz>
void assignArray(int (&myArray)[sz])
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

Unless, of course, even the compiler doens't know how big it is at compile time. In which case you have to pass a size explicitly.

void assignArray(int* myArray, size_t sz)
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

If you don't know the size, you have a design error.

http://codepad.org/Sj2D6uWz

Upvotes: 3

Related Questions