Re: VirtualAlloc()



"Chris Kushnir" wrote:
Well, I always have been using VirtualAlloc() for
large/global memory usage instead of "new". I only use
"new" inside functions. Because VirtualAlloc() can
allocate physical storage (either in RAM or on disk).

Whoever told you that was wrong. How do you think `new'
allocates memory? As any other allocation function it
uses `VirtualAlloc' (either directly or indirectly).
However, by avoiding `new' you miss many useful checks.
Like memory leaks, in your case, for example.

The real question is: Why do you need `VirtualFree'
instead of regular `new'?

new (in VC++) eventually calls malloc, which may
eventually call HeapAlloc, which may eventually manipulate
memory reserved/committed by VirtualAlloc. There are a
number of reasons to use Virtual*(), just as there are for
Heap*(), just as there are for malloc, just as there are
for new. Granted, most apps don't need to go beyond
new/malloc, but knowing how/when/why to use the
Heap/Virtual API's can give some apps significant (speed)
improvements.

All this is nice, but original poster didn't provide enough
info to decide whether VirtualAlloc is appropriate or not.
Using VirtualAlloc in order to allocate large portion of
memory can be justified sometimes, but such occasions are
rare. Representing VirtualAlloc as an alternative to new
because it allegedly allocates global memory (whatever it
is) or can allocate physical storage, or more stable than
regular `new' is utter nonsense. VirtualAlloc doesn't
allocate global memory; control over physical storage, which
VirtualAlloc provides is indirect and close to none.

Once memory is committed by the Heap*() API is is not
de-committed until the heap is destroyed (or the app
exits). Temp allocation of large objects using new (and
through it Heap*()) could cause the process heap to expand
it's committed use to the point that other apps on the
system need to page more frequently until the app exits.
There is also the issue of heap fragmentation.

Clearly, original poster prefers VirtualAlloc over `new' not
because of heap fragmentation.

`VirtualAlloc' has nothing to do with allocation physical
storage. That's why it's called "Virtual", after all. You
cannot allocate physical storage from user mode code. OS
will use physical storage at its discretion in order to
back up virtual allocations that your application makes.

I expect the OP was talking about the ability to control
the committment of pages directly via Virtual*().
You are correct in that the OS (usually) decides whether a
committed page is in physical memory or paged out to a
paging file.
You are misleading in that Virtual*() does allow you to
committ pages, and does provide some measure of direct
memory allocation via AWE (IIRC AWE memory is never paged
out).

OP's code doesn't mention AWE anywhere. Moreover, AWE isn't
available for Win9x/Me anyway. Op says that he uses
VirtualAlloc on Win9x/Me platforms because it's more
"stable" than `new'.


.



Relevant Pages

  • Re: VirtualAlloc()
    ... VirtualAlloc() can allocate physical storage. ... As any other allocation function it uses `VirtualAlloc' (either directly ... Like memory leaks, in your case, for example. ...
    (microsoft.public.vc.language)
  • Re: Games made with BBC BASIC for Windows
    ... Memory handling, string handling and data structures are part of BBC ... allocate 256 Mbytes of contiguous virtual address space (actual memory ... used is of course typically very much less) but for some reason Wine ... options of VirtualAlloc, presumably because of some limitation in ...
    (comp.sys.acorn.misc)
  • Re: Sudden decrease in available virtual address space
    ... a loss of available memory in the kernel. ... little application that does the same GlobalMemoryStatus and see if it exhibits the same ... >VirtualAlloc, GlobalAlloc, HeapAlloc, LocalAlloc, GlobalReAlloc, ... All the memory that we allocate ...
    (microsoft.public.vc.mfc)
  • Re: Wince address space allocation
    ... malloc allocates memory from your process' heap which is created by ... the kernel within the RAM memory region specified in the MEMORY ... You do not need to do anything else to allocate cacheable memory for ... returned by virtualalloc, it remains the same as returned by normal ...
    (microsoft.public.windowsce.platbuilder)
  • Re: VirtualAlloc or LocalAlloc? Plz Help
    ... the size you need to allocate and how you use the memory you have allocated. ... virtual space which could be process's free virtual space (in 32MB prcess ... VirtualAlloc() to allocate memory from virtual space. ...
    (microsoft.public.windowsce.platbuilder)