ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm doing some raw socket scripting in C. Everything works fine.
However, I monitored the memory using top, and it shows that the memory used by my script is increasing over time.
I don't have that many mallocs (maybe a couple), but lots of pointers. Even though I free those pointers (used as local variables in functions), the memory used by my program is still consuming memory (although slower than if free() is not being used).
I would like to know what situations may produce memory leak and how to deal with them. Do the char[] variables need to be freed?
In general, every piece of memory that is malloced should eventually be freed. But you should not free memory that was not allocated from the heap.
However, you can also have 'leaks' if you do not free system resources (for example, you may be failing to close open sockets that are no longer being used).
Valgrind can be useful for finding leaks if you get really stuck.
Shouldn't the sentence be: But you cannot free memory that was not allocated from the heap.
You are correct in the sense that in an environment like glibc, calling free on a pointer that is outside the heap will generate an error. However, the standard simply says that the behaviour is not defined, and in some environments freeing such a chunk of memory not created by alloc or calloc will appear to work (but corrupt the memory around the chunk).
Last edited by neonsignal; 12-07-2010 at 11:59 PM.
However, I monitored the memory using top, and it shows that the memory used by my script is increasing over time.
Which field are you looking in 'top' command ? It (top) does not accurately represent malloc/free. Neither do most monitoring tools like top. It's quite possible that freeing memory may not be reflected in top.
Only tools that directly interact with application can give an accurate view. As neonsignal mentioned, you can use Valgrind, which would be my first choice (I'm a lazy-to-type GUI lover :P), or you could use mtrace (simple, yet effective) to find the source of leaks.
There is a smallish class of functions which silently allocate memory, and for which the expectation is that the caller will be responsible to free said memory. The function strdup() is an example of one such function. I'm not sure if there is a way to get a concise list of functions that behave similarly. If in doubt, check the repective man pages for functions you use.
There is a smallish class of functions which silently allocate memory, and for which the expectation is that the caller will be responsible to free said memory.
The alloca() function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the function arguments.
Last edited by Aquarius_Girl; 12-08-2010 at 09:09 PM.
No, you don't need the actual return statement for the allocaton to be released.
I'd be curious to see documentation on how it's buggy. I'd risk it in Linux. I have no data to support thinking this way; it's just worth trying.
If you're running a recent version of Linux, it looks as though the scariness in the link I posted as the man page might be too hasty. Here's the scoop on the man page as distributed with Debian lenny. If nothing on that man page gives you specific pause, I'd say use it if you want.
Code:
NOTES
The alloca() function is machine- and compiler-dependent. For certain
applications, its use can improve efficiency compared to the use of
malloc(3) plus free(3). In certain cases, it can also simplify memory
deallocation in applications that use longjmp(3) or siglongjmp(3).
Otherwise, its use is discouraged.
Because the space allocated by alloca() is allocated within the stack
frame, that space is automatically freed if the function return is
jumped over by a call to longjmp(3) or siglongjmp(3).
Do not attempt to free(3) space allocated by alloca()!
Notes on the GNU Version
Normally, gcc(1) translates calls to alloca() with inlined code. This
is not done when either the -ansi, -std=c89, -std=c99, or the
-fno-builtin option is given (and the header <alloca.h> is not
included). But beware! By default the glibc version of <stdlib.h>
includes <alloca.h> and that contains the line:
#define alloca(size) __builtin_alloca (size)
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible to take
the address of this function, or to change its behavior by linking with
a different library.
The inlined code often consists of a single instruction adjusting the
stack pointer, and does not check for stack overflow. Thus, there is
no NULL error return.
BUGS
There is no error indication if the stack frame cannot be extended.
(However, after a failed allocation, the program is likely to receive a
SIGSEGV signal if it attempts to access the unallocated space.)
On many systems alloca() cannot be used inside the list of arguments of
a function call, because the stack space reserved by alloca() would
appear on the stack in the middle of the space for the function argu-
ments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.