Sizeof String Literal

Sizeof string literal

  1. sizeof("f") must return 2, one for the 'f' and one for the terminating '\0'.
  2. sizeof(foo) returns 4 on a 32-bit machine and 8 on a 64-bit machine because foo is a pointer.
  3. sizeof(bar) returns 2 because bar is an array of two characters, the 'b' and the terminating '\0'.

The string literal has the type 'array of size N of const char' where N includes the terminal null.

Remember, arrays do not decay to pointers when passed to sizeof.

Sizeof string vs sizeof string pointer

The size of a pointer is always the size of the pointer itself, not what it points to. That's because sizeof is mostly a compile-time operator (the result is evaluated by the compiler) and the compiler can't know what a pointer might point to at run-time.

As for sizeof *title it's the same as sizeof title[0] which is a single char. And the size of a char is 1 (it's specified to always be 1 by the way, no matter the actual bit-width).

Lastly about sizeof "VP". In C all literal strings are really arrays of characters, including the terminating null character. So the literal string "VP" is an array of three characters, hence its size is 3.


To make the answer a little bit more complete, I say that the sizeof operator is mostly compile-time. That of course can't be true for variable-length arrays, where the compiler must insert code to store the actual size of the array in a way that it can be fetched at run-time. If the array decays to a pointer, then all you have is the pointer and again sizeof returns the size of the pointer itself.

And a note about string literal arrays. While they are technically non-constant arrays, they still can't be modified. Attempting to modify a string literal leads to undefined behavior. Literal strings are thus, in effect, read-only.

Determining the Length of a String Literal

If you want the number computed at compile time (as opposed to at runtime with strlen) it is perfectly okay to use an expression like

sizeof "A really large text message that "
"is spread over multiple lines";

You might want to use a macro to avoid repeating the long literal, though:

#define LONGLITERAL "A really large text message that " \
"is spread over multiple lines"

Note that the value returned by sizeof includes the terminating NUL, so is one more than strlen.

I am a beginner in programming in c,need help in sizeof() string constant?

The string literal "a" has the type char[2]. You can imagine it like the following definition

char string_literal[] = { 'a', '\0' };

sizeof( char[2] ) is equal to 2 because (The C standard, 6.5.3.4 The sizeof and alignof operators)

4 When sizeof is applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the result is
1.

A character constant in C indeed has the type int. So for example sizeof( 'a' ) is equal to sizeof( int ) and usually equal to 4.

But when an object of the type char is initialized by a character constant like this

char c = 'a';

an implicit narrowing conversion is applied.

Size of character strings

A string literal is actually an array, not a pointer. Section 6.4.5p6 of the C standard regarding string literals states:

In translation phase 7, a byte or code of value zero is
appended to each multibyte character sequence that results from a
string literal or literals. The multibyte character sequence is
then used to initialize an array of static storage duration
and length just sufficient to contain the sequence.

So when it is the operand of the sizeof operator, the usual decay to a pointer does not happen and you get back the size of the array in bytes.

The behavior of array decay into a pointer is documented in section 6.3.2.1p3:

Except when it is the operand of the sizeof operator, the
_Alignof operator, or the unary & operator, or is a string literal
used to initialize an array, an expression that has type ‘‘array
oftype’’ is converted to an expression with type ‘‘pointer to type’’
that points to the initial element of the array object and
is not an lvalue. If the array object has register storage
class, the behavior is undefined.

Size of string literal consisting of escaped characters

This

"\n\r\t"

is a so-called string literal. It is stored in memory as a constant character array with terminating zero. Each escape character is one character.

So this string literal has three explicitly specified characters plus the terminatimg zero. In total there are four characters in the literal.

As for function strlen then it does not take into account the terminating zero. So it will report only three characters that were specified explicitly in the string literal.

The function strlen uses the terminating zero as the mark where it shall stop to count characters in a string.

As for the operator sizeof then it returns total memory in bytes occupied by an object. As your string literal has type const char[4] then sizeof will return 4. It is the total memory in bytes occupied by the string literal.

Compile time size of string literal in array of string literals

Actually the advice in the linked answer is wrong. as it has the indexes reversed. The declaration should be more along the lines of this:

static const char header_left[][40] =
{
" | | Raw | Raw |",
" | | Start | End |",
"Interval#| Duration | Point | Point |",
"---------+----------+-------+-------+",
};

The left-most index can still be provided by the compiler, and indicates the count of strings. The strings themselves must be a fixed char array, which you could provide an upper bound on (40, in this example). You'll get a compile-error if any string exceeds that length (including null terminator). The potential downside for your purposes is wasted space.

In any event, you can't have the compiler deduce both sizes for you - that of both arrays - and jagged arrays aren't supported in C++.



Related Topics



Leave a reply



Submit