Differences and connections between memory overflow, memory leak, memory overrun and stack overflow in C + + memory mechanism

Source: Internet
Author: User

when we use C + + to do the underlying driver, often encounter memory shortage warning, the reason is often because of memory overflow, leakage or cross-border reasons. So what's the connection between them?

memory overflow (out of Memories)

means that the program does not have enough memory space for it to use when it requests memory.

memory leak (leak)

This means that the program cannot free up the requested memory space and consumes useful memory after it has applied for memory.

Note: Memory leaks can eventually cause memory overflow

Simply understood, a memory overflow is a requirement for allocating more memory than is given by the system. A memory leak is a point that the system requests to allocate memory for use (new), but does not return (delete) when it is exhausted, resulting in a valid memory consumption.

Memory leaks can be divided into 4 categories:

1. Frequent memory leaks

Code that causes a memory leak is executed many times, causing a memory leak every time the execution occurs

2. Accidental memory leaks

Memory leaks are caused by executing code that causes a memory leak in certain environments

From the above two memory leaks, the importance of the test environment and test methods in the program life cycle is essential.

3. Disposable memory leaks

The code executes only once, but there is always a piece of memory leaking, more common in the construction class, the destructor does not free memory.

4. Implicit leakage

The program continues to allocate memory during the run, until the end of the release of memory, but the general server program will run for a long time, not released in a timely manner can lead to memory exhaustion and memory leaks.

To sum up, a one-time memory leak on the user's program maintenance is no real harm, but in real life, we still try to avoid such events occur.

memory out of boundsis pointing to the system to request a piece of memory, when used is beyond the scope of application. For example, some functions of operating memory: sprintf, strcpy, strcat, vsprintf, memcpy, Memset, Memmove. When the code that caused the memory leak runs, the error is unavoidable and often causes1. Corrupt memory allocation information data in heap2. Corrupted the memory space of other objects in the program3. Corrupted free memory blocksattached: If your program has run all right before, but because you add a few classes of member variables or modify some of the code (if you make sure that these changes are completely correct) and cause the program error, because of whether the memory is destroyed, focus on troubleshooting memory is out of bounds. buffer Overflow (stack overflow)in order to temporarily access the data needs, the general allocation of some memory space is called a buffer. If you write data to a buffer that cannot fit into the buffer, the opportunity causes the storage unit outside the buffer to be overwritten, called a buffer overflow. and the stack overflow is a buffer overflow, the principle is the same. Divided into upper overflow and down overflow. Among them, the overflow refers to the full and add new data to it, resulting in data overflow, the next overflow refers to the empty stack and delete operations, resulting in a space overflow.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Differences and connections between memory overflow, memory leak, memory overrun and stack overflow in C + + memory mechanism

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.