High quality programming guide-C/C ++ Chapter 4 Memory Management

Source: Internet
Author: User

Chapter 4 Memory Management
16.1 Memory Allocation Method
There are three memory allocation methods:
I. Slave"Static storage Zone"Allocate
For example, global variables and static variables.
Ii. Slave"Stack"Upper allocation
For example, local variables (including parameters) in a function ). This type of operation is very efficient, and generally there is no risk of failure, but the allocation
Limited Memory capacity (2 m ?), Stack Overflow may occur.
Iii. Slave"Heap (or free storage zone )"Upper allocation
It is also called dynamic memory allocation. For example, malloc, new, and other applied memory. This type of operation is flexible, but it is more overhead than stack allocation.
The general principle is: if Stack storage and static storage can meet application requirements, do not use dynamic storage (HEAP ).


16.2 memory errors and Countermeasures

1. If the pointer P is a function parameter, use assert (P! = NULL); check to avoid entering invalid parameters.
2. Make sure all variables are initialized before use.
3. Memory out-of-bounds: array out-of-bounds.
4. Memory leakage: not released or not completely released.
5. Memory release error: the same block of memory is released multiple times.

16.3 how does the pointer Parameter Pass the memory?
The compiler always creates a temporary copy for each parameter of the function.

16.4 what about the pointer in free and delete?
After the memory pointed to by the pointer is released, the pointer must be set to null to prevent "wild pointer ".

16.5 will dynamic memory be automatically released?
Never!


16.6 eliminate "wild pointer"


16.7 with malloc ()/Free (), why New/delete?
1. malloc ()/Free () cannot automatically call constructor and destructor. initialize () and destroy () must be defined for the ADT/UDT type to complete initialization and clearing.
2. New directly returns the pointer of the target type. You do not need to convert the display type, but malloc () returns void *. You must convert the display type before using it.

16.8 usage of malloc ()/Free ()
1. the prototype of the function malloc () is as follows:
Void * malloc (size_t size );
2. The prototype of function free () is as follows:
Void free (void * memblock );

16.9 use of new
1. Plain new/delete
Void * operator new (STD: size_t) Throw (STD: bad_alloc );
Void operator Delete (void *) Throw ();

2. nothrow new/delete
Void * operator new (STD: size_t, const STD: nothrow_t &) Throw ();
Void operator Delete (void *) Throw ();

3. Placement new/delete
Void * _ operator new (size_t, void *);
Void _ operator Delete (void *, void *);

16.10 usage highlights of new/delete
1. New/delete and new []/Delete [] must be paired.
2. Multiple delete operations on a pointer p that is not equal to null may cause a runtime error. However, delete a null pointer is safe.

16.11 What should I do if the memory is exhausted?
A general problem: for 32-bit and above operating systems, the use of malloc () and new is almost impossible to cause "memory depletion ". This is because the 32-bit operating system supports "virtual storage", the memory is used up, and the hard disk space is automatically replaced. Therefore, for 32-bit or more applications, the "memory-exhausted" error handler is almost useless.

16.12 simulate pointer with object
1. Memory Management with objects
2. Generic pointer auto_ptr
3. smart pointer

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.