Does anyone know why the following code fails? The strange thing is that with the flag set for HEAP_GENERATE_EXCEPTIONS the return value is still 0. It seems to ignore both STATUS_NO_MEMORY and STATUS_ACCESS_VIOLATION .

The value of FileSize is A0000000h Is this too large a value for this proc?

Any help or suggestions appreciated.

invoke HeapCreate,HEAP_GENERATE_EXCEPTIONS,FileSize,0
mov hHeap,eax
cmp eax,00h
jz @f

invoke HeapAlloc,hHeap,HEAP_GENERATE_EXCEPTIONS,FileSize

jmp @f

jmp @f

mov pMemory,eax


invoke HeapSize,hHeap,NULL,pMemory
invoke HeapFree,hHeap,NULL,pMemory


best regards,

Posted on 2002-08-09 00:24:14 by czDrillard
Since private address space is only 2 GB (= 80000000h bytes) your size is too large. Possibly it runs on WinXP with the 3 GB option set.
Posted on 2002-08-09 00:35:19 by japheth
Oops! my mistake, FileSize==A000000h (167,772,160d) bytes. I put too may zeros in my post.

best regards,

Posted on 2002-08-09 01:32:55 by czDrillard
Try getlasterror.

HANDLE HeapCreate(
DWORD flOptions, // heap allocation flag
DWORD dwInitialSize, // initial heap size
DWORD dwMaximumSize // maximum heap size

I think dwMaximumSize => dwInitialSize
Posted on 2002-08-11 03:55:00 by Roy Cline
Also, the win32 heap memory management functions (heapalloc, globalalloc, whatever) cannot allocate blocks larger than "almost 256 megabyte", iirc... there's info on it somewhere.
Posted on 2002-08-11 07:19:11 by f0dder
Does it have anything to do with you calling HeapFree almost immediately after the HeapAlloc?

Just a thought...

PS. I believe the limit for a single HeapAlloc is 104 bytes short of 256 MB, it's in the knowledge base at MSDN. Just search for HeapAlloc and it'll pull the article up.

Posted on 2002-08-11 08:15:40 by chorus
Hello everybody,

First, thanks for all replies, in the end because I am allocating 160 meg and want to be flexible enough to allocate even more I used a different method which I will say at the end.

Roy Cline: I took the following exerpt from the SDK. I asssumed this to mean that if I want to allocate more than 0x7fff8 bytes then I must zero the values when calling HeapCreate.

I tried using FileSize == 0a000000h for initial value. HeapAlloc still fails. There is no error in HeapCreate, error is in HeapAlloc
from the SDK:

Number of bytes to be allocated.
If the heap specified by the hHeap parameter is a "non-growable" heap, dwBytes must be less than 0x7FFF8. You create a non-growable heap by calling the HeapCreate function with a nonzero value.

f0dder: I didn't discover the "almost 256 megabyte" upper limit of HeapAlloc, but I'm not so sure 160 megabyte is almost 256 megabyte :)

chorus: function fails at HeapAlloc, return value says so. Therefore I think it might have not much to do with HeapFree. But I thank you for the thought because I have non at this time.

What I ended up doing, and it seems to work very fast was allocate one page with global allocate. I then fill this page with cz's then recursively write one page at a time to disk until file is full. It appears to work well and it doesn't matter how big the file is although granularity is only one page that's ok. Of course it could allocate different amounts less or more than one page.

Like I said this works, maybe not the best way and if any ideas please let me know.

best regards,

Posted on 2002-08-11 12:25:28 by czDrillard

I didn't discover the "almost 256 megabyte" upper limit of HeapAlloc, but I'm not so sure 160 megabyte is almost 256 megabyte :)

True, 160 is some way from 256. But the limit is there, documented in MSDN - I'm just too lazy to hunt down the URL for you.

Since GlobalAlloc internally calls HeapAlloc, if one fails so should the other.

Anyway, if you just want to fill a file with a repeating pattern, allocating "filesize" bytes is a *waste*, and rather stupid... I dunno what the optimal size is, but a couple of pages is probably best (you'll want to have a large enough buffersize so you don't spend too long in r3<>r0 switches - shouldn't matter too much when you're writing a few pages, but writing ie a single byte at a time will be slow).
Posted on 2002-08-11 12:55:53 by f0dder
czDrillard, by your original post I thought that the code was making it through the tests for STATUS_NO_MEMORY and STATUS_ACCESS_VIOLATION (you said the code "ignores" it). My bad.

f0dder and those who are interested, this is the url about the 256 MB limit for win9x and HeapAlloc:;EN-US;Q198959&

Posted on 2002-08-11 14:07:25 by chorus

I agree with your comment.

I was try to say not to create and alloc the "big" heap in one call.


There by knowing more closely the problem area.

I hope works well. Sorry for my unclear comment.
Posted on 2002-08-13 20:31:52 by Roy Cline