Pointer and memory allocation

1. The pointer is very flexible, which makes the pointer difficult to manage. When defining the pointer, a memory will be opened in the stack to store the address of the pointer (the memory in the stack is allocated and released by the system), and the address memory of the pointer is only the address of the pointer. , Does not store the data pointed to by the pointer. It is worth noting that when the pointer is defined, the pointer will randomly point to a piece of memory, such as int *p; p will point to a piece of non-empty memory, which is quite dangerous, such as executing judgment if(!p)( printf("p is empty");}; "p is empty" will not be output here; so when you want to make the pointer p empty when defining, you must int *p = NULL; if the program is large, the execution result will be wrong no idea where it is.

 

2. Assigning pointers, I have limited ability, I only know that there are two types, one is to allocate a piece of memory on the heap (dynamic allocation of memory), and let the pointer point to this memory. The second is to point the pointer to a piece of memory on the stack ( Generally define a non-pointer variable and let the pointer point to this variable); these two methods are implemented by the following code:

  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. int  main ()  
  4. {  
  5.     //The first  
  6.     int  *p1 = ( int *)malloc( sizeof ( int ));  // dynamically allocate a piece of memory on the heap (manual allocation)  
  7.     scanf("%d",p1);  
  8.     printf("%d/n",*p1);  
  9.     free(p1);   //Release memory  
  10.   
  11.     //The second  
  12.     int   i;            //A block of memory is automatically allocated on the stack by the system  
  13.     int  *p2 = NULL;    
  14.     scanf("%d",&i);  
  15.     p2 = &i;           //Point p2 to the memory of i on the stack  
  16.     printf("%d/n",*p2);  
  17.   return 0;    
  18. }  

 

3. I don’t know if you know why I didn’t add free(p2) after the second method? If free(p2) is added, the compiler will report a memory error, because p2 points to the i memory, and the i memory is in

On the stack, the memory on the stack is managed by the system, such as allocation and release, without manual management. The use of free (p2) here is to release the memory of i. This memory does not need you to manage, but you use free() Of course management will report errors,

 

4. Dynamically allocate memory. In C language, it is allocated using malloc() and released using free(). In C++, it is allocated using new allocation and delete release. Both are allocated on the heap. The memory in malloc() brackets The size is the type of size_t (unsigned int) , the free() parenthesis is the address of the memory or the pointer to this memory, new is an overloaded function of C++, new+size,

delete+address; see the code for specific usage:

[c-sharp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int  main ()  
  6. {  
  7.     //The first  
  8.     int  *p1 =  new int // dynamically allocate a piece of memory on the heap (manual allocation)   
  9.     scanf("%d",p1);  
  10.     printf("%d/n",*p1);  
  11.     delete p1;   //Release memory  
  12.      
  13.     //The second usage  
  14.     int   *p2 =  new int [3];  //Allocate 3 blocks of int type memory space   
  15.     int  i =0;  
  16.     while(i<3)  
  17.     {  
  18.         scanf("%d",&p2[i]);   
  19.         i++;  
  20.     }  
  21.     delete [] p2;  //Pay attention to this release method  
  22.   
  23.     //The third usage  
  24.     int  *p3 =  new int (45);  //Allocate a piece of memory and store 45 in this memory   
  25.     printf("%d/n",*p3);  
  26.     delete p3;  
  27.       
  28.   return 0;    
  29. }  

 

The second way I entered in turn is 56, 78, 23, the following figure is the storage method of p2

 




Guess you like

Origin blog.csdn.net/junzhu_beautifulpig/article/details/51550131