Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are string literals guranteed to be adjacent to each other in memory?

Tags:

c

Is it guranteed that the string literals are stored adjacent in the memory?

Therefore does the below necessarily get an output hellohello

printf(3 + "%d");
printf("hello");
like image 237
phoxis Avatar asked Apr 21 '12 20:04

phoxis


1 Answers

No, string literals are stored wherever your compiler fancies. The fact that you declared two of them in consecutive lines is irrelevant. You can make no assumptions about where the compiler will store them.

The compiler can do all sorts of things. For example, if you write the following code

printf("hello");
printf("hello");

then the compiler is perfectly at liberty to create only a single literal. Or not.

like image 177
David Heffernan Avatar answered Nov 15 '22 16:11

David Heffernan