Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do sizeof(arr) / sizeof(arr[0]) work?

Tags:

c++

sizeof

When looking for a size of an array in a for loop I've seen people write

int arr[10]; for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){} 

How is sizeof(arr) / sizeof(arr[0]) the length of the array? How does it technically work?

like image 426
Felix Willis Avatar asked Nov 04 '15 13:11

Felix Willis


People also ask

How does sizeof array work?

The sizeof() operator returns pointer size instead of array size. The 'sizeof' operator returns size of a pointer, not of an array, when the array was passed by value to a function. In this code, the A object is an array and the sizeof(A) expression will return value 100. The B object is simply a pointer.

What does Arr 0 mean in C?

How to access element of an array in C. You can use array subscript (or index) to access any element stored in array. Subscript starts with 0, which means arr[0] represents the first element in the array arr. In general arr[n-1] can be used to access nth element of an array.

What is int n sizeof A )/ sizeof a 0?

meaning of sizeof(a) and sizeof(a[0]) sizeof(a) is the size (in bytes) of its operand, in this case, an array called a . sizeof(a[0]) is the size (in bytes) of its operand, in this case, a single element of the array. ... to calculate the number of elements in an array.


2 Answers

If you have an array then sizeof(array) returns the number of bytes the array occupies. Since each element can take more than 1 byte of space, you have to divide the result with the size of one element (sizeof(array[0])). This gives you number of elements in the array.

Example:

std::uint32_t array[10];  auto sizeOfInt = sizeof(std::uint32_t); // 4 auto numOfBytes = sizeof(array); // 10*sizeOfInt = 40 auto sizeOfElement = sizeof(array[0]); // sizeOfInt = 4 auto numOfElements = sizeof(array) / sizeof(array[0]); // numOfBytes / sizeOfElement = 40 / 4 = 10 

LIVE EXAMPLE

Note that if you pass an array to a function, the above won't work since the array decays to a pointer and sizeof(array) returns the size of the pointer.

std::size_t function(std::uint32_t a[]) // same for void function(std::uint32_t a[10]) {     return sizeof(a); // sizeof(std::uint32_t*)! }  std::uint32_t array[10]; auto sizeOfArray = function(array); // array decays to a pointer inside function() 

LIVE EXAMPLE #2

like image 153
rozina Avatar answered Sep 18 '22 21:09

rozina


As it is described in the C++ Standard (5.3.3 Sizeof)

1 The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

In this expression

sizeof(arr) / sizeof(arr[0]) 

there are used two subexpressions with the sizeof operator.

This subexpression

sizeof(arr) 

yields the number of bytes occupied by array arr (I suppose that arr is an array).

For example if you declared an array like

int arr[10]; 

then the compiler has to reserve memory that to hold 10 elements of type int. If for example sizeof( int ) is equal to 4 then the compiler will reserve 10 * 4 = 40 bytes of memory.

Subexpression

sizeof(arr[0]) 

gives the number of bytes occupied by one element in the array. You could use any index as for example

sizeof(arr[1000]) 

because the expression is unevaluated. It is only important the size in bytes of the object (an element of the array) used inside the operator.

Thus if you know the total bytes that were reserved for an array

sizeof(arr) 

and know how many bytes each element of the array occupies (all elements of an array have the same size) then you can calculate the number of elements in the array by using the formula

sizeof(arr) / sizeof(arr[0]) 

Here is a simple relation. If you have an array of N elements of type T

T arr[N]; 

and you know the size of the memory occupied by the array then you can calculate the size of its element by using formula

sizeof( arr ) / N == size of an element of the array.  

And vice verse

If you know the size of the memory occupied by the array and the size of its element you can calculate the number of elements in the array

sizeof( arr ) / sizeof( a[0] ) == N - number of elements in the array 

The last expression you can rewrite also the following way

sizeof( arr ) / sizeof( T ) == N - number of elements in the array 

because the elements of the array have type T and each element of the array occupies exactly the number of bytes that are required to allocate an object of type T.

Take into acccount that usually beginners make such an error. They pass an array as an argument to a function. For example let's assume that you have a function

void f( int a[] ) {    // ... } 

And you pass to the function your array

int arr[10]; f(arr); 

then the function uses the pointer to the first element of the array. In fact the function has declaration

void f( int *a ) {    // ... } 

So if you write for example within the function

void f( int *a ) {    size_t n = sizeof( a ) / sizeof( a[0] );    // ... } 

then as a within the function is a pointer (it is not an array) then you will get something like

void f( int *a ) {    size_t n = sizeof( int * ) / sizeof( int );    // ... } 

Usually the size of a pointer equal to either 8 or 4 bytes depending of the used environment. And you won't get the number of elements. You will get some weird value.

like image 35
Vlad from Moscow Avatar answered Sep 17 '22 21:09

Vlad from Moscow