简体   繁体   中英

C null pointer with string literals

Using an ARM, C compiler, I can successfully compile and run the following:

static char * myString = 0;

void myfunc(int x){

   if (x <= 0)
       myString = "Hello World";
   else 
       myString = "This is a different string with a different length";

}

int main(){

    myfunc(-1);
    printf("%s\n", myString);
    myfunc(2);
    printf("%s\n", myString);
}

Why does this work?

Shouldn't the pointer be a NULL pointer?

At the very least, shouldn't the string literal by allocated in a read-only memory location?

EDIT: its a C++ compiler

EDIT2: Why does the string literal exist in static scope, after myfunc has gone out of scope? Are string literals not declared on the stack? And when do they get deallocated?

Thanks!

The two strings ARE allocated in read-only memory and are completely different. But you use one and the same pointer to point to each of them... What's not to understand?

Remember, char* is just a pointer. It is mutable (non-const).

char* p = 0;
p = "Hello"; //OK
p = "Jo" //OK;
p[0] = 'X' //OOPS, now THIS is bad (undefined behavior)

After your edit:

No, string literals have static storage duration (unlike all other literals), they aren't created on stack . They will exist till program termination.

if you were to declare

char const *MyString = 0;  

then you would have troubles as the const pointer cannot be reassigned. String literals are constants.

       .section        .rodata
.LC0:
        .string "Hello World"
        .align 8
.LC1:
        .string "This is a different string with a different length"
        .text

The literal string data is assembled in the read only data section.

Constant strings are generally created in the "data segment" of the program and the pointers to them are always valid (the string "objects" are valid for the program lifetime). So no. The literal strings are not created on the stack.

I am fairly certain the semantics are well-defined in C.

Happy coding.

它是,但是你为它指定了一个指向字符串文字的指针,因此它指向两个字符串数组中的一个。

Initially it is a null-pointer. But you yourself explicitly change that pointer and make it non-null inside myfunc function.

In the first call to myfunc you explicitly make your pointer to point to string literal "Hello World" . After that moment the pointer is, of course, no longer null.

String literals are indeed allocated in a read-only memory location (at least conceptually). However, in both C and C++ you are allowed to point to string literals with char * pointers (ie const char * is not required). It is OK to just point to string literal with char * pointer, as long as you are not trying to modify the literal. Your code does not make any attempts to modify anything, so it is OK.

String literals in C and C++ have static storage duration. So no, they are not allocated "on the stack". They are always allocated in static memory, meaning that they live forever - as long as your program is running.

PS In order to answer your question more completely, you have to explain why on Earth you expect your pointer to remain null.

PPS int main , not void main .

myString是一个指针变量,你故意将它设置为指向存储两个字符串常量之一的内存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM