We often talk about memory overflow and memory leak in actual programming, especially for C/C++ programs (the following code examples are all C/C++), because we deal directly with memory at this time. However, many times we cannot fully understand these two concepts, and sometimes even confuse the two. In fact, we can also understand the general meaning of memory overflow and memory leak from the naming. Let me give you an example that may not be appropriate. It is like drawing water from a water tank. Originally, this tank can only hold 5 buckets of water. After the 5th bucket is filled, you insist on adding the 6th bucket. The water in the tank naturally overflows. This is called "memory overflow". After the tank is filled with water, no one uses it. On the second day, it is found that the water in the tank is half gone. On the third day, there is no water left. It turns out that there is a hole in the bottom of the tank that was forgotten to be repaired (why do you need to make a hole in the bottom of the tank? No reason, just for fun, willful). This is like applying for a piece of memory and forgetting to release it, causing a "memory leak". The following will briefly analyze these two concepts. First, let's talk about memory overflow. Simply put, memory overflow means that the memory requested for allocation exceeds what the system can provide. For example, if you apply for a 10-byte memory space and you insist on stuffing 11 bytes of data into it, it will naturally be full and overflow (as shown in code example 1). In fact, array out-of-bounds is also a kind of memory overflow, such as exceeding the array range when writing data (reading array data out of bounds is not considered a memory overflow). After the array is filled out of bounds, if you stuff more into it, it will occupy the stack memory (generally, arrays are declared as local variables, and local variables automatically allocate memory in the stack area). The out-of-bounds part is treated as a local variable to occupy the stack memory, because the stack grows from the bottom of the RAM to the top (storing data), and the other data of the program is from the top to the bottom, so when the stack is stored more and more and accumulates higher and higher, the stack will collide with the data when the program is running, and the two will fill the entire RAM memory. At this time, the stack continues to consume, and the stack grows upward, directly overwriting the variables required for the program to run, and the program will run away. So it seems that memory overflow is also very scary.
Next, let's talk about memory leaks. Generally speaking, memory leaks refer to heap memory leaks (Heap leaks). Memory leaks occur when memory is dynamically requested on the heap and is not released in time after use. By the way, if the memory pointed to by the pointer is released, but the pointer is not immediately set to NULL, a wild pointer will be created (as shown in Code Example 2). A single memory leak may not be detected and may not cause any harm, but the accumulation of memory leaks will cause memory exhaustion, which will have serious consequences. For example, memory leaks occur in loop bodies. Of course, there are other forms of memory leaks, such as memory leaks caused by system resource leaks (Resource leaks), etc. According to the frequency of memory leaks, general memory leaks can be divided into the following four types:
|
<<: 7 challenges facing Apple Watch app developers
>>: FB open-sources React Native, using JS to develop native iOS apps
Lee Kun-hee, the helmsman of Samsung Group, is st...
Live-streaming influencers are nothing new nowada...
[[141042]] Original question: What computer techn...
For Borgward, a car brand with German roots that ...
The traditional auto industry is continuously inc...
People in the Internet industry like to talk abou...
This course starts with the basics and explains i...
Nowadays, people are increasingly interested in c...
In the previous issue, we introduced the GAN mode...
Bilibili , the main battlefield to capture young ...
[[226318]] The next generation of Android operati...
1. Development trend of short videos With the acc...
Contemporary Society Wearing a horse-faced skirt ...
A few days ago, I pushed some tips on how to get ...
© Caravaggio/The Atlantic Leviathan Press: I pers...