C ++ operator sizeof - pointer to double

I have an unexpected result with the sizeof operator (C ++). In the main class, I have these lines

double * arguments_ = new double(); *arguments_ = 2.1; *(arguments_+1) = 3.45; cout << (sizeof arguments_) << ' ' << (sizeof arguments_[0]) << ' '<< (sizeof arguments_)/(sizeof arguments_[0]); 

which give me the result 4 8 0

The double size is 8 bytes and (sizeof arguments_ [0]) = 8. However, why (sizeof arguments_) is also not expressed in bytes (2 * 8 = 16)? Is the operator sizeof applica

+4
source share
4 answers

Both values ​​are expressed in the same units. You have a 32-bit system, so the address size is 32 bits or 4 bytes. The double size on your system is 8 bytes. The result of integer division 4/8 is zero.

+5
source

Because when you apply the sizeof operator to a pointer no matter what type it points to, you get the size of the space that the pointer occupies.

And in C ++, a pointer variable takes 4 bytes (on architectures with 32-bit address buses).

+2
source

(sizeof arguments_) gives the size of your pointer, which is 4 bytes.

+2
source

You have:

sizeof (pointer) = 4 bytes

sizeof (double) = 8bytes

4/8 = 0 (remeber / equals integer division)

+1
source

Source: https://habr.com/ru/post/1398804/


All Articles