Original: http://blog.csdn.net/bizhu12/article/details/6532235
1. The pointer is very flexible, which makes the pointer difficult to manage, when the pointer is defined, the stack will open up a memory of the address of the pointer (memory in the stack is allocated and freed by the system), the address of the pointer memory is just the address of the pointer, not the data pointing to the pointer, it is worth noting that the pointer is defined such as int *p;p will point to a piece of non-empty memory, quite dangerous, such as execution judgment if (!p) {printf ("P is empty");}; This does not output "P is empty", so if you want the pointer p to be empty when you define it, you want int *p = NULL, and if the program is large, the result of execution is not known where.
2. Assigning a value to the pointer, I have limited ability, only know that there are two, one is to allocate a piece of memory on the heap (dynamic allocation of memory), so that the pointer to the memory. The second is that the pointer points to a block of memory on the stack (typically defining a non-pointer variable that points to the variable);
[CPP]View Plaincopy
- #include <stdio.h>
- #include <malloc.h>
- int main ()
- {
- //The first type
- int *p1 = (int*) malloc (sizeof (int)); //Dynamic allocation of a piece of memory on the heap (manually allocated)
- scanf ("%d", p1);
- printf ("%d/n", *P1);
- Free (p1); //Free memory
- //second type
- int i; //The system automatically allocates a piece of memory on the stack
- int *p2 = NULL;
- scanf ("%d", &i);
- P2 = &i; //P2 point to memory on stack i
- printf ("%d/n", *P2);
- return 0;
- }
3. Don't know if you know why I didn't add free after the second method (P2)? If the P2 compiler will report a memory error because P2 points to I memory, I's memory is
Stack, the memory on the stack is managed by the system, such as allocation and release, do not need to manually manage, here use Free (p2) is to release the memory of I, this block of memory does not need you Tube, but you use the administration of free () of course will error,
4. Dynamic allocation of memory, in C language is the use of malloc () to allocate and use free (), in C + +, is the use of new allocation and delete release, are allocated on the heap, malloc () in parentheses is the size of the memory, is size_t (unsigned int) , free () is the address of the memory or a pointer to the memory, new is the overloaded function of C + +, new+ size,
delete+ address; See code for specific usage:
[C-sharp]View Plaincopy
- #include <stdio.h>
- #include <malloc.h>
- #include <iostream>
- Using namespace std;
- int main ()
- {
- //The first type
- int *p1 = new int; //Dynamic allocation of a piece of memory on the heap (manually allocated)
- scanf ("%d", p1);
- printf ("%d/n", *P1);
- Delete P1; //Free memory
- //Second usage
- int *p2 = new int[3]; //Allocate 3 blocks of memory space of type int size
- int i = 0;
- While (i<3)
- {
- scanf ("%d", &p2[i]);
- i++;
- }
- delete [] P2; //Pay attention to this release method
- //Third usage
- int *p3 = new int (45); //Allocate a chunk of memory and store 45 in this memory
- printf ("%d/n", *P3);
- Delete P3;
- return 0;
- }
The second way I entered in turn is 56,78,23, which is how the P2 is stored
Pointers and memory allocations