How do I create a global array of known length on the stack Clark
Posted on 2001-06-26 05:53:00 by Superman_San
Are you sure you wish to create an array on the stack? It is best to leave the stack for push/poping, or local variables in PROCs. You can use the API functions GlobalAlloc, VirtualAlloc, or HeapAlloc, to "create" memory on the fly.

.data?
pMyMemory dd ?

.code
invoke GlobalAlloc, GMEM_FIXED, 1048576
mov pMyMemory, eax

invoke GlobalFree, pMyMemory
Will allocate 1MB of (fixed) memory, which can be accessed directly (pMyMemory is a pointer to the memory when its fixed, if you allocate moveable memory you need to invoke GlobalLock first, and GlobalUnlock after you've finished with it). Mirno
Posted on 2001-06-26 06:15:00 by Mirno
So is it not possible to do it. How does the compilers do it. Is it internally using the heap ? If not then how is it done. Clark
Posted on 2001-06-26 06:23:00 by Superman_San
It is possible to do, but the stack is not really meant for global data, the stack's main purpose is to store temporary data. When you create it, you will not be able to pop things off the stack that were placed on it before the creation of it. You also need to make sure you clean it up properly. What you will need to do is something like this (I'm not entirely sure though): mov pMyArray, esp add esp, 1024 ;Allocate 1k on the stack. If you do this inside a proc, you cannot leave the proc until after you clean it up. It is much easier to let the OS allocate some memory, and use that. The APIs provided are much more flexible, programmer friendly, and OS friendly! Best to leave the stack alone, unless you really want to use it, and know how to... Mirno
Posted on 2001-06-26 06:36:00 by Mirno
Clark Kent, Mirno is right here, the stack is memory that is for another purpose, it gives you parameters for procedure calls, space for LOCAL variables and space to PUSH or POP registers in assembler code. The suggestions of using dynamic memory are good ones and you have far more memory available that way, the stack can be changed but it is usually set at about 1 meg where if you have a lot of memory in a machine, you can allocate much more. The normal GLOBAL access to memory is through the .DATA or .DATA? section and you could if you wished allocate memory here for an array but anything you allocate here makes your program bigger so Mirno's sugestion is a lot more efficient, allocate a GLOBAL handle in the .DATA? section and then allocate memory dynamically using that handle and you have more or less as much memory as you require for the array you have in mind. Regards, hutch@pbq.com.au
Posted on 2001-06-26 06:55:00 by hutch--