C + + string constants

Source: Internet
Author: User

string constants. It is called a constant, because it can be regarded as an unnamed string and is a constant, stored in the static data area.
The static data area is relative to the dynamic data area such as heap and stack.
The static data area holds global variables and static variables. At 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 the S2. But there is only one copy in memory, which is quite similar to the nature of static variables.
Char *c= "Chenxi";
The book says: the string "Chenxi" is treated as a constant and is placed in the memory static area of the program.
that general int i=1;
1 is also a constant, why 1 is not placed in the memory static area of this program?
Please expert guidance.
All character channeling constants are placed in the static memory area
Because the string constants need to be changed very little. Put in a static memory area for increased efficiency
Cases:
<span style= "FONT-SIZE:18PX;" >char str1[] = "abc"; char str2[] = "abc"; const char str3[] = "abc"; const char str4[] = "abc"; const char *STR5 = "abc"; c  Onst 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;&L T;/span>

The result is: 0 0 1 1
STR1,STR2,STR3,STR4 is an array variable. They have their own memory space;

Str5,str6,str7,str8 are pointers, they point to the same constant area.


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

No matter what the problem is. Because "Hello world!" is a string constant that is stored in the static data area.
Assigns a pointer to the first address of the static data area where the string constant is stored.
So when the RETURNSTR function exits. The memory in which the string constant resides is not recycled. Therefore, the pointer can be a smooth and accurate interview.



However, the following are 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 one 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 then the function returns a memory address that has been freed. So the print out is garbled.

Assuming that the return value of a function is not the address of a local variable, the local variable must be declared as a static type.

For example, the following:

#include <stdio.h>char *returnstr () {static char p[]= "Hello world!"; Store in the static storage area return p;} int main () {char *str=null; str=returnstr (); printf ("%s\n", str); return 0;}


C + + string constants

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.