C ++ getting started learning (memory and pointers and arrays)

xiaoxiao2021-03-06  15

1. Memory analysis and pointer can be divided into code area (CONST, COMMON and DATA), Stack Dynamic Assigning Area) in C , data area (divided into const, common, and DATA), stack area (HEAP) and a stacking area. The global variable is stored in the DATA area, local variables are stored in the stack area, and the dynamic variables are stored in the pile area, and the function code is placed in the code area. In C , the object can be staticly allocated, ie, the memory is applied, so it is also relatively high. Such as int ival = 1024, compiled for IVAL for 4 bytes (Win 32), the initial value is 1024, of course, it is also necessary to store the address of this variable in another place. Char * p = "Hello World", the system applies for a memory in the Const area of ​​the data area, stores the string Hello Word, and then assigns the address of the memory to P, and this memory area is read only. The operation of P [1] = 'k' is wrong. The reference to the string constant is the use of its address, such as: if ("Hello" == "Hello) cout <<" Equal "; Else Cout <<" NOT Equal "; The output of the program is run NOT Equal, because the comparison of the above two strings is essentially comparison to their address (the same two array names is also a comparison of the address). The same char * a; a = "hello"; The crux typical "Hello" assignment to the pointer, essentially assigns the address of the character constant to the pointer a. The two main differences between static objects and dynamic allocation are: (1) static objects are variables with names, we directly Operation. And the dynamic object is a variable without the name. We operate it indirectly through the pointer. The so-called static is compiled. It is known that the size cannot be changed; (2) The allocation and release of static objects are automatically handled by the system, the opposite, Analysis and release of dynamic objects must be displayed by programmers. The so-called dynamic is not known when compiling and connecting. Only when running, you can create dynamic arrays according to dynamic allocation; Dynamic object assignment: int * p = new int [5]; int * p = (int *) malloc (intend * 5); these two statements allocated the Int type of int type without the name (that is, memory space, length 5) SIZEOF (int)) and then returns the address of the object (or memory space) to the pointer P, and then only the P can be operated. We need to manually release them for dynamic memory (using delete and free analysis to release the above Two memory spaces) .int * p = new int (10) This is also dynamic allocation, allocating a length of SIZEOF (int), initialized value 10, and returning the address of the memory to P, This is different from the above code. The above code is assigned an array, and the value cannot be initialized. After the dynamic memory allocation, such as char * p = new char [10], the following statement is to determine whether the memory is allocated, such as IF ( p == NULL).

转载请注明原文地址:https://www.9cbs.com/read-47469.html

New Post(0)