Pointers and memory allocations

Source: Internet
Author: User

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
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. int main ()
  4. {
  5. //The first type
  6. int *p1 = (int*) malloc (sizeof (int)); //Dynamic allocation of a piece of memory on the heap (manually allocated)
  7. scanf ("%d", p1);
  8. printf ("%d/n", *P1);
  9. Free (p1); //Free memory
  10. //second type
  11. int i; //The system automatically allocates a piece of memory on the stack
  12. int *p2 = NULL;
  13. scanf ("%d", &i);
  14. P2 = &i; //P2 point to memory on stack i
  15. printf ("%d/n", *P2);
  16. return 0;
  17. }

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
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <iostream>
  4. Using namespace std;
  5. int main ()
  6. {
  7. //The first type
  8. int *p1 = new int; //Dynamic allocation of a piece of memory on the heap (manually allocated)
  9. scanf ("%d", p1);
  10. printf ("%d/n", *P1);
  11. Delete P1; //Free memory
  12. //Second usage
  13. int *p2 = new int[3]; //Allocate 3 blocks of memory space of type int size
  14. int i = 0;
  15. While (i<3)
  16. {
  17. scanf ("%d", &p2[i]);
  18. i++;
  19. }
  20. delete [] P2; //Pay attention to this release method
  21. //Third usage
  22. int *p3 = new int (45); //Allocate a chunk of memory and store 45 in this memory
  23. printf ("%d/n", *P3);
  24. Delete P3;
  25. return 0;
  26. }

The second way I entered in turn is 56,78,23, which is how the P2 is stored

Pointers and memory allocations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.