Where can I find the source?
Char * c = "ABC" and char C [] = "ABC", where the former changes
The program crashes, and the latter is completely correct.
Program Demonstration:
Test Environment devc ++
Code
# Include <iostream>
Using namespace STD;
Main ()
{
Char * C1 = "ABC ";
Char c2 [] = "ABC ";
Char * C3 = (char *) malloc (3 );
C3 = "ABC ";
Printf ("% d % s \ n", & C1, C1, C1 );
Printf ("% d % s \ n", & C2, C2, C2 );
Printf ("% d % s \ n", & C3, C3, C3 );
Getchar ();
}
Running result
2293628 4199056 ABC
2293624 2293624 ABC
2293620 4199056 ABC
References:
First, we need to figure out the partition format of the memory occupied by the Compilation Program:
I. prerequisites-program memory allocation
The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation is similar
Stack in the data structure.
2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Pay attention to it and Data
The heap in the structure is similar to the linked list.
3. Global (static)-the storage of global variables and static variables is put together. initialized global variables and static variables
Variables are in one area, and uninitialized global variables and uninitialized static variables are in another adjacent area. After the program ends
Release.
4. Text Constant Area-constant strings are placed here. The program is released by the system.
5. Code Area
This is written by a senior. It is very detailed.
// Main. cpp
Int A = 0; // global initialization Zone
Char * P1; // not initialized globally
Main ()
{
Int B; stack
Char s [] = "ABC"; // Stack
Char * P2; // Stack
Char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack.
Static int C = 0; // global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20); // The allocated 10-byte and 20-byte areas are in the heap area.
Strcpy (P1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it into
Location.
}
Ii. Theoretical knowledge of heap and stack
2.1 Application Method
STACK:
Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
Heap:
The programmer needs to apply and specify the size. In C, the malloc Function
For example, P1 = (char *) malloc (10 );
Use the new operator in C ++
For example, P2 = (char *) malloc (10 );
But note that P1 and P2 are in the stack.
2.2
System Response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
Traverse the linked list to find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and
The space of the node is allocated to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space.
Small, so that the delete statement in the code can correctly release the memory space. In addition, the size of the heap node is not necessarily positive.
If the size is equal to the applied size, the system automatically places the excess part in the idle linked list.
2.3 Application size limit
STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. This sentence refers to the place at the top of the stack.
The maximum address and stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB). In short, it is a compilation
If the applied space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space that can be obtained from the stack
Small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses a linked list to store idle memory.
The link is not consecutive, And the traversal direction of the linked list is from the low address to the high address. The size of the heap is limited by
Virtual Memory. It can be seen that the space obtained by the heap is flexible and large.
2.4 comparison of application efficiency:
STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.
Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use.
In addition, in windows, the best way is to use virtual alloc to allocate memory, instead of heap or stack, but directly
The address space of the Process retains a piece of memory, although it is the most inconvenient to use. However, it is fast and flexible.
Storage content in 2.5 heap and stack
STACK: when calling a function, the first entry to the stack is the next instruction of the main function (the next executable statement of the function call Statement ).
Address, followed by the parameters of the function. In most C compilers, the parameters are from right to left and then local changes in the function.
Quantity. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the master
The next instruction in the function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.
2.6 comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during compilation;
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:
# Include
Voidmain ()
{
Char A = 1;
Char C [] = "1234567890 ";
Char * P = "1234567890 ";
A = C [1];
A = P [1];
Return;
}
Corresponding assembly code
10: A = C [1];
004010678a4df1movcl, byteptr [ebp-0Fh]
0040da-a884dfcmovbyteptr [ebp-4], Cl
11: A = P [1];
00401_d8b55ecmovedx, dwordptr [ebp-14h]
004010708a4201moval, byteptr [edX + 1]
004010738845 fcmovbyteptr [ebp-4], Al
The first method reads the elements in the string directly to the CL register, while the second method reads the pointer value to the edx.
Reading characters by EDX is obviously slow.
2.7 summary:
The difference between stack and stack can be seen in the following metaphor:
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave without worrying.
Cooking, washing, and other preparation work, as well as washing dishes, flushing pots and so on, has the advantage of being quick, but has little freedom.
Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.
Self-summary:
Char * C1 = "ABC"; in fact, a piece of memory is allocated in the text constant area to put "ABC", and then an address is allocated to C1 on the stack and points
This address, and changing the constant "ABC" will naturally crash.
However, char c2 [] = "ABC", in fact, the memory allocated by ABC is not the same as that of the upper user.
4199056
2293624 we can see that there are two parts. It is inferred that 4199056 is in the constant zone, and 2293624 is in the stack zone.
2293628
2293624
2293620 this output shows that the areas allocated by the three pointers are Stack zones, from high address to low address.
2293620 4199056 ABC indicates that the compiler points C3 optimization to the "ABC" of the constant zone"
Continue Thinking:
Code:
# Include <iostream>
Using namespace STD;
Main ()
{
Char * C1 = "ABC ";
Char c2 [] = "ABC ";
Char * C3 = (char *) malloc (3 );
// * C3 = "ABC" // Error
Strcpy (C3, "ABC ");
C3 [0] = 'G ';
Printf ("% d % s \ n", & C1, C1, C1 );
Printf ("% d % s \ n", & C2, C2, C2 );
Printf ("% d % s \ n", & C3, C3, C3 );
Getchar ();
}
Output:
2293628 4199056 ABC
2293624 2293624 ABC
2293620 4012976 GBC
Write comments, and subsequent changes will crash.
It can be seen that strcpy (C3, "ABC"); ABC is allocated in another place and can be changed, which is not necessarily the same as the reference document above.
A self-compiled test code:
# Include <iostream>
Using namespace STD;
Int main (void)
{
Char * character = "ABC ";
Char CH2 [] = "ABC ";
Cout <"condition:" <condition <Endl;
Cout <"CH2:" <CH2 <Endl;
Cout <"change happens before followings:" <Endl;
Activities = "abcdd ";
CH2 [0] = 'M ';
CH2 [1] = 'n ';
CH2 [2] = 'q ';
Cout <"condition:" <condition <Endl;
Cout <"CH2:" <CH2 <Endl;
Cout <"change happens before followings:" <Endl;
// Strcpy (failed, "yhn"); // an error occurs.
Strcpy (CH2, "ik ,");
Cout <"condition:" <condition <Endl;
Cout <"CH2:" <CH2 <Endl;
Char * CH3 = (char *) malloc (3 );
Strcpy (CH3, "EDC"); // If CH3 = "rfv" is written here, an error will occur in the following statement.
// The difference lies in the definition before writing or direct writing.
CH3 [0] = '0 ';
CH3 = "ABC ";
Cout <"CH3:" <CH3 <Endl;
Return 1;
}