Go to C ++ dynamic memory creation and Memory Management Learning Notes [2]

Source: Internet
Author: User
2 C Medium Memory Allocation

A malloc : The prototype is void * malloc (size_t size). A Size Space is allocated to the dynamic storage area in the memory. The Void pointer pointing to the first address of the memory area with the size is returned; you must determine the object length, that is, the size of the applied space. At the same time, malloc only allocates a memory with the return value of void * instead of generating an object. Because the type returned by malloc is void *, you must explicitly convert the type when calling malloc and convert void * to the required pointer type, the malloc function does not recognize the type of memory to be applied for. It only cares about the total number of bytes in the memory. Therefore, sizeof is a good method. For example: int * P = (int *) malloc (sizeof (INT) * length); Use malloc to allocate a piece of continuous memory, and because of compiler implementation problems (such as boundary alignment ), the allocated memory may be a little more than the requested memory. If the memory pool is empty or the available memory cannot meet the requested memory, the compiler requests more memory from the operating system, and execute the allocation task on the obtained memory. If the operating system cannot provide more memory to malloc, malloc returns a null pointer, so it is necessary to check the malloc return pointer. B realloc : The prototype is void realloc (void * PTR, size_t sz). It is used to modify the size of a previously allocated memory block, which can be expanded or reduced. If it is expanded, the original memory is retained and the newly added memory is placed behind the original memory block. The new memory is not initialized in any way. If it is reduced, part of the memory at the end of the memory block is released, and the rest are retained. Note: If the first parameter of realloc is null, the behavior is the same as that of malloc. In addition, if the size of the original memory block cannot be changed, realloc will allocate another correct memory and copy the original memory content to the new block, therefore, you cannot use a pointer to the original memory after using realloc. Instead, you should use a new pointer returned by realloc. C calloc : The prototype is void * calloc (size_t elem_num, size_t elem_sz). The main difference with malloc is that calloc initializes it to 0 before returning a pointer to the memory, the other difference is that they cannot get through the way to request the amount of memory. calloc parameters include the number of required elements and the number of bytes for each element, so that it can calculate the total memory to be allocated. D free : The prototype is void free (void * P). Because the pointer p type and the memory capacity it refers to are known in advance, the statement free (p) can correctly release the memory. If P is a null pointer, no matter how many times the free P operation will fail. If P is not a null pointer, two consecutive operations on P by free will lead to undefined results.

Demo: (a representative example must be written) 

Related Article

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.