From c to C + + to obj-c memory Management Learning notes (ii)

Source: Internet
Author: User

After finishing the memory management Hong Kong concept, this article focuses on the use and implementation of the memory stack, malloc ()-free () A, new-delete.


First of all, what we must know is that each program is stored in several storage areas, static storage area, stack, heap, free storage area, constant storage area and program binary code. There is also the register of the CPU. As shown in the following:




Static storage : Stores the static variables mentioned in the previous article, including static global variables, static internal variables, static local variables, which are stored in the static store when the program is compiled.


constant Storage : The constants involved in the main repository, such as string constants, are stored in this area.


Register: Registers can be placed in this area can also not include, because he belongs to the CPU, in order to complete, I still include him in.


Program binary code area : The main storage is the program code by the compiler converted into binary code.


Stack area : The stack area holds the automatic variables in the nested code block structure, each defining a variable to add a variable to the stack, which is generally 1M in size. The local variable pops up at the end of the code block. It is worth noting that the popup here is not a variable really popped from the stack, but the stack top pointer moves down, the original variable is not destroyed, wait until there is a new automatic variable into the stack, overwrite, the pointer moves up. As shown in the following:



It is important to note that when we call a recursive function, the stack overflow occurs if the recursive hierarchy is too deep to exceed the stack's upper memory limit.


Heap Area : The heap area is typically used to store variables that we dynamically allocate, such as the memory allocated by malloc () and new. If malloc initiates a specified size memory request to the heap, if the heap has enough memory space to return to the appropriate address space, the operation will wait for the memory to return if it is unsuccessful. As shown in the following:


It is worth noting that the memory allocation of the heap is implemented by an idle list, and when requested, if the size of the free list is larger than the requested space, the corresponding memory interval is returned, otherwise the application is unsuccessful.

When memory is freed through free () and delete in the code, the corresponding memory is returned to the idle list, and the idle list is organized again to wait for the next request.

Heap area memory allocation is characterized by the allocation of fast recovery of slow, suitable for large memory objects.


Compared to the stack area, the heap area is mainly manually managed, and the stack area is automatically managed; The heap area can be up to 4G, while the stack area is only 1 m, the heap area has a lot of memory fragmentation in memory management, the stack area is almost free of fragmentation, and the heap area needs to organize memory space, which is lower than the stack area


The following is another introduction to the difference between malloc (), Calloc (), ReAlloc ():

malloc () has only one parameter, that is, the required memory space, the application is not initialized;

calloc () has two parameters, one is the number of memory space, the other is the size of each memory space, the application will automatically initialize each bit to 0;

realloc () is the memory size reallocation, if the new request for memory than the original is returned to the original address, the newly added address is not initialized, if it is reduced, it will return the new memory address.


It is worth noting that when using malloc (), remember to use free () to release the discarded variable space in a timely manner, otherwise memory leaks. All memory is automatically emptied after the program ends unless you can do so at the close of the program.


The new operation is described below. New is a C + + keyword, not in the C language.

When using new for class objects in C + +, three steps are performed:

1. Request memory space in the heap;

2. Execute the constructor function;

3. Return the object.


Use new or new [] when you need to allocate memory dynamically, and use delete or delete[when not needed to free memory, which prevents memory leaks.

In addition to the regular new, you can manually allocate memory and memory locations through layout new. Such as:

<span style= "FONT-SIZE:18PX;" >char buffer[512];d ouble *p;p = new (Displacement +n*sizeof (double)) double[n];</span>
a buffer is created in the code, using layout new to allocate memory space at a specified position (position of displacement) on the buffer, and unlike the normal new operation, the layout new operation does not know which memory is being used, so this requires the program ape to manage its memory location.

It is also important to note that when using layout new, if the buffer is not dynamically allocated, the memory cannot be freed with operations such as delete p[], and the requested memory is released as the buffer (automatic variable) is released. You can use Delete to free up memory space only if the buffer is also dynamically allocated.

From c to C + + to obj-c memory Management Learning notes (ii)

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.