Why is a constant string in a static storage zone?
Char * c = "zhaobei ";
The book says: "zhaobei" is treated as a constant and placed in the static memory zone of the program.
The general int I = 1;
1 is also a constant. Why is 1 not placed in the static memory zone of this program?
Please kindly advise!
All character escape constants are placed in the static memory area.
Because string constants rarely need to be modified, putting them in the static memory area will improve efficiency.
Example:
Char str1 [] = "ABC ";
Char str2 [] = "ABC ";
Const char str3 [] = "ABC ";
Const char str4 [] = "ABC ";
Const char * str5 = "ABC ";
Const char * str6 = "ABC ";
Char * str7 = "ABC ";
Char * str8 = "ABC ";
Cout <(str1 = str2) <Endl;
Cout <(str3 = str4) <Endl;
Cout <(str5 = str6) <Endl;
Cout <(str7 = str8) <Endl;
Result: 0 0 1 1
Str1, str2, str3, and str4 are array variables which have their own memory space;
Str5, str6, str7, and str8 are pointers pointing to the same constant area.
Problem introduction:
Let's take a look at the output of the following program:
# Include <stdio. h> Char * returnstr () { Char * P = "Hello world! "; Return P; } Int main () { Char * STR = NULL; // Initialization is required. STR = returnstr (); Printf ("% s \ n", STR ); Return 0; }
|
There is no problem with this, because "Hello world! "Is a String constant stored in the static data area,
Assign the first address of the static data area where the String constant is stored to the pointer,
Therefore, when the returnstr function exits, the memory where the String constant is located will not be recycled, so it can be smoothly accessed through the pointer.
However, the following issues occur:
# Include <stdio. h> Char * returnstr () { Char P [] = "Hello world! "; Return P; } Int main () { Char * STR = NULL; // Initialization is required. STR = returnstr (); Printf ("% s \ n", STR ); Return 0; }
|
"Hello world! "Is a String constant stored in the static data area. That's right,
However, a String constant is assigned to a local variable (char [] array), which is stored in the stack,
In this way, there will be two memories with the same content,
That is to say,"
char p[]="hello world!";
"This statement makes" Hello world !" This string has two copies in the memory, one in the dynamically allocated stack and the other in the static storage area.This is the most essential difference from the former,
When the returnstr function exits, the stack needs to be cleared, and the memory of local variables is also cleared,
Therefore, the function returns a released memory address, so garbled characters are printed.
If the return value of a function is not the address of a local variable, the local variable must be declared as static. As follows:
#include <stdio.h> char *returnStr() { static char p[]="hello world!"; return p; } int main() { char *str=NULL; str=returnStr(); printf("%s\n", str); return 0; }
|
This problem can be better explained through the following example:
# Include <stdio. h> // The returned address is the address of the local variable, which is located in the dynamic data zone and in the stack.
Char * S1 () { Char * P1 = "qqq"; // to test' char p[]="Hello world!" 'Is there a copy of the string in the static storage area? Char P [] = "Hello world! "; Char * P2 = "W "; // To test' char p[]="Hello world!" 'Is there a copy of the string in the static storage area?
Printf ("in S1 P = % P \ n", P ); Printf ("in S1 p1 = % P \ n", P1 ); Printf ("in S1: String's address: % P \ n", & ("Hello world! ")); Printf ("in S1 P2 = % P \ n", P2 ); Return P; }
// Return the address of the String constant, which is located in the static data zone.
Char * S2 () { Char * q = "Hello world! "; Printf ("in S2 q = % P \ n", q ); Printf ("in S2: String's address: % P \ n", & ("Hello world! ")); Return Q; }
// The address of the static local variable is returned. The address is located in the static data zone.
Char * S3 () { Static char R [] = "Hello world! "; Printf ("in S3 r = % P \ n", R ); Printf ("in S3: String's address: % P \ n", & ("Hello world! ")); Return R; }
Int main () { Char * T1, * t2, * T3; T1 = S1 (); T2 = S2 (); T3 = S3 (); Printf ("in main :"); Printf ("P = % p, q = % P, R = % P \ n", T1, T2, T3 );
Printf ("% s \ n", T1 ); Printf ("% s \ n", T2 ); Printf ("% s \ n", T3 ); Return 0; }
|
Output result:
in s1 p=0013FF0C in s1 p1=00431084 in s1: string‘s address: 00431074 in s1 p2=00431070 in s2 q=00431074 in s2: string‘s address: 00431074 in s3 r=00434DC0 in s3: string‘s address: 00431074 in main:p=0013FF0C, q=00431074, r=00434DC0 $ Hello world! Hello world!
|
This result proves the above explanation. At the same time, we can draw a conclusion:
A String constant is called a constant because it can be regarded as an unnamed string and is a constant and stored in the static data zone.
The static data zone mentioned here is relative to the dynamic data zone such as heap and stack.
The static data area stores global and static variables. In this regard, string constants can also be called an unknown static variable,
Because "Hello world! "This string is referenced in functions S1 and S2, but there is only one copy in the memory, which is similar to static variables.
Why is a constant string in a static storage zone?