Re: Memory allocation methods
- From: "Arnaud Debaene" <adebaene@xxxxxxxxxxxxxxxx>
- Date: Thu, 17 Nov 2005 19:35:51 +0100
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
.
- Follow-Ups:
- Re: Memory allocation methods
- From: Stefan Kuhr
- Re: Memory allocation methods
- References:
- Memory allocation methods
- From: Martina Burger
- Re: Memory allocation methods
- From: Stefan Kuhr
- Memory allocation methods
- Prev by Date: Re: Understandng Spy++ Technique
- Next by Date: Re: Timers for Completion Ports?
- Previous by thread: Re: Memory allocation methods
- Next by thread: Re: Memory allocation methods
- Index(es):
Relevant Pages
|