0

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,

4
  • 4
    sizeof on a pointer returns the size of the pointer, not what the pointer might point to. Commented Oct 1, 2019 at 8:24
  • 4
    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 additionally std::cout prints the return value of printf
    – KamilCuk
    Commented Oct 1, 2019 at 8:25
  • 3
    Also note that the correct printf format for the type size_t (which is what sizeof returns) is %zu. And why are you even using printf here? And why do you print the return-value of printf? Commented Oct 1, 2019 at 8:25
  • 2
    Double on your system is reallllllly 281! Wow :-)
    – Klaus
    Commented Oct 1, 2019 at 8:26

2 Answers 2

6

I wrote a simple program in order to understand the functioning of the function of the standard c++ library sizeof().

Wrong terminology. Please read n3337 (a C++ standard) and the wikipage on sizeof.

sizeof is a compile-time operator, not a function. If v is some variable, sizeof(v) only depends on the type of v and never on its value (in contrast, for most functions f, the value of f(v) depends upon the value of v).

And a good way to understand something about C++ is to refer to documents like standards or good web pages about it.

If somebody can explain me

Yes. Read a good book about C++. This one is written by the main designer of C++. Try to understand more and better the (difficult) semantics of C++. You could also study the source code of existing open source C++ compilers such as GCC or Clang/LLVM (thus effectively using one of your free software freedoms).

BTW, with a lot of pain you might find C++ implementations with sizeof(int) being 1 (e.g. for some DSP processors). On cheap 32 bits ARM processors (those in cheap mobile phones today, for instance; then you would probably use some cross-compiler) or on some Raspberry Pis (or perhaps some mainframes) you could have sizeof(array[0]) or sizeof(void*) being 4 even in 2019.

7
  • 1
    I don't think remarks like "Read a good book about C++" are relevant for "answers!" Such are OK in comments - but if you post an answer, then make it an answer, not a lecture! Commented Oct 1, 2019 at 8:53
  • 1
    I don't have time to write many pages about sizeof and if I did I am sure people would find my answer too long. The "someone" I am refering to is called B.Stroustrup. And I gave two relevant hyperlinks before mentioning that book Commented Oct 1, 2019 at 8:54
  • 1
    I can understand @BasileStarynkevitch hints how to go forward after the answer and avoid just a series of follow up questions and how to learn are making sense to me. He still wrote an quality answer to the question. Commented Oct 1, 2019 at 8:59
  • My answer might not be decent (but what would be an indecent answer? Something with pornography ??) but I am trying hard to make it correct. Remember that I am not a native English speaker Commented Oct 1, 2019 at 9:03
  • I'm not sure if suggesting a complete newbie to go read source code of GCC or LLVM is a good advice.
    – patentfox
    Commented Oct 1, 2019 at 9:16
2

Let's break down the meaning of the somewhat confusing output values you see!

First, the sizeof(array) and sizeof(array[0]) (where your output method is fine). You have delared/defined array as an array of two char* values, each of which is a pointer. The size of a pointer on your system is 8 bytes, so the total size of array is: 8 * 2 = 16. For array[0]: this is a single pointer, so its size is simply 8 bytes.

Does all this make sense so far? If so, then let's look at the second part of your code …

The values for sizeof(char), sizeof(int), sizeof(float) and sizeof(double) are, on your system, in order, 1, 4, 4, and 8. These values are actually being output! However, as you are also outputting the return value of printf(), which is the number of characters it has written, you are getting the extra values, "2", "2", "2" and "1" inserted (in a confusing, and possibly undefined, order), for the four calls (the last one has no newline, so it's only one character; all others are one digit + newline = 2 characters).

Change the second part of your code as follows, to get the correct outputs:

printf("%zu\n", sizeof(char));   //1
printf("%zu\n", sizeof(int));    //4
printf("%zu\n", sizeof(float));  //4
printf("%zu\n", sizeof(double)); //8
3
  • You could have C++ implementations where sizeof(int) is 1. I think some DSP processors have that. And on some machines from the previous century with some C++ compiler, you had sizeof(int) == 2 Commented Oct 1, 2019 at 8:43
  • @BasileStarynkevitch But not on the OP's system! My answer was specific to his/her question, not a general statement on C++ implementations. Commented Oct 1, 2019 at 8:45
  • 1
    this is still not correct. You must use %zu or UB will happen
    – phuclv
    Commented Oct 1, 2019 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.