Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of pointers and memory leaks in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the analysis of pointers and memory leaks in c language". In the daily operation, I believe that many people have doubts about the analysis of pointers and memory leaks in c language. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "pointers and memory leak examples analysis in c language". Next, please follow the editor to study!

What's gonna go wrong?

After the build is complete, there may be a variety of problematic scenarios that can cause problems. You can use the information in this article to avoid many problems when using pointers.

Uninitialized memory

In this example 2, p has allocated 10 bytes. These 10 bytes may contain junk data, as shown in figure 1.

Char ∗ p = malloc (10)

If a code segment p tries to access a value before it is assigned to it, it may get that junk value, and your program may behave mysteriously. P may have a value that your program never expected.

A good practice is to always use memsetwithmalloc or always use calloc.

Char ∗ p = malloc (10); memset (p, â ™\ 0 â ™, 10)

Now, even if the same code snippet p tries to access a value before it is assigned to it, and it handles the null value correctly (as it should ideally), it behaves normally.

Memory coverage

Since p has allocated 10 bytes, if a code snippet tries to write a value to p11 bytes, the operation will quietly eat a byte from another location without telling you. Let's assume that pointer Q represents this memory.

As a result, pointer Q will contain content that was never expected. Even if your module is well coded, it may behave incorrectly because the coexisting module performs some memory overwriting. The following sample code snippet can also explain this situation.

Char ∗ name = (char ∗) malloc (11); / / Assign some value to namememcpy (pMagneNameMame 11); / / Problem begins here

In this example, the memcpy operation attempts to write 11 bytes to p, but it allocates only 10 bytes.

It is a good practice to make sure to cross-check the number of bytes available and the number of bytes being written whenever you write a value to the pointer. Typically, the memcpy function will be a checkpoint.

Memory overflow

Memory overreading means that the number of bytes being read exceeds the expected number. It's not too serious, so I won't go into details. The following code gives an example.

Char ∗ ptr = (char ∗) malloc (10); char name [20]; memcpy (name,ptr,20); / / Problem begins here

In this example, the memcpy operation attempts to read 20 bytes of ptr from, but it allocates only 10 bytes. This will also lead to unwanted output.

Memory leak

Memory leaks are really annoying. The following list describes some scenarios that cause memory leaks.

Redistribution I will use an example to explain redistribution.

Char ∗ memoryArea = malloc (10); char ∗ newArea = malloc (10)

Show more this assigns values to the memory location shown in figure 4 below.

MemoryArea and newArea are each allocated 10 bytes, and their respective contents are shown in figure 4. If someone executes the statement shown below (pointer reassignment)

MemoryArea = newArea

Then it is sure to get you into trouble in the later stages of the development of this module. In the above code statement, the developer has assigned the memoryArea pointer to the newArea pointer. As a result, the memory location previously pointed to by memoryArea becomes isolated, as shown in figure 5 below. It cannot be released because there is no reference to this location. This will result in a memory leak of 10 bytes. Figure 5. Memory leak

Before allocating pointers, make sure that the memory location is not isolated.

First release the parent block assuming there is a pointer to the memoryArea10 byte memory location. The third byte of this memory location further points to some other dynamically allocated 10-byte memory locations, as shown in figure 6.

Free (memoryArea)

If memoryArea is released by calling free, the newArea pointer will also become invalid. NewArea cannot release the memory location you are pointing to because there is no pointer to that location. In other words, the memory location newArea pointed to is orphaned and causes a memory leak. Whenever a structured element is released, it also contains a pointer to the dynamically allocated memory location, first traversing the child memory location (newArea in the example) and releasing it from there, traversing back to the parent node. The correct implementation here will be:

Free (memoryArea- > newArea); free (memoryArea)

Sometimes when the return value is not handled properly, some functions return a reference to dynamically allocated memory. Calling keeps track of this memory location and correctly handles its responsibility as a function.

Char ∗ func () {return malloc (20); / / make sure to memset this location to â void callingFunc () {func (); / / Problem lies here}

In the above example, the call to the callingFunc () function inside the func () function does not handle the return address of the memory location. As a result, the 20-byte block allocated by the func () function is lost and results in a memory leak.

Give back what you've got.

When developing components, there may be a lot of dynamic memory allocation. You may forget to track all pointers (pointing to these memory locations), and some segments of memory have not been freed and have been allocated to the program.

Always track all memory allocations and release them when appropriate. In fact, you can develop a mechanism to track these allocations, such as keeping a counter in the link list node itself (but you must also consider the extra overhead of this mechanism! ).

Access null pointer

Accessing a null pointer is very dangerous because it may cause your program to crash. Always make sure you don't access a null pointer.

At this point, the study of "pointer and memory leak instance analysis in c language" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report