As the introduction of memory allocation on the internet is relatively simple, I wrote a copy by myself.
If you have a problem with contact C, please point out that you are welcome to discuss it.
The compiling environment is vs2005...
First, let's take a look at the following section:
Int main ()
{
Char * name = "fengkewei ";
Char name1 [] = "fengkewei ";
Char * name2 = "fengkewei ";
Char * name3 = "woaifengkewei ";
Int I = 10;
Int J = 10;
Int K = 11;
Return 0;
}
If you think they should all be stored in a part of the memory, please look down ....
The following is the memory address allocated by the compiler for each variable. Because the address is allocated on the stack, the address is decreased.
--------------------- Stack zone ------------------------------------
+ & Name 0x0013ff5c unsigned char **
+ & Name1 0x0013ff48 unsigned char [10] *
+ & Name2 0x0013ff3c unsigned char **
+ & Name3 0x0013ff30 unsigned char **
+ & I 0x0013ff24 int *
+ & J 0x0013ff18 int *
+ & K 0x0013ff0c int *
---------------------------------------------------------------
& Name and & name1 [] differ by 20 bytes, but "fengkewei" only has 10 bytes (with null characters)
The difference between & name1 [] and & name2 is 12 bytes.
Normally followed by 12 errors
Why is it 12?
--------------------------------------------
The following is the first address of each string. They are all the same in the text constant area. If they are the same, they are incrementally allocated (the text constant area is also incrementally allocated)
--------------------------- Text constant area -------------------------------------------------
+ Name 0x004256b8 "fengkewei" unsigned char *
+ Name2 0x004256b8 "fengkewei" unsigned char *
+ Name3 0x00416010 "woaifengkewei" unsigned char *
+ Name1 0x0013ff48 "fengkewei" unsigned char [10]
It can be seen that name1 [] is in a different place, that is, it is on the stack.
Now let's set an integer pointer.
Int * P;
P = & I;
The P address should point to the I address and stack area.
Sure enough:
+ & I 0x0013ff24 int *
+ P 0x0013ff24 int *
--------------------------------------------------------------------
What if I set a struct pointer?
Char * P;
P = Name;
+ & Name 0x0013ff5c unsigned char **
+ Name 0x004256b8 "fengkewei" unsigned char *
+ P 0x004256b8 "fengkewei" unsigned char *
P points to the string address, that is, to the text constant area.
Bytes ------------------------------------------------------------------------------------------
Why is the & name and name addresses different? Because name is a variable of variable type, the space of the variable is allocated in the stack area, and the string pointed to by name is in the text constant area,
Name is a variable of the character type, and the value saved in it is the address of the string in the text constant area. That is why the Pointer Points to the content and the pointer itself.
The reason for the difference.
It can be seen that the string constants are placed in the text constant area. When you initialize the value assignment, these constants first open up a space in the text constant area to save the constant.
The same constants will be here in the future.
Char name1 [] = "fengkewei ";
+ Name1 0x0013ff48 "fengkewei" unsigned char [10]
-& Name1 0x0013ff48 unsigned char [10] *
[0] 102 'F' unsigned char
[1] 101 'E' unsigned char
[2] 110 'n' unsigned char
[3] 103 'G' unsigned char
[4] 107 'K' unsigned char
[5] 101 'E' unsigned char
[6] 119 'W' unsigned char
[7] 101 'E' unsigned char
[8] 105 'I' unsigned char
[9] 0 unsigned char
It can be seen that name1 always points to an address, which is the address of the stack zone. This can be understood why name1 indicates the first address of the array.
But what does the following 101,102,103,... represent? It is an ascii code.
Look at the text constant area:
+ Name 0x004256b8 "fengkewei" unsigned char *
Name [0] 102 'F' unsigned char
Name [1] 101 'E' unsigned char
Name [2] 110 'n' unsigned char
That is to say, both in the stack area and in the text constant area are saved in this way.
But if so:
Char name4 [20];
Strcpy (name4, "fengkewei ");
+ & Name4 0x0013fee4 unsigned char [20] *
+ & Name4 [0] 0x0013fee4 "fengkewei" unsigned char *
+ & Name4 [1] 0x0013fee5 "engkewei" unsigned char *
+ & Name4 [2] 0x0013fee6 "ngkewei" unsigned char *
Name4 [0] 102 'F' unsigned char
Name4 [1] 101 'E' unsigned char
It can be seen that the addresses of each array element are in the stack zone, and their values are saved with corresponding characters. That is to say, this strcpy () does not call the "fengkewei" in the text constant area ",
Instead, the content is directly stored in the stack name4 [20.
Similarly
Char name5 [20] = "fengkewei ";
+ Name4 0x0013fee4 "fengkewei" unsigned char [20]
+ Name5 0x0013fec8 "fengkewei" unsigned char [20]
The two methods are saved in the stack.
Finally, we will summarize the Saving Method of the text constant area:
Char * name = "fengkewei ";
+ Name 0x004256b8 "fengkewei" unsigned char * // The value of name is the address saved in the text Constant Area "fengkewei"
+ & Name 0x0013ff5c unsigned char ** // the address of the name itself is allocated to the stack by the compiler.
Name [0] 102 'F' unsigned char // the first character value of name is 'F'
+ & Name [0] 0x004256b8 "fengkewei" unsigned char * // It is saved in the text Constant Area
Name [1] 101 'E' unsigned char // The second character is 'E'
+ & Name [1] 0x004256b9 "engkewei" unsigned char * // It is also stored in the text Constant Area
It can be seen that string constants are divided into the following types based on different storage regions:
One is saved in the stack, char name5 [20] = "fengkewei ";
Or char name1 [] = "fengkewei ";
One type is saved in the text constant area, that is, char * name = "fengkewei ";
It is saved in the global area (static area) (next time you have a chance to talk about it ...)
The last type is stored in the heap zone, that is, the memory allocation area is allocated by malloc, alloc, and realloc, which can be allocated and released by the programmer.
Many posts on the Internet are not described here due to space issues...
There are other areas: the program code area stores the binary code of the function body.
Of course, after reading this post, you should be able to guess the storage method of other zones ....
Thank you for reading this article .... :)
Article Source: http://hi.baidu.com/summy00/blog/item/501a16dae1fe96deb7fd487d.html
(A constant is called a "text constant". "text" means that we can only refer to it as its value. "constant" means that its value is immutable. At the same time, note that a text constant is unaddressable (that is, it is impossible for our program to obtain an expression like constant 20 & 20 ), although constants are stored somewhere in the memory, we cannot access the constant address.
Constants are of the following type:
1. Character-type char: A byte representation, which usually represents a single character or a small integer. Character-type constants are represented by a pair of single quotation marks 'with a character.
(1) printable character constant representation:
'A '2 '','''
The storage format of character constants in memory depends on the ASC character table.
(2) character constants cannot be printed, expressed by the slash:
'/N' line break' // 'backslash'/t' horizontal tab '/0' null (null) character
2. Integer INT: the integer of the length of a machine word.
Short integer short: the integer of half the length of a machine word.
Long Integer: the integer of one or two machine characters.
In 32-bit machines, int and long are usually the same.
(1) The Char character type mentioned above can also be considered as a byte character integer.
In the following small example, we can see that the memory format varies with the char data initialization methods.
Char A = '1 ';
Cout <A + 1 <Endl; // The output result is 50. Refer to the ASC table. The character constant '1' is in the memory of 49 decimal digits.
Char B = 1;
Cout <B + 1 <Endl; // The output result is 2.
In fact, character constants can also initialize int, long, and other types of data, such:
Int c = '1 ';
Cout <C + 1 <Endl; // The output result is also 50.
And:
Char A = '1 ';
Cout <A <Endl; // The output result is 1.
Int A = '1 ';
Cout <A <Endl; // The output result is 49.
These differences need to attract our attention.
(2) Integer constants can be expressed in decimal, octal, and hexadecimal notation. For example:
20 (decimal) 024 (octal) 0x24 (hexadecimal, can also be written as 0x24, "X" is case insensitive)
(3) Integer constants can be signed or unsigned. For example:
An 8-bit signed Char:-128 ~ 127
An 8-bit unsigned char: 0 ~ 255
Note: The default integer constant is int type. We can use the suffix "L" or "L" to forcibly express the integer constant as long type, you can also add the suffix "U" or "U" to specify the number of unsigned characters, such as 128u, 1024ul, 1l, and 8lu.
3. Float: A single-precision floating point number with a Word Length
Double-precision: double-precision floating-point number with two character lengths
Long dual-precision long double: Extended Floating Point Number with three or four characters in length
(1) floating point constants can be written into scientific notation or common decimal numbers.
(2) floating-point constants are double by default. They can be suffixed with "F", "F", "L", or modified to single-precision floating-point numbers or Extended Floating-point numbers, however, you can only modify the floating point in decimal format. Another difference with Integer constants is that floating point numbers are positive and negative, that is, they cannot use the suffix "U" or "U.
Example: 3.14159f 0.1f 12.345l 0.0 3e1 1.0e-3 1.0l
4. boolean constants: true or false
5. String constant: a special type. It is not a built-in or basic data type, but an array of character constants, it consists of the character string itself and the null character that represents the end added by the compiler.
The actual format of the String constant "AB" in the memory is 'A' B '/0'
If "two" "some" is in close proximity in the program, the C ++ compiler will connect them together and add an empty character at the end, that is, the output is "twosome"
Character string constants can also be indicated by line breaks. You only need to add "/" to the line breaks, for example:
"ABC/
De/
Fgh"
It actually represents "abcdefgh"
Article Source: DIY tribe (http://www.diybl.com/course/3_program/c++/cppjs/20091021/179797.html ))