C + + string constants

Source: Internet
Author: User

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.
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 string constants rarely need to be modified, placing them in the static memory area increases 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 are array variables, and they have their own memory space;

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

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