GlobalAlloc || malloc

Started by
14 comments, last by Ethereal Darkness 22 years, 5 months ago
ok, let me give you some more info on what im doing,
im allocating image data and 3d mesh vertices,

so it''s pretty sizable data, that is allocated once in the program,

so should i use virtual alloc?

i want to use a function that doesnt end up calling a bunch of other functions that i could have called myself.
Advertisement
If it is performance you are worried about you need to ask yourself ''How many times am I allocating memory and freeing it?''.

If you are only doing infrequently, it really doesn''t matter. But if you are diung it in every frame you a) need to rework your code so that this does not happen (it is very inefficient IMHO) or use virtualalloc (it appears to be the root function).

I use malloc/free (well new/delete in reality), I don''t call it alot so it makes no difference to me.

D.V.
D.V.Carpe Diem
Since the operation is only performed once the performance cost of using a wrapper is negligible. I would suggest writing your routine using the easier to use functions first and then if that''s not satisfactory moving on to the more involved functions - VirtualAlloc etc. The definition of satisfactory is left to you.
I use VirtualAlloc for windows development for two reasons.

1. Its the lowest level memory allocation available in Windows, ie: all other memory allocations will eventually go throw this function.

2. I have control over the type of memory being allocated, ie: PAGE_READONLY, PAGE_READWRITE, PAGE_EXECUTE... This is very useful for debugging and optimization.

I allocate all of my memory at the start of the program, in separate banks depending on how I will be using it. Then I have my own memory allocation function that does the dirty work. Most of these calculations are done at compile time. In some cases the overhead for memory allocation is only 1 cycle. It just wouldn't make sense to use malloc in the inner loop on say a linked list.

Portability is easy because I use abstraction.

quote:
Windows SDK

The following functions are equivalent: GlobalAlloc, LocalAlloc, and HeapAlloc with the handle returned by the GetProcessHeap function.

The VirtualAlloc function allows you to specify additional options for memory allocation. However, its allocations use a page granularity, so using VirtualAlloc can result in higher memory usage.

The malloc function has the disadvantage of being run-time dependent. The new operator has the disadvantage of being compiler dependent and language dependent.



Edited by - burp on November 14, 2001 11:32:38 AM
I had question like this a week before. I was wondering what
the mood was like on using extended memory management programs
like XMS and DPMI in DOS and Windows environment.

How do you rate these memory management programs? Your opinions
are greatly appreciated.

Thanks,
BM
If the question is between using "GlobalAlloc" or "LocalAlloc" versus "malloc" or "new", you should pretty much always use "malloc" or "new". This is preferred, not only because it''s portable, but also because it can be a lot faster, and provides some opportunities for leak and access checking. The "malloc" implementation in Visual C++, for example, tends to be a lot faster than the API level allocators, because it uses the API level allocators smartly.

This topic is closed to new replies.

Advertisement