flat assembler
Message board for the users of flat assembler.

Index > Windows > heap & stack

Author
Thread Post new topic Reply to topic
eskizo



Joined: 22 Nov 2005
Posts: 59
eskizo 18 Sep 2006, 16:55
hi,

whats the main diference bitween HEAP and STACK ?
where is heap on program memory, or is it from system memory ?

I know its newbie question but I havent found the answer anywhere!
Post 18 Sep 2006, 16:55
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 18 Sep 2006, 21:28
The stack is local memory that is allocated per process, it is usually a fixed size (1mb or something?), when you do push/pop this is using the stack. In Windows there is a process heap and you can use the system memory (heap). The stack is generally faster because it's already allocated, but it's very limited to how much it can hold.

I am sure some one like Tomasz will come and expand on this Smile.

_________________
redghost.ca
Post 18 Sep 2006, 21:28
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Sep 2006, 22:02
stack is thing that is used with "PUSH", "POP", "CALL"s return address, etc. It's a thing on which you can add dword sized things ("push" them), and then remove then is reversed order as you was adding them ("pop" them). ESP register is pointing to things that was last added to stack (it's called "top of stack). when you push something to stack, you do this:
1. subtract size of something from ESP
2. copy something to memory pointet by ESP
and when you pop something from stack
1. copy something from memory pointet by ESP to where you want it
2. add size of something to ESP
... hope that clears things a little, altough i realize it's far from enough explaination.

heap is just bunch of memory that is used with things like C's "alloc()", "realloc()", eg. dynamic memory handling. for example you say - now i need X bytes of memory. Value of X is not known during compilation (for example, it depends on user input). So you just call alloc with argument of X, and it allocates given amount of memory (from "heap"), and returns pointer to you. After you are done with memory, you call free and memory is "freed", you can no longer use it.

This is implemented by allocating one big chunk of memory first, from which "alloc" takes how much it needs and "free" puts it backs. That chunk is called "heap"
Post 18 Sep 2006, 22:02
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
eskizo



Joined: 22 Nov 2005
Posts: 59
eskizo 20 Sep 2006, 13:13
hi angain!

I don't want to create a new thread to ask up this:

- Once the stack is a fixed memory block, can the program point static variables to stack begining (the last pointer used) to save fixed memory size ?
- Is HeapAlloc better than Global/LocalAlloc ? I've seen somewhere that HeapAlloc is the fastest way to get a block of memory.

thankyou all!
Post 20 Sep 2006, 13:13
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 20 Sep 2006, 13:24
first: why to do this? you can set stack smaller and data segment larger Smile
second: don't care, use any of them Wink
Post 20 Sep 2006, 13:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 20 Sep 2006, 16:31
HeapAlloc is the fastest heap allocator, since malloc/GlobalAlloc/LocalAlloc/etc are all just wrappers around it.
Post 20 Sep 2006, 16:31
View user's profile Send private message Reply with quote
eskizo



Joined: 22 Nov 2005
Posts: 59
eskizo 20 Sep 2006, 18:27
vid,

Quote:

first: why to do this? you can set stack smaller and data segment larger
second: don't care, use any of them


first: Good answer, but I am not talking about DOS; It was just to know the possibility of doing what I sad... If necessary, I would set CODE section READABLE WRITEABLE to hope all my static variables;
2nd: alright Wink
Post 20 Sep 2006, 18:27
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 20 Sep 2006, 19:49
Data segment = data section Smile
Post 20 Sep 2006, 19:49
View user's profile Send private message Visit poster's website Reply with quote
eskizo



Joined: 22 Nov 2005
Posts: 59
eskizo 20 Sep 2006, 20:03
lol
Post 20 Sep 2006, 20:03
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 21 Sep 2006, 04:04
If you want stack allocation with some kind of procedure there is alloca() from the CRT, it gets destroyed when a procedure leaves but I imagine you would need to manaully balance the stack before doing a 'ret' instruction.

_________________
redghost.ca


Last edited by RedGhost on 23 Sep 2006, 05:02; edited 1 time in total
Post 21 Sep 2006, 04:04
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 21 Sep 2006, 06:45
if you use EBP-pointed stack, then you don't need to balance it:
Code:
;enter procedure
push ebp
mov ebp, esp
sub esp, locals

;eax = alloca(100)
sub esp, 100
mov eax, esp

;return
mov esp, ebp
pop ebp
ret
    
Post 21 Sep 2006, 06:45
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 21 Sep 2006, 19:21
eskizo: What I meant is that when vid mentioned data segment, he wanted to say data section (as I understand it). You wrongly assumed he was talking about DOS.
Post 21 Sep 2006, 19:21
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.