C ++ Memory Allocation

Source: Internet
Author: User

I have some experience in processing C ++ memory allocation. If there is any error, you are welcome to point it out.
The allocation of variables and objects in the memory is well arranged by the compiler during program compilation, but it also brings inconvenience. For example, the array must be large and small, and the pointer must point to an existing variable or object. Dynamic Memory Allocation solves this problem.
C/C ++ defines four memory intervals: Code zone, global data zone, stack zone, and heap zone. Defining variables is static storage allocation during program compilation. All dynamic allocation is performed in the heap area. But it cannot be said that it is absolutely related to the compiler and the library. Function parameters may not be allocated through stacks, which is related to the compiler. When memory allocation fails, the return value is not NULL. Many compilers can capture exceptions thrown by the new operator.

Global data zone code zone stack zone heap Zone
Data code stack heap

Global Variable Function Local variable new, space applied by delete
Static Data function parameters
Constant Return address
Returned data

Allocating and releasing heap memory is the key to reusing limited resources.

Pointer variable name = new type name (initial value );
Delete pointer name;

The new operator returns a pointer to the allocated type variable (object), and the dynamically created object itself has no identifier name. The new expression allocates an object from the heap and initializes and modifies the object with the value in brackets. When objects are allocated from the heap, the new expression calls the library operator new (). For example:
Int * pi = new int (0 );
Note that when the memory allocation fails, zero value is returned. Therefore, you must check the returned pointer when dynamically allocating the memory.
When the delete operator is used, only the memory space pointed to by the pointer is released, but the pointer itself is not revoked, and the memory space occupied by the pointer is not released.

Char * pc = new char;
* Pc = 'a ';
Int * pi = new int (8 );

Stack zone heap Zone
Pc
Pi 8
However, after the function or program is executed, the system stacks and the two variables pc and pi will disappear, but the memory allocated to the heap zone will not be automatically released, so the memory will not be available, unless the system is restarted.

Write Protection Method
Char * pc = new char;
* Pc = 'a ';
Int * pi = new int (8 );

If (pc)
{
Delete pc;
}
If (pi)
{
Delete pi;
}


Char * string = new char [20];

If (string = 0)
{
Return;
}
/*...*/
Delete [] string;

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.