If you didn't do the malloc
, it would still give you 8. Because it's not related to the malloc at all!
sizeof
is a compile-time constant that tells you size needed to store instance of the type of argument. Type of argument is int *
and your compiler apparently compiles for 64-bit, so pointer takes 8 bytes.
Note: In C++, sizeof
is always compile-time constant. In C there is a special case for variable length arrays where the actual runtime size of the array is returned. But C++ does not have variable length arrays, so you don't have to bother with this special case.
Another note: In C++, you should be using vector
and other collections and if all else fails (there is no make_unique
, unfortunately) new
, but usually not malloc
.