Is memory leak possible without using malloc?

This question has the same name: Is it possible to cause a memory leak without using any specific kernel tools such as malloc, new, etc.?

What if I create a linked list inside a function with many elements, and after that I exit this function without clearing the list. The list will be created without using malloc calls, i.e.

struct list_head { struct list_head *next, *prev; } 

Is it possible to guarantee that all resources will be freed after exiting this function? So that I can freely execute it a million times, and nothing will be missed?

Subject: Unless you are using any particular malloc or new calls, you will not get a heap memory leak. Never. It is right?

+4
source share
5 answers

A leak is always associated with a resource. A resource, by definition, is what you acquire manually, and that you must release it manually. Memory is a prime example, but there are other resources (files, mutex locks, network connections, etc.).

A leak occurs when you acquire a resource, but then lose the resource descriptor so that no one can release it. A smaller version of the leak is a "still reachable" situation in which you do not release the resource, but you still have a handle and you can release it. This is mostly lazy, but a contrast leak is always a programming error.

Since your code never gets any resources, it also cannot have any leaks.

+12
source

The variables that you used without malloc or new are on the stack of space in memory. Therefore, when the function returns, the variable is back.

On the other hand, the memory that you used with malloc or new located in the cumulus space. The system does not care about whether you release or not. In this situation, if you are not using free or remote, a memory leak.

+1
source

Subject: Unless you are using any particular malloc or new calls, you will not get a heap memory leak. Never. It is right?

This assumption is not entirely correct. The problem is that the operating system itself (or other third-party components that you must rely on) may also have a memory leak. In this case, you cannot actively call malloc, but call other (operating systems) functions that may leak.

So, your assumption depends on how much you consider such a thing. You can claim that the OS / third party implementation is outside of your domain, then this assumption would be correct. If you have a well-defined system and your requirements are such that you need to arrange a certain uptime, something like this can also be considered.

So the answer to this question ...

Is memory leak possible without using malloc?

...:

Yes it is possible.

+1
source

malloc() allocates memory from the heap, and space for string and structure literals ( string1 , string2 and those list_head ) will be reserved at compile time on the stack.

In fact, any memory allocated for the program (heap or stack) will be restored by the kernel when the process is completed (at least on the * nix system).

I would define a memory leak as allocating memory in a heap and not freeing it when your program exits. This definition really answers your question.

There are standard functions (e.g. strdup ) that will allocate memory on a bunch, beware of them.

0
source

Another example of a resource that you can allocate and forget for free:

If you use OpenGL and you call glGenBuffers() million times without the corresponding calls to glDeleteBuffers , it is very likely that you will run out of VRAM and your graphics driver will leak into system memory.

I just happened. Fortunately, the Visual Studio memory profiler is pretty easy to find. It appeared as a large number of distributions made by the external nvoglv32.dll process, which is my NVIDIA OpenGL driver.

0
source

All Articles