0

Is there any hidden feature of the sizeof keyword I'm missing here when it's applied to a literal and not a pointer initialized to another literal?

#include <iostream>

int main(int argc, char* argv[]) {
    const char* string = "Hello, World!";

    // sizeof(...) on literal; returns 14 (the actual length)
    std::cout << "sizeof (with literal): " << sizeof("Hello, World!") << std::endl;

    // sizeof(...) on initiated pointer; returns 4 (wonder why?)
    std::cout << "sizeof (with variable): " << sizeof(string) << std::endl;

    return 0;
}

Or is it just my compiler "optimizing" the source?


The question here may seem to answer my question, but only partially.

The answer provided makes note on C++ not being able to pass entire arrays as function parameters and as such receiving array arguments as pointers instead
(Which logically, argument pointers could be of any size).

To which I can guess here that the literal is probably optimized to be a fixed-sized array and the pointer has an arbitrary size during compile-time, but the question is: why?


It's easier to wrap my head around this if the variable had been uninitialized:
char* string; // Arbitrary size that's mutable (usually 4 bytes)

but not in this case scenario when its initiated to a fixed-size literal:
const char* string = "Hello, World!"; // Fixed-size of 14 allocated to variable?

After all the valid characters of the variable had been specified in initiation (which implicitly gives a length by the number of characters initialized), would that not make the variable inherit the allocated size of the literal?

Lapys
  • 936
  • 2
  • 11
  • 27

2 Answers2

2

To which I can guess here that the literal is probably optimized to be a fixed-sized array

A literal is not "optimized" to be a fixed-sized array. A string literal is a fixed-sized array: It is not a matter of optimization.

and the pointer has an arbitrary size during compile-time

It's unclear what you mean by "arbitrary". The size of a data pointer is determined by the target system. On a 64 bit x86 system, the size of a pointer is 8 bytes for example.

const char* string = "Hello, World!"; // Fixed-size of 14 allocated to variable?

What a pointer points to does not affect the size of the pointer.

would that not make the variable inherit the allocated size of the literal?

Only thing that affects the size of a variable is the type of the variable. If the type of the variable is const char*, then size of that variable is exactly same as sizeof(const char*).

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

A pointer is a pointer, and it’s size is the size of a pointer, regardless of what it points at. Initializing a pointer to point at a string literal doesn’t change that — it’s still a pointer.

Don’t conflate arrays and pointers. They are two different things. The name of an array in most contexts decays into a pointer to its first element, and that often confuses beginners. One of the situations where the name of an array does not decay into a pointer is when the name is the argument to the sizeof operator. That’s why sizeof “abcdef” is 7 and not the size of a pointer.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165