Re: [ILUG] C question [puzzled by strdup()]

From: Padraig Brady (Padraig at domain AnteFacto.com)
Date: Thu 02 Aug 2001 - 11:45:16 IST


John P. Looney wrote:

> On Thu, Aug 02, 2001 at 12:06:47AM +0100, Conor Daly mentioned:
>
>>The first call to strdup() allocates memory to hold the string and sets
>>optionstring to that address. When the second strdup() call passes a
>>*new* address to optionstring, what happens the previously allocated
>>memory that optionstring *was* pointing to?
>>
>
> No offense Conor, but I strongly recommend you stick to Perl, Shell, TCL
> or Java! Something that'll do your garbage collection for you.
>
> In the manpage for strdup, there is;
>
> "Memory for the new string is obtained with mal­ loc(3), and can
> be freed with free(3)"
>
> It should read "For crying out loud, free this as soon as you can, or
> you'll be reaching for ElectricFence within a week".

ef doesn't find memleaks, it finds heap corruption.
There are actually 2 really good tools built into glibc for dynamic
memory debugging.

1. To find heap corruption of various forms, do:
MALLOC_CHECK_=1 myprogam
Note you can do this for any program as it doesn't
need a recompile.

2. To find leaks do:

#include <mcheck.h>
main() {
    mtrace();
    ...
    exit();
}

MALLOC_TRACE=myprogram.mt myprogram
mtrace myprogram.mt



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:11:26 GMT