简体   繁体   中英

Functioning of sizeof() operator in C++

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,

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 (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM