7

On an x86_64 architecture, a pointer is 8 bytes. It makes sense to me that sizeof(x) should return 8. I understand that a char is a single byte, and 5 bytes is the size of array z. What is the intuition behind why sizeof(z) does not return 8?

int* x = new int[10];
char z[5];

// Returns 8
std::cout << "This is the size of x: " << sizeof(x) << std::endl;

// Returns 5
std::cout << "This is the size of z: " << sizeof(z) << std::endl;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tantillo
  • 367
  • 1
  • 5
  • `z` is an array of 5 char. – Eljay Dec 03 '19 at 05:06
  • 4
    `sizeof` is an operator, not a function. So the array of 5 chars doesn't degrade to a pointer when passed to `sizeof`. Or in other words, when an array is passed to `sizeof`, the result is the absolute size of the array in bytes. – selbie Dec 03 '19 at 05:06
  • 3
    The size of z is 5 bytes, not 8 or 16 or 777. What other intuition could be there? – n. m. could be an AI Dec 03 '19 at 05:07
  • Do you know that you can write template functions that keep the size of an array? [see here](https://stackoverflow.com/questions/33234979/how-to-write-a-template-function-that-takes-an-array-and-an-int-specifying-array) –  Dec 03 '19 at 06:37

4 Answers4

4

What is the intuition behind why sizeof(z) does not return 8?

z is not a pointer. Hence sizeof(z) is not anything, but 5 bytes. In case of sizeof, the array doesn't decay to pointer. Refer: What is array decaying?


There are several implicit conversions in C++ like array to pointer, enum to integer, double to float, derived to base, any pointer to void* and so on. Which may lead us to think if their sizes are same or what?
Hence, a litmus test for self understanding is to create a pointer reference & try to assign the other type. It results in error for non matching types. e.g.

int *x = new int[5], *&px = x; // OK
int z[5], *&pz = z; // error: can't initialize
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Is it not the case that an array is a pointer to its first element? – Tantillo Dec 03 '19 at 18:51
  • 1
    @Tantillo, no, it is not the case that an array is a pointer to it's first element. In most cases, such as when indexing or passing to a function, the array is implicitly converted to a pointer, but that does not mean that's what it actually is. – Charlim Dec 03 '19 at 22:26
4

You've defined x as a pointer to char, so sizeof(x) yields the size of a pointer to char. On a current implementation, that'll typically be either 32 bits or 64 bits. A char is typically 8 bits, so you can expect sizeof(char *) to yield 4 or 8 on most current compilers.

You've defined z as an array of 5 char, so sizeof(z) yields the size of an array of 5 char. Since the elements of an array are contiguous, and sizeof(char) is guaranteed to be 1, the obvious value for that would be 5.

If (for example) you put an array of 5 char into a struct, and that's followed by (say) an int, there's a very good chance the compiler will insert some padding between those two elements.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • In the case of an array inside a struct/class, any compiler-added padding is not part of the array itself, it follows the array, so calling `sizeof` for the array will not include the padding. – Remy Lebeau Dec 03 '19 at 06:45
0

Every character have size of '1' Suppose when you compile it

 //It returns 1
      char z='a';
     std::cout << "This is the size of z: " << sizeof(z) << std::endl;
//It returns 5 because it is an array of 5 characters
 char z[5];
std::cout << "This is the size of z: " << sizeof(z) << std::endl;
Wasim
  • 600
  • 2
  • 11
  • 32
  • Here is one more point check it https://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c – Wasim Dec 03 '19 at 05:22
0

When you pass an array name to sizeof, you want to know how many "bytes" of data belong to this array.

When you pass a pointer to sizeof, you want to know how many "bytes" does this pointer occupy.

The difference is very obvious when you pass an array name as a function argument. In this case the function cannot see the whole data area the array occupies. It only see the "pointer" type.

alan23273850
  • 232
  • 2
  • 6