How to avoid memory leak during development in c

Iam programming in C. I want to know what practice to follow in order to avoid memory leaks during development itself. Please indicate the precautions that you need to take specifically, while working with strings and dynamic memory allocation.

+4
source share
5 answers

Use variables on the stack, if possible, instead of using memory from the heap.

Try to avoid common mistakes, a few pointers:

  • Be sure to call free() when using malloc() or calloc() .
  • Do not reassign the pointer that points to the allocated memory location without free() first, i.e. do not lose the link.
  • Be careful when using realloc() . Do not use the same pointer for input and output parameters.

Avoid common mistakes made with strings with a few pointers:

  • Make sure there is memory to complete the NUL .
  • Make sure the NUL line is complete in all your use cases (even when used in functions like strncpy() , etc.).

Learn to use a debugger ( gdb )
Learn to use static analysis tools. Tools such as splint , valgrind , clang can be installed on the linux system from the distribution repository.

Some useful links:
c-faq - Arrays and Pointers
c-faq - memory allocation
Secure C Coding - Memory Management
SO Question related to the prevention of memory leak in C / C ++
yolinux tutorial

Hope this helps!

+6
source

I do not agree with the votes on this issue. I think this is a real question and rather deep.

On the surface, the answer is "calling free on any memory that you malloc ed".

But the real answer is that your design should include a clear ownership model. The only way to avoid memory leaks and access to tattered memory is to always know for each part of the dynamically allocated memory which object owns this memory (and is responsible for deleting it).

If you do not have such a clear ownership model, you will forever catch memory leaks and errors after use (this also applies to C ++). Using a garbage collector will allow you to plaster these problems through significant processor cycles.

If you have a clear ownership model, these problems usually just disappear: the owner of free all the memory that it owns when it is deleted.

+9
source

The only way to avoid memory leaks is to manually free() all the memory you have allocated throughout the life cycle of your code.

You can use tools like valgrind to check for memory leaks. It will show all the memory that is not freed at the end of the program.

+4
source

There is the option of collecting garbage in C, in particular using the conservative GC Boehm . To use it, replace malloc with GC_malloc , strdup with GC_strdup throughout your program, and you should not worry about calling free or GC_free . In practice, BEM works very well (even if in theory there is little chance of leakage).

Please note that the availability of live data is not a modular property: this piece of data is alive in the entire program (not in a specific module).

To answer the original question, an important issue is the definition of a distribution policy and its documentation. In particular, each function that returns dynamically allocated data should indicate how and who this free -d data should be.

additions

A useful tool for finding memory leak errors in C (or C ++) on Linux is valgrind . Remember to pass the -g -Wall flags to gcc and g++ when developing your code.

+2
source

I am not a C programmer, but as a rule, you need to destroy or destroy everything that you no longer use.

If you do not destroy objects, and the string is objects, then memory may not be garbage collected and may remain in memory.

0
source

Source: https://habr.com/ru/post/1382426/


All Articles