Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of (string) literals

Tags:

c

string

literals

I always try to avoid to return string literals, because I fear they aren't defined outside of the function. But I'm not sure if this is the case. Let's take, for example, this function:

 const char * return_a_string(void) {     return "blah"; } 

Is this correct code? It does work for me, but maybe it only works for my compiler (gcc). So the question is, do (string) literals have a scope or are they present/defined all the time.

like image 658
quinmars Avatar asked Nov 05 '08 23:11

quinmars


People also ask

What is the use of string literals?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

Which are the string literals?

A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols. A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .

How many types of string literals are there?

There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an exact value which a string, number, or boolean must have.


2 Answers

This code is fine across all platforms. The string gets compiled into the binary as a static string literal. If you are on windows for example you can even open your .exe with notepad and search for the string itself.

Since it is a static string literal scope does not matter.

String pooling:

One thing to look out for is that in some cases, identical string literals can be "pooled" to save space in the executable file. In this case each string literal that was the same could have the same memory address. You should never assume that it will or will not be the case though.

In most compilers you can set whether or not to use static string pooling for stirng literals.

Maximum size of string literals:

Several compilers have a maximum size for the string literal. For example with VC++ this is approximately 2,048 bytes.

Modifying a string literal gives undefined behavior:

Modifying a string literal should never be done. It has an undefined behavior.

char * sz = "this is a test"; sz[0] = 'T'; //<--- undefined results 

Wide string literals:

All of the above applies equally to wide string literals.

Example: L"this is a wide string literal";

The C++ standard states: (section lex.string)

1 A string literal is a sequence of characters (as defined in lex.ccon) surrounded by double quotes, optionally beginning with the letter L, as in "..." or L"...". A string literal that does not begin with L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type "array of n const char" and static storage duration (basic.stc), where n is the size of the string as defined below, and is initialized with the given characters. A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type "array of n const wchar_t" and has static storage duration, where n is the size of the string as defined below, and is initialized with the given charac- ters.

2 Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

like image 54
Brian R. Bondy Avatar answered Sep 29 '22 07:09

Brian R. Bondy


I give you an example so that your confusion becomes somewhat clear

char *f() { char a[]="SUMIT"; return a; } 

this won't work.

but

char *f() { char *a="SUMIT"; return a; } 

this works.

Reason: "SUMIT" is a literal which has a global scope. while the array which is just a sequence of characters {'S','U','M','I',"T''\0'} has a limited scope and it vanishes as soon as the program is returned.

like image 41
Sumit Trehan Avatar answered Sep 29 '22 08:09

Sumit Trehan