Re: Dlls,memory allocation



jacob navia <jacob@xxxxxxxxxxxxxxxx> wrote:
Sundar wrote:
i am trying to make an application that will require registering of
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..

This is a good idea not only for dlls but for
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
.



Relevant Pages

  • Re: Simple C containers, std::vector analog
    ... Possibly an artifact of your OS's memory handler. ... >standard C library dynamic heap memory allocation facility, ... For coding libraries in general, I suggest portability is more ...
    (comp.lang.c)
  • Re: Memory Management
    ... The program is a bunch of DLLs. ... I'm still using the CRT's malloc and free to allocate memory. ... If the application makes a memory allocation request, ... I have implemented a memory manager for some SDK I'm working on (a ...
    (microsoft.public.vc.language)
  • Re: Dlls,memory allocation
    ... quite a few dlls and execute. ... be no dynamic memory in the libraries.. ... In that case, the allocation /freeing will ...
    (comp.lang.c)
  • Re: More Flex issues
    ... It was distributed as part of the now-ancient RISC_OSLib library, and I believe in a newer incarnation is shipped as part of the Toolbox libraries with the Castle 'C' development tools. ... As you allocate memory your program's Wimp slot size will increase. ... Fragmentation occurs when free spaces open up as they might not be large enough to accommodate a subsequent allocation. ... Many applications attempt to get around this using Flex. ...
    (comp.sys.acorn.programmer)
  • Re: W3WP memory usage
    ... You may be correct regarding the libraries using the lion's share of memory. ... > I am quite aware that Sharepoint files are stored in the database. ... >>> public class SPVersion ...
    (microsoft.public.sharepoint.portalserver.development)