Page 1 of 1

Posted: Wed Mar 20, 2002 8:28 am
by BackupUser
Restored from previous forum. Originally posted by Schlowski.

Hello to all,

I have a simple question, but can't find any answer in the docs and samples - maybe I'm blind :wink:

Whe I allocate memory I get back a pointer to this memory block. Is this memory block fixed, i.e. the address remains the same all over the program run time or does this address change? This would explain to me the function UseMemory for which I normally have no use.

But what happens if memory is moveable when I want to transfer pieces from one to the other memory block, do I have to repeatedly use UseMemory for every single byte to copy? And no, I don't want to copy the whole block from a to b, so I can't use CopyMemory :)

Bjoerg

Posted: Wed Mar 20, 2002 9:29 am
by BackupUser
Restored from previous forum. Originally posted by Pupil.
Hello to all,

I have a simple question, but can't find any answer in the docs and samples - maybe I'm blind :wink:

Whe I allocate memory I get back a pointer to this memory block. Is this memory block fixed, i.e. the address remains the same all over the program run time or does this address change? This would explain to me the function UseMemory for which I normally have no use.

But what happens if memory is moveable when I want to transfer pieces from one to the other memory block, do I have to repeatedly use UseMemory for every single byte to copy? And no, I don't want to copy the whole block from a to b, so I can't use CopyMemory :)
Bjoerg
Yes, the location of the memory is fixed, unless you free the memory and reallocates memory. I'll provide an example to show how you can copy bytes from one place to another. It is important to note that with UseMemory(#memid) you'll get the start address of the allocated memory block and that AllocateMemory() allocates a continous block of memory..
This is just one example, there are many ways to do the same thing.

Code: Select all

Structure char
  byte.b
EndStructure

If AllocateMemory(0,1024,0)
  If AllocateMemory(1,1024,0)
    *buffer0 = UseMemory(0) ; this is for method 1 with peekb and pokeb
    *buffer1 = UseMemory(1) ; this is for method 1 with peekb and pokeb
    *pointer0.char = UseMemory(0) ; This is for method 2 using a structure to access data
    *pointer1.char = UseMemory(1) ; Same as above
    maxvalue.l = UseMemory(0)+1024 ; just to know when to stop the repeat-loop
; Ok now we want to copy every other byte in memblock 0 to the begining of memblock 1
    Repeat
      ; below method 1 using PeekB and PokeB
      PokeB(*buffer1, PeekB(*buffer0)) ; now we moved 1 byte from block 0 to block 1
      *buffer0+2 : *buffer1+1 ; increase the pointers
      ;
      ; Now method 2 using a structure....
      *pointer1\byte = *pointer0\byte ; this equals the PokeB(*buffer1,PeekB(*buffer0) line above!
      *pointer0+2 : *pointer1+1 ; As in method 1 you have to increase the pointers
    until *buffer0 >= maxvalue ; We don't want to go outside our allocatet memory block!
  EndIf
EndIf
Hope you'll be able to interpret and understand this example, please note that the example uses two access methods and that only one is necessary in a real life application, as it's quite useless to copy the same data twice..

Posted: Wed Mar 20, 2002 11:23 am
by BackupUser
Restored from previous forum. Originally posted by Schlowski.
Hello to all,

I have a simple question, but can't find any answer in the docs and samples - maybe I'm blind :wink:

Whe I allocate memory I get back a pointer to this memory block. Is this memory block fixed, i.e. the address remains the same all over the program run time or does this address change? This would explain to me the function UseMemory for which I normally have no use.

But what happens if memory is moveable when I want to transfer pieces from one to the other memory block, do I have to repeatedly use UseMemory for every single byte to copy? And no, I don't want to copy the whole block from a to b, so I can't use CopyMemory :)
Bjoerg
Yes, the location of the memory is fixed, unless you free the memory and reallocates memory. I'll provide an example to show how you can copy bytes from one place to another. It is important to note that with UseMemory(#memid) you'll get the start address of the allocated memory block and that AllocateMemory() allocates a continous block of memory..
This is just one example, there are many ways to do the same thing.

Code: Select all

Structure char
  byte.b
EndStructure

If AllocateMemory(0,1024,0)
  If AllocateMemory(1,1024,0)
    *buffer0 = UseMemory(0) ; this is for method 1 with peekb and pokeb
    *buffer1 = UseMemory(1) ; this is for method 1 with peekb and pokeb
    *pointer0.char = UseMemory(0) ; This is for method 2 using a structure to access data
    *pointer1.char = UseMemory(1) ; Same as above
    maxvalue.l = UseMemory(0)+1024 ; just to know when to stop the repeat-loop
; Ok now we want to copy every other byte in memblock 0 to the begining of memblock 1
    Repeat
      ; below method 1 using PeekB and PokeB
      PokeB(*buffer1, PeekB(*buffer0)) ; now we moved 1 byte from block 0 to block 1
      *buffer0+2 : *buffer1+1 ; increase the pointers
      ;
      ; Now method 2 using a structure....
      *pointer1\byte = *pointer0\byte ; this equals the PokeB(*buffer1,PeekB(*buffer0) line above!
      *pointer0+2 : *pointer1+1 ; As in method 1 you have to increase the pointers
    until *buffer0 >= maxvalue ; We don't want to go outside our allocatet memory block!
  EndIf
EndIf
Hope you'll be able to interpret and understand this example, please note that the example uses two access methods and that only one is necessary in a real life application, as it's quite useless to copy the same data twice..


Posted: Wed Mar 20, 2002 11:27 am
by BackupUser
Restored from previous forum. Originally posted by Schlowski.

Oops, killed my own reply, sorry

Thanks for your example and answer, I wasn't sure about fixed memory allocation.
And the pointer arithmetics with structs are very interesting, didn't realized that this kind of programming is available with PureBasic - the more I leran the more I like it!

BTW, I use the return value from AllocateMemory since this is the starting address of the memory block or 0 if any errors occured so I save one function call