What is the existence of a String constant in C?

Source: Internet
Author: User

Summary of constant Storage
Local variables, static local variables, global variables, Global static variables, string constants, and dynamically applied memory Zone

1. Local variables are stored in the stack.
2. Global variables and static variables (global and local static variables) are stored in the static storage area.
3. The new memory is in the heap.
4. string constants are also stored in the static storage area.

Note:
1. The variable memory in the stack will be automatically released with the completion of the defined swap zone; For the heap, manual free is required; otherwise, it will exist until the end of the program;
2. For the static storage area, the variable constants will always exist during the program running and will not be released, and the variable constants have only one copy, there will be no different copies of the same variables and constants.

========================================================== ========
The following is a clear explanation of string constants:

Char * c = "zhouming ";
The book says: "zhouming" is treated as a constant and placed in the static memory zone of the program.
C is a balanced pointer. If it is a local variable, it is stored in the stack. the pointer variable contains an address,

The address is the address of Z, the first letter in the string.

 

When printf () is used for output, % s is selected for formatting, and zhouming is output. This is when printf encounters the ending symbol '\ 0', it stops printing.

 

The string "zhouming" is a constant that is stored in a continuous memory. A Terminator at the end of the string indicates the end of the string.

 

The general int I = 1;


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 [] = "abcd ";
Char str2 [] = "abcd ";

Const char str3 [] = "abcd ";
Const char str4 [] = "abcd ";

Const char * str5 = "abcd ";
Const char * str6 = "abcd ";

Char * str7 = "abcd ";
Char * str8 = "abcd ";


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. Character arrays are stored in the stack as local variables;
Str5, str6, str7, and str8 are pointers pointing to the same constant area ., "Abcd" is stored in the static data zone and is global,

 

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 are two pieces of memory with the same content, that is, "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:

Static is mainly used to limit the scope.

# 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.

 

There is also an experiment:

 

 

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Int main ()
{
 
Char * B;
 
Char * c;
 
Char a [] = "chenhuijie ";
B = "chenhuijie ";
C = "chenhuijie ";
Printf ("% d, % d \ n", B, a, c, & ("chenhuijie "));
 
 
 
 
 
}

# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Int main ()
{

Char * B;

Char * c;

Char a [] = "chenhuijie ";
B = "chenhuijie ";
C = "chenhuijie ";
Printf ("% d, % d \ n", B, a, c, & ("chenhuijie "));

 

 

}

 

The experiment result is:

4282272,1244988, 4282272,4282272
Press any key to continue...

 

By the way, the character constant 'A'

Sizeof ('A ')

It must be 4 characters long.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.