Where does the string constant exist in C language?

Source: Internet
Author: User

Constant Storage Summary
Local variables, static local variables, global variables, global static variables, string constants, and memory areas for dynamic requests

1, local variables stored in the stack
2, global variables, static variables (global and local static variables) stored in the static storage area
3. New application memory is in the heap
4, string constants are also stored in the static storage area

Additional notes:
1, the variable memory in the stack is automatically released as the end of the defined interval, and for the heap, manual free is required, otherwise it will persist until the program ends;
2, for the static storage area, where the variable constant will persist during the program run, will not be released, and the variable constant in which there is only one copy, there will be no different copies of the same variable and constant.

===============================================
For string constants, here's an article that explains it clearly:

Char *c= "zhouming";
The book says: the string "zhouming" is treated as a constant and is placed in the memory static area of the program.
c is a character 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 the first letter z in the string.

When you use the printf () output, when you format the%s, the output is zhouming, which is when printf encounters the end symbol ' \ s ', which stops the display printing.

The string "zhouming" is a constant, stored in contiguous memory with a trailing character at the end of the string.

that general int i=1;


All character channeling constants are placed in the static memory area
Because string constants rarely need to be modified, placing them in the static memory area increases efficiency

Cases:

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;

The result is: 0 0 1 1
STR1,STR2,STR3,STR4 are array variables, they have their own memory space, and character arrays are stored as local variables in the stack area;
Whereas str5,str6,str7,str8 are pointers, they point to the same constant area. , "ABCD" is stored in the static data area, and is global,

Introduction of the problem:
Look at the output of the following program:

#include <stdio.h>
Char *returnstr ()
{
Char *p= "Hello world!";
return p;
}
int main ()
{
Char *str=null;//must be initialized, good habit
Str=returnstr ();
printf ("%s\n", str);

return 0;
}

This has no problem, because "Hello world!" is a string constant, stored in the static data area,
Assigns 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 of the string constant is not recycled, so it can be accessed smoothly through pointers.

However, the following is problematic:
#include <stdio.h>
Char *returnstr ()
{
Char p[]= "Hello world!";
return p;
}
int main ()
{
Char *str=null;//must be initialized, good habit
Str=returnstr ();
printf ("%s\n", str);

return 0;
}


"Hello world!" is a string constant, stored in the static data area, yes,
However, a string constant is assigned to a local variable (char [] type array), which is stored in the stack,
This will have two pieces of memory, meaning "char p[]=" Hello world! ";" This statement makes "Hello world!" This string has two copies in 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 is emptied, and the local variable's memory is emptied.
So the function returns a memory address that has been freed, so the print is garbled.

If the return value of a function is not the address of a local variable, then the local variable must be declared as a static type. As follows:

Static is mainly used to limit the range.

#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 by one of the following examples:

#include <stdio.h>
The address of the local variable is returned, which is located in the Dynamic Data area,

Char *s1 ()
{
char* p1 = "QQQ";//In order to test ' char p[]= ' Hello world! "' Whether the string in the static store also has a copy
Char p[]= "Hello world!";
char* P2 = "W";//In order to test ' char p[]= ' Hello world! ' Whether the string in the static store also has a copy
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;
}

Returns the address of a string constant that is located in the static data area

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, which is located in the static data area

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


Run the 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!

The result is a proof of the above explanation, and at the same time, a conclusion is drawn:
A string constant, which is called a constant, because it can be regarded as an unnamed string and a constant, stored in a static data area.
The static data area is relative to the dynamic data area such as heap and stack.
The static data area holds the global variables and static variables, and from this point, the string constants can also be called a nameless static variable.
Because "Hello world!" This string is referenced in both the function S1 and S2, but there is only one copy in memory, which is quite similar to the nature of the static variable.

In addition, there is 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,%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,%d,%d\n",b,a,c,& ("Chenhuijie"));

}

The experimental results are as follows:

4282272,1244988,4282272,4282272
Please press any key to continue ...

Yes, character constant ' a '

sizeof (' a ')

is 4, and a word length.

Where does the string constant exist in C language?

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.