lmao samoara
lmao samoara

Reputation: 33

Why does sizeof(*value) returns different value than sizeof(value) in c++

I'm curious about this one:

int main()
{
    int a[7];
    std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << " - " << sizeof(a) << " - " << sizeof(*a) << std::endl;
}

Returns

Length of array = 7 - 28 - 4

So my question is: Why do I have to use (sizeof(a)/sizeof(*a)) to receive the right size, and why does sizeof(*a) returns 4, but sizeof(a) returns 28?

Upvotes: 1

Views: 2042

Answers (5)

Xiongmao
Xiongmao

Reputation: 1

a decays to a pointer when it is passed to a function, used in an operator, or pretty much used like a variable in any way. *a is referring to what the decayed pointer points to. In other words, a decays to an int* which points to the first value in a, while *a is an int, which is why sizeof is returning different sizes with the two data types.

Upvotes: 0

Jacob Blomquist
Jacob Blomquist

Reputation: 276

sizeof(a) returns 28 because you have 7 elements in the array, and each element is 4 bytes long.

sizeof(*a) returns 4 because *a dereferences the first element in the array (an integer) which is 4 bytes long.

Thus, sizeof(a) / sizeof(*a) == 28/4 == 7

EDIT: You shouldn't make a habit of finding the length of an array this way in c++. It is much better to keep another variable around which stores the length of the array. sizeof is computed at compile-time with fixed-length arrays. If you call sizeof on a variable length array, it will usually return 8 as this is the size of a pointer (on 64 bit systems). See here for more information on this.

Upvotes: 4

Jarod42
Jarod42

Reputation: 217065

a, and *a have different type; int[7] and int respectively (ignoring possible reference as l-value).

We have:

  • sizeof(a[0]) = sizeof(*a) = sizeof (int)
  • sizeof(a) = sizeof (int[7]) = 7 * sizeof (int)
  • sizeof(a)/sizeof(*a) = std::extent<int[7]>::value = 7

It appears that, for your configuration, sizeof(int) == 4.

Upvotes: 2

user13602601
user13602601

Reputation:

By the statement int a[7], you are declaring an array of integers of size 7 (Only 7 integer elements can be stored here). If you use the statement sizeof(a), you get the size of a. As you probably may know, it is 7 x 4. (Why 4? Because the size of an integer is 4 bytes). Therefore, if you need to get the real size of the array (i.e. the number of elements), you must divide it with the size of int.

Why do I have to use (sizeof(a)/sizeof(*a)) to receive the right size, and why does sizeof(*a) returns 4, but sizeof(a) returns 28?

To answer this, you must be aware that the array name is actually a pointer to the 1st element of the array (i.e. In this case, it points at a[0].) So, what is a[0]? It is an integer right? You can think of sizeof(*a) as sizeof(int). Since sizeof(int) returns 4, so will sizeof(*a). The reason why you get 7 (the correct answer) is because: sizeof(a) = 7 * 4, sizeof(*a) = 4, therefore (sizeof(a)/sizeof(*a)) = 7.

Upvotes: 0

EhsanFld
EhsanFld

Reputation: 1

The name of an array decays into a pointer to the first element.

*a == a[0]
*(1+a) == a[1]

And so on.

So, an integer pointer is 4 bytes long, just like an int variable.

On the other hand, the a array is an integer array of length 7, so the overall size is 7 * 4 = 28

Upvotes: 0

Related Questions