Re: Memory allocation methods

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Stefan Kuhr wrote:
> Hi Martina,
>
> Martina Burger wrote:
>> Hi,
>>
>> my program frequently allocats and deallocates memory with various
>> allocation sizes.
>>
>> Which memory managment method is to prefer:
>>
>> - LIBC alloc/free
>> - C++ new/delete
>> - HeapAllo/HeapFree
>>
>> The latter offers from W2K a low fragmentation mode but my program
>> has to run on WinNT4, too.
>>
>> Hints appreciated!
>
> In general I would agree to what Arnaud wrote, you should use what
> your language offers as the most natural choice. However there are a
> number of additional considerations you might take into account:
>
> - using the runtime allocators like malloc and new, you cannot pass
> memory ownership of dynamically allocated memory between modules, i.e.
> you cannot malloc memory in a DLL and pass it back to a .exe module
> which is responsible for cleaning up using free.
Yes you can! Just make sure that all modules are compiled against the same
DLL version of the CRT.

> In such a situation
> you have to use some sort of an API based allocator/deallocator pair.
> I frequently use LocalAlloc/LocalFree in such situations.
Just compile all of your modules (exe an dlls) either with /MD, either with
/MDd (do not mix...)

> - new might or might not throw std::bad_alloc depending on the runtime
> you are using (e.g. new in VC6 in STL doesn't throw, whereas in later
> versions it does, thus mnore closely adhereing to the standard). This
> is something you might want to have or not, at least it can influence
> your decision on what allocator you want to use in a particular
> situation.
agreed.

> - the runtime allocators do not set a thread's last error in case they
> fail (at least they don't do so officially), so if your code is
> heavily based on propagating last errors back to the caller, you at
> least have to set a last error with
> SetLastError(ERROR_NOT_ENOUGH_MEMORY) if they fail (hoping it was
> really a low memory condition, not a corrupted heap).
agreed again, although I wouldn't rely on GetLastError/errno for any
serious, process wide, error checking anyway....

Arnaud
MVP - VC


.



Relevant Pages

  • Re: Red-black trees?
    ... significant way on the amount of storage requested. ... assigned memory the first program released. ... You're mixing up two different issues, initializing space used by ... Some storage allocators initialize allocated storage, ...
    (comp.programming)
  • Re: [PATCHv2 2/4] mm: cma: Contiguous Memory Allocator added
    ... Various chips require contiguous blocks of memory to operate. ... The Contiguous Memory Allocator (CMA) is a framework, ... in-between between device drivers and pluggable allocators. ...
    (Linux-Kernel)
  • Re: STL Slow - VS2005
    ... will give slower node reuse but heap memory use will be minimal as it is ... Instead they measure the default choice of allocators that comes ... to inform the library designers what their intention is with a container. ... don depend on the choice of growth factor for vector, string. ...
    (microsoft.public.vc.stl)
  • Re: OO compilers and efficiency
    ... > would entirely agree with you -- people who think they can code faster ... In C you can make "custom allocators" which will allocate ... you collapse the memory by freeing them in blocks at a time. ... C is a horrible programming language has caused the software ...
    (comp.programming)
  • Re: allocation of memory to a program
    ... size of the heap and the stack memory to be allocated to it. ... mean that presently i was reading about physical memory allocators and ... require,so how does the kernel or the allocater decides ... i.e. global variables that are not assigned initial values ...
    (comp.os.linux.development.system)