flat assembler
Message board for the users of flat assembler.

Index > Windows > VirtualAlloc or HeapAlloc

Author
Thread Post new topic Reply to topic
Hicel



Joined: 09 Sep 2004
Posts: 55
Hicel
I want to use memory to have something like dynamic arrays. I found that you can use VirtualAlloc and HeapAlloc and that VirtualAlloc is global memory and heapalloc is reserved memory with PE or something.. as given in PE header in SizeOfHeapReserve? Is this right?

What is better VirtualAlloc or HeapAlloc? I don't want to be limited like with HeapAlloc is VirtualAlloc more flexible? I think it's very confusing.

Best Regards,
Hicel
Post 08 Oct 2007, 21:40
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
better for what? HeapAlloc may be better for some things, and VirtualAlloc for other things.

Also, i believe you can allocate more that SizeOfHeapReserve is. This is only size that should be quaranteed by windows to be present, and I wouln't be surprised if it is ignored by windows.
Post 08 Oct 2007, 22:16
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Hicel



Joined: 09 Sep 2004
Posts: 55
Hicel
Better for storing and reading an array of dwords, bytes, values whatever.
Yes you are right my WinXP seems to ignore it or just zeros some mem before. I guess I'll go with Virtual* API.

Thanks,
Hicel
Post 08 Oct 2007, 23:05
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
I used to have some major memory leaking problems with VirtualAlloc, even though I tested it in OllyDbg and it was successfully freeing all of the memory.
Switching to HeapAlloc stopped the leaks, for some reason.

Granted, it could easily be an error on my part; however, HeapAlloc seems to be more "resilient" to bugs.

Just my experience.
Post 09 Oct 2007, 01:17
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Hicel: what should those bytes, words, dwords represent? There is not such simple answer as "HeapAlloc is better than VirtualAlloc" (or vice-versa).

For example VirtualAlloc allows you to reserve block of memory, without commiting it, and then you can slowly commit parts of that block you need. This can sometimes save lot of time. VirtualAlloc is also much less limiting on size of allocated memory, and doesn't suffer so badly from segmentation.

On other side, HeapAlloc is probably faster most of times, can allocate blocks smaller than 4KB, and is more error-tolerant (regardless of whether this is good or bad)
Post 09 Oct 2007, 02:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
HeapAlloc is better for memory blocks <4M. You may allocate more with HeapAlloc on 2k/xp, but you may fail HeapRealloc then to increase size. With VirtualAlloc you may reserve large memory block, and commit as many blocks as you need to save not used memory pages for the system.
Post 09 Oct 2007, 04:54
View user's profile Send private message Visit poster's website Reply with quote
Hicel



Joined: 09 Sep 2004
Posts: 55
Hicel
Sorry for the late reply,

I can just say I used VirtualAlloc now and it works just fine. Thanks for the info about HeapAlloc anyway. vid the bytes words and so on were intended to be used as dynamic array in memory.

Another question
I got used to use edi for screwing arround in this array
Code:
invoke VirtualAlloc,...

mov edi,eax ;store addr in edi
mov edx ; element 5 dword

mov [edi+edx*4],eax ;to save dword
mov eax,[edi+edx*4] ;to load dword    


Is this ok or is another register standard for such things?
also do I need to push/pop edi when changing it to save the old value?
or does it not affect anything (Of course if I don't do something somewhere else in code with edi..)

Regards,
Hicel
Post 14 Oct 2007, 19:25
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
it is ok, just edi, esi and ebx could be better for indexed access, but there is no much difference in 32-bit mode. in 16-bit mode you just may not use edx,eax and ecx as indexing register, but if you do not use 16-bit mode - it's not too interesting. if you use registers in window procedure you must preserve ebx, edi and esi.

regards!
Post 15 Oct 2007, 05:07
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.