News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

moving memory

Started by gelatine1, December 14, 2013, 12:47:38 AM

Previous topic - Next topic

gelatine1

Imagine I have some memory used to represent an array. What if I want to increase the size of my array? I should move all the data behind it and then insert a new piece of data? or?

Thanks in advance
Jannes

jj2007

Check the HeapReAlloc documentation, and the movsb (or lodsb and stosb) instructions in \Masm32\help\opcodes.chm

Lazy assembler programmers have other options, too:

include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  Dim My$()        ; create a dynamic string array
  xor ecx, ecx
  .Repeat
        Let My$(ecx)=Str$("This is string #%i", ecx)
        inc ecx
  .Until ecx>5
  Insert My$(3)
  Let My$(3)="This string was inserted"
  xor ecx, ecx
  .Repeat
        Print My$(ecx), CrLf$
        inc ecx
  .Until ecx>6
  Inkey
  Exit
end start


Output:
This is string #0
This is string #1
This is string #2
This string was inserted
This is string #3
This is string #4
This is string #5


But of course, under the hood you'll find HeapReAlloc, too ;-)

Tedd

If you only perform a few insertions, you can simply HeapReAlloc and move the necessary data along to make space.
If you expect to make many insertions, it may be better to use a more appropriate data structure, e.g. a linked-list.
Potato2

dedndave

if i have a large array, rather than move the data, i will use a "circular buffer" design
that means you keep a pointer to the beginning (head) and a pointer to the end (tail)
you also know the buffer beginning and end addresses
by adjusting the head and tail pointers, and wrapping around the buffer end to beginning, you create a circular buffer
such a buffer is handy for things like serial I/O
it's much faster to change the value of the pointers than it is to move all that data   :t

gelatine1

Okay and if I have an array, where should I store it? Can I simply choose an arbitrary address? or are there special addresses for this or something?

dedndave

it depends
life-span - is it to exist temporarily, or for the life of the program session?
scope/visiblity - some arrays may be accessed, internal to a specific function - others may be accessed by any function
size - small arrays might be allocated in one of the data sections - larger ones might be on the heap or virtual
initialized/uninitialized - some may have values pre-assigned - some may not

the programmer takes all those things into consideration and chooses the best method

initialized or uninitialized data section, stack, HeapAlloc, VirtualAlloc are some of the most common choices

jj2007

Quote from: gelatine1 on December 14, 2013, 08:15:47 PM
Okay and if I have an array, where should I store it?

If you have an array, it's already stored somewhere:

include \masm32\include\masm32rt.inc

.code
MyArray   REAL8 1.23, 4.56, 7.89, 9.87
start:
   inkey real8$(MyArray)
   exit
end start


Any kind of memory is OK. Check hlhelp.chm, macro categories, memory allocation.

gelatine1

jj2007:
I would like to implement an array myself. So in this case I believe this simply requires one register to hold a pointer to the beginning of the array.

dedndave:
life-span: for the life of the program session.
visibility: global/ public
size: any size
both initialized and uninitialized possible.

Could you tell me where I can find information about the heapalloc and virtual alloc?

Thanks in advance

jj2007

Quote from: gelatine1 on December 15, 2013, 05:06:59 AMCould you tell me where I can find information about the heapalloc and virtual alloc?

Go to the PowerBasic tools page and download the Windows 32-bit API Help File.


hutch--

You have two approaches to moving memory when you are inserting or deleting sections, you can re-alloc at the end of the memory and then move items around to fit OR if its not too big, just allocate a big enough buffer and rewrite the whole lot to the new buffer in the order you require. The second is usually faster but at the price of temporarily allocating twice the memory. Depending on your computer memory size, if its just a couple of meg, use another buffer, if its a gig do it the hard way to preserve memory.