Re: Dlls,memory allocation
- From: jt@xxxxxxxxxxx (Jens Thoms Toerring)
- Date: 21 Nov 2006 12:06:03 GMT
jacob navia <jacob@xxxxxxxxxxxxxxxx> wrote:
Sundar wrote:
i am trying to make an application that will require registering ofThis is a good idea not only for dlls but for
quite a few dlls and execute. Now one of the first bottlenecks that my
mentor refused is allocation of memory or the usage of mallocs in the
dlls. He tells me that for an application to run perfectly , there must
be no dynamic memory in the libraries.. I am not quite impressed..
normal libraries too.
If you allocate memory in the library, it could be that the user thinks
he can free it. That would be catastrophic if your library was compiled
with another compiler and has another runtime free/malloc system than
what the user of the library is using.
What got the compiler to do with it? If I am not completely mistaken,
malloc() and friends are from the C standard library. And if a library
would require a different standard C library from the one the program
linking against the library needs I guess problems about malloc() etc.
would probably be only just one of your headaches.
You can only allocate memory if you also provide a means for freeing
the memory in your library. In that case, the allocation /freeing will
work. Whether it is a good practice it is another thing.
So a non-standard, but often found function like strdup() would also
be unacceptable since there's no "unstrdup" function and you need to
free() the memory for the copy you got? And is there a difference if
I write my own version of strdup() and don't put it into a library or
put it in the library? Would the first way be a "good idea" but the
second a bad one?
A cleaner interface is when the user makes all allocations/freeing
and your library uses the memory it is provided to work with: you
receive pointers to buffers, etc.
Quite often not feasible when only the library can determine how
much memory it's going to need. And if you would have a separate
function for figuring out how much memory is going to be needed
there can be a lot of cases where the information you got that way
can already be outdated once you have allocated memory and finally
call the library function that then is supposed to use the memory.
And as a simpler example: how useful would e.g. again strdup() be
if you would have to do call strlen() first, then allocate enough
memory for the copy and only then do the copy? Actually, you then
would already have written yourself all that strdup() does for you.
I would think that the user should *read* the documentation for the
library (s)he is using, taking special care of which memory one gets
from calls of library functions can or must be free'ed, if it will
be re-used on another invocation of the function etc. Not allocating
memory in libraries just because some users can gat it wrong sounds
to me like outlawing chain saws because some people will hurt them-
selfs trying to use them because the skipped reading the safety in-
structions (or just using their brains).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.
- Follow-Ups:
- Re: Dlls,memory allocation
- From: Tom St Denis
- Re: Dlls,memory allocation
- References:
- Re: Dlls,memory allocation
- From: jacob navia
- Re: Dlls,memory allocation
- Prev by Date: Re: Macro to calculate seconds of a year ( 16 bit processor)
- Next by Date: Re: fork to create ppp link !!!
- Previous by thread: Re: Dlls,memory allocation
- Next by thread: Re: Dlls,memory allocation
- Index(es):
Relevant Pages
|