Deep understanding of C pointer Reading Notes, deep understanding of pointers

Source: Internet
Author: User

Deep understanding of C pointer Reading Notes, deep understanding of pointers

Chapter2.h

# Ifndef _ CHAPTER_2 _ # define _ CHAPTER_2 _/* learning notes for understanding the C pointer in depth-Chapter 2 * // * Two Memory leakage Forms 1. forget to recycle memory 2. memory Address Loss */void _ memory_leak_test ();/* memory operation functions mallocallocreallocfree */void _ memory_function_test (); # endif

Chapter2.cpp

# Include "Chapter2.h" # include <stdio. h> # include <malloc. h> # include <assert. h>/* Two memory leaks: 1. forget to recycle memory 2. memory Address Loss */void _ memory_leak_test () {/* applied for memory but forgot to recycle unused memory */int * p_test1 = (int *) malloc (sizeof (int); * p_test1 = 1; // do_something_to: p_test1/* When p_test1 is no longer required, the memory should be released in time, and assign NULL * // * free (p_test1); p_test1 = NULL; * // * the memory address is lost in the program, so there is no way to release the application (HEAP) memory */int * p_test2 = (int *) malloc (sizeof (int); * p_test2 = 2; // do _ Something_to: p_test2/* When p_test2 is no longer required, the memory should be released in time and assigned NULL * // * free (p_test2); p_test2 = NULL; * // * But at this time, another memory is applied again, causing the original memory address to be lost */p_test2 = (int *) malloc (sizeof (int )); /* after this memory is not required, it should be displayed to release the memory */free (p_test2); p_test2 = NULL;} void _ memory_function_test () {/* malloc function is used to allocate memory. The void * type pointer is returned, pointing to the initial address of the allocated memory. However, Initialization is not performed on this memory. When the memory application fails, returns NULL */int * p_test1 = (int *) malloc (sizeof (int);/* If the applied memory is large, you need to determine whether the application failed. Generally, the application is small. */Assert (p_test1! = NULL); // do_something_to_p_test1/* release the memory in time after use to avoid Memory leakage */free (p_test1);/* calloc function to allocate memory from the stack, the difference with the malloc function is that it clears the applied memory. When the memory application fails, NULL */int * p_test2 = (int *) calloc (2, sizeof (int);/* the above operation can use malloc and memset to implement int * p_test2 = (int *) malloc (2 * sizeof (int); memset (p_test2, 0, 2 * sizeof (int); * // * If the applied memory is large, you need to determine whether the application failed, this operation is not required for small memory blocks. */assert (p_test2! = NULL); // do_something_to_p_test2/* release the memory in time to avoid Memory leakage */free (p_test2);/* realloc function, it is generally used to process the previously applied memory, apply for a larger memory, or reduce the memory size. if the parameter is 0, the memory will be released, as if it were free. if the applied memory is smaller than the currently allocated memory, the excess memory will be returned to the heap 3. if the requested memory is larger than the currently allocated memory, a larger memory may be allocated next to the current memory area. Otherwise, it is allocated in other areas of the heap and the old memory is copied to the new area */char * p_test3; char * p_test4; char * p_test5; p_test3 = (char *) malloc (8 * sizeof (char *); printf ("p_test3 address is: % x \ n", p_test3); p_test4 = (char *) realloc (p_test3, 64 * sizeof (char *); printf ("p_test4 address is: % x \ n", p_test4); p_test5 = (char *) realloc (p_test4, 65 * sizeof (char *); printf ("p_test5 address is: % x \ n", p_test5);/* The running result of the preceding example is as follows: p_test3 address is: 53a848p_test4 address is: 53a8e0 finds a new region and allocates larger memory p_test5 address is: 53a8e0 allocates larger memory on the original memory * // * do not release the memory repeatedly, otherwise, the result is undefined. The exception is to release the NULL pointer (NULL) */int * p_test6 = (int *) malloc (sizeof (int);/* free (p_test6 ); here is the correct operation free (p_test6); here there will be problems, because the released memory * // * free (p_test6) is released ); here is the correct operation p_test6 = NULL; free (p_test6); although it is released multiple times, but the previous operation has assigned NULL to the pointer, free will not cause problems */}


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.