开发者

C null pointer with string literals

开发者 https://www.devze.com 2023-02-17 23:54 出处:网络
Using an ARM, C compiler, I can successfully compile and run the following: static char * myString = 0;

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);
}

W开发者_如何学JAVAhy 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.


It is, but then you assign a pointer to the string literal to it, so it points to one of the two string arrays.


myString is a pointer variable, you are deliberately setting it to point to the memory where one of your two string constants are stored.


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 (i.e. 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.

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

P.P.S. int main, not void main.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号