Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the data type of a string literal in C++?

Similar questions have been asked about the data type of string literals in C++.

Many people have cited the standard:

A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7)

I've written the following statement in the main function:

  char cstring[]= "hellohellohellohellohellohello";

But I can't find any string literal stored as static data in the assembly. In fact, the assembly shows that the string is decomposed and "stored" directly in the instructions.

    movl    $1819043176, -48(%rbp)
    movl    $1818585199, -44(%rbp)
    movl    $1701343084, -40(%rbp)
    movl    $1752132716, -36(%rbp)
    movl    $1869376613, -32(%rbp)
    movl    $1819043176, -28(%rbp)
    movl    $1818585199, -24(%rbp)
    movw    $28524, -20(%rbp)
    movb    $0, -18(%rbp)

While a similar statement in the global scope has as a result the string stored as static data.

char cstring1[] = "hellohellohellohellohellohello";

The assembly

cstring1:
    .string "hellohellohellohellohellohello"

The above example is available online here.

So this seems not conform to the cited standard. Maybe there are some exceptions to what is cited here?

like image 303
Gab是好人 Avatar asked Jul 25 '16 12:07

Gab是好人


People also ask

What data type is string literal?

String literals are specified by one or more characters enclosed in single quotes. The default data type for string literals is varchar, but a string literal can be assigned to any character data type or to money or date data type without using a data type conversion function.

Is a string literal an array?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'.

What is string literal example?

A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' 'He said, "Take it or leave it."'

Is a string literal or variable?

A string literal is where you specify the contents of a string in a program. Here 'A string' is a string literal. The variable a is a string variable, or, better put in Python, a variable that points to a string.


2 Answers

It does conform to the standard, under the "as-if" rule.

Since the only thing that the string literal is ever used for is to initialize cstring, there is no need for any object representation for it. The compiler has eliminated it in favour of initializing cstring by an alternative means that has equivalent results, but that the compiler decides is better in some respect (speed or code size).

like image 121
Steve Jessop Avatar answered Sep 30 '22 03:09

Steve Jessop


Expressions have type. String literals have type if they are used as an expression. Yours isn't.

Consider the following code:

#include <stdio.h>

#define STR "HelloHelloHello"

char global[] = STR;

int main(void)
{
    char local[] = STR;
    puts(STR);
}

There are three string literals in this program formed using the same tokens, but they are not treated the same.

The first, the initializer for global, is part of static initialization of an object with static lifetime. By section 3.6.2, static initialization doesn't have to take place at runtime; the compiler can arrange for the result to be pre-formatted in the binary image so that the process starts execution with the data already in place, and it has done so here. It would also be legal to initialize this object in the same fashion as local[], as long as it was performed before the beginning of dynamic initialization of globals.

The second, the initializer for local, is a string literal, but it isn't really an expression. It is handled under the special rules of 8.5.2, which states that the characters within the string literal are independently used to initialize the array elements; the string literal is not used as a unit. This object has dynamic initialization, resulting in loading the value at runtime.

The third, an argument to the puts() call, actually does use the string literal as an expression, and it will have type const char[N], which decays to const char* for the call. If you really want to study object code used to handle the runtime type of a string literal, you should be using the literal in an expression, like this function call does.

like image 32
Ben Voigt Avatar answered Sep 30 '22 02:09

Ben Voigt