Sizeof an Array Passed as Function Argument

Find the Size of integer array received as an argument to a function in c

You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.

If you want to know the size of an array passed to the function, you need to work it out before decay and pass that information with the array, something like:

void function (size_t sz, int *arr) { ... }
:
{
int x[20];
function (sizeof(x)/sizeof(*x), x);
}

Using sizeof() on an array passed to a function

Array in C always passed by reference. Thats why you are getting pointer size each time, not actual size.

To work with array in C as an argument, you should pass size of array with array.

I modified your program to working condition:

typedef unsigned char BYTE;

void checkArraySize(BYTE data[], int sizeOfArray)
{
int internalSize = sizeOfArray;
printf("%d", internalSize );
}

void main(void)
{
BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
int externalSize = sizeof(data)/sizeof(BYTE); //it would return number of elements in array

checkArraySize(data, externalSize);
}

passed by reference means only address of first element of array is sent. If you change anything even inside function checkArraySize, this change would be reflected to original array too. Check modified above example.

typedef unsigned char BYTE;

void checkArraySize(BYTE data[])
{
int internalSize = sizeof(data);
printf("%d\n", internalSize );
data[3]= 0x02; //internalSize is reported as 4
}

void main(void)
{
BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
int externalSize = sizeof(data); //externalSize is reported as 8
printf("Value before calling function: 0x%x\n",data[3]);
checkArraySize(data);
printf("Value after calling function: 0x%x\n",data[3]);
}

output would be:

Value before calling function: 0x4
4
Value after calling function: 0x2

Sizeof array passed as parameter of function

In most cases the size of a pointer is 8 bytes. In your system that is the size of the pointer.

Note that the size of the pointer, and you probably know this, is not the size of the object it points or the size of the type of object.

The size of a pointer depends on several factors, like the CPU architecture, compiler or Operating System.

The way it usually works is if the system is 16-bit, the of size pointers is 2 bytes, if the system is 32-bit, the size is 4 bytes, if it is 64-bit, it's 8 bytes.

Using sizeof on arrays passed as parameters

What causes this inconsistency?

The name of the array decays as an pointer to its first element.

When you pass an array to an function, this decaying takes place and hence the expression passed to sizeof is a pointer thus returning pointer size.

However, an array passed to sizeof always returns size of the array because there is no decaying to an pointer in this case.

What is the best way of getting the size of an array when it is passed as a parameter?

Don't pass them by value, pass them by reference or

Pass size as an separate argument to the function.

When passing an array to a function in C++, why won't sizeof() work the same as in the main function?

The problem is here:

int length_of_array(int some_list[]);

Basically, whenever you pass an array as the argument of a function, no matter if you pass it like int arr[] or int arr[42], the array decays to a pointer (with ONE EXCEPTION, see below), so the signature above is equivalent to

int length_of_array(int* some_list);

So of course when doing sizeof(some_list)/sizeof(*some_list) you will get the ratio between the size of the pointer the array decayed to and the size of the type representing the first element. In your case, 1, as it looks like on your machine the size of a pointer is probably 4 bytes (32 bits), same as the size of an int.

So my C++ instructor told us in class that there was no function to determine an array size in C++ and I was not satisfied with that.

YOUR TEACHER IS WRONG! There is a way of passing an array by reference and getting its size:

template<size_t N>
int length_of_array(int (&arr)[N])
{
std::cout << N << std::endl; // WORKS!
return N;
}

The length of array changes after passing it a function

int passing_array(int array[]) {
int size = sizeof(array);
printf("The size after passing: %d\n", size);
}

is the same as

int passing_array(int* array) {
int size = sizeof(array);
printf("The size after passing: %d\n", size);
}

Hence, size is the size of a pointer.

C sizeof a passed array

There is no magic solution. C is not a reflective language. Objects don't automatically know what they are.

But you have many choices:

  1. Obviously, add a parameter
  2. Wrap the call in a macro and automatically add a parameter
  3. Use a more complex object. Define a structure which contains the dynamic array and also the size of the array. Then, pass the address of the structure.

C - passing array in function an get its size

In C you must pass in not only the array, which decays to a pointer, but the size of the array as well. In C the common convention is (array, size):

void print_array(int a[], size_t s) {
for (size_t i = 0; i < s; ++i) {
... a[i] ...
}
}

Where you call it like:

print_array(a, 5);


Related Topics



Leave a reply



Submit