I wrote a simple program in order to understand the functioning of the function of the standard c++ library sizeof().
It follows:
const char* array[] = {
"1234",
"5678"
};
std::cout << sizeof(array) << std::endl;//16
std::cout << sizeof (array[0]) << std::endl;//8
std::cout << printf("%lu\n",sizeof (char) );//1
std::cout << printf("%lu\n",sizeof (int) );//24
std::cout << printf("%lu\n",sizeof (float) );//24
std::cout << printf("%lu",sizeof (double) );//281
It is possible to see by the output reported the characters has dimension 1 byte in my OS, as expectable. But I do not understand why the dimension of '''array[0]''' is 8, as it contains 4 charcaters and at least other 2 charcaters for the end sequence "\n" which is contained in a string. Thus, I supposed that the number of bytes occupied by the first element of the array should be 6 and not 8. Moreover, if I increase/decrease the number of charcaters contained in the first element of the array, the its size does not change. Clearly, I am wrong. If somebody can explain me this functioning, I would really appreciate. Thanks,
sizeof
on a pointer returns the size of the pointer, not what the pointer might point to.std::cout << printf...
you are printing the output of printf and the return value of printf.printf
is printing the sizeof with newlines and then additionallystd::cout
prints the return value ofprintf
printf
format for the typesize_t
(which is whatsizeof
returns) is%zu
. And why are you even usingprintf
here? And why do you print the return-value ofprintf
?