Thursday, September 24, 2009

Play with memory

If you ever need to play with raw memory then this tutorial is a start.
I will explain a bit about AllocMem, ReallocMem, ZeroMemory, MoveMemory, CopyMemory, FillMemory, FillChar, FreeMem methods in this tutorial.
Before we start let's talk about bits 'n' bytes 'n' stuff, you must know that 1 byte is equal to 1 octet or 8 bits, a bit can have value 0(True) or 1(False), now... 1 byte can have value from 0 to 255 that's 256 values(255 + 1(which is 0)).
Word is represented on 2 bytes so it's 2(bytes) * 8(bits per byte) = 16 bits, get it? if not it's ok read on.
Integer(32 bits) is represented on 4 bytes so it's 4(bytes) * 8(bits per byte) = 32 bits it's about time you get the idea.
Int64(integer represented on 64 bits) is represented on 8 bytes so it's 8(bytes) * 8(bits per byte) = 64 bits.
Double value is represented on 8 bytes as Int64.
From now on if someone asks you: What is an octet?
You should be able to answer: 1 Byte or 8 Bits!
Now let's start talking about memory, consider memory as a pack of bytes, let's say you wish to allocate 4 bytes to a pointer(a pointer is a reference to a memory block, it holds the address) then store "2009" value in that allocated memory then free it, in Delphi you can do it this way(or use a memory stream :))
procedure MyFirstMemoryAllocation;
var
  (* the pointer I was talking about *)
  myPointer: Pointer;
begin
  (* allocate memory, self explained, I've
     allocated 4 bytes in this case, but I could
     have also used 2 because the maximum value
     you can store on 2 bytes is 65535, that's
     256 * 256 -1 *)
  myPointer := AllocMem(4);
  (* store the value on "myPointer" using a hard
     cast, I used the ^ sign because I am refering
     to myPointer's value *)
  Integer(myPointer^) := 2009;
  (* we are cleaning garbage from memory! *)
  FreeMem(myPointer, 4);
end;

Memory reallocation is very simple it takes two parameters the first is the pointer(in our case myPointer) and second the new size, i.e. ReallocMem(myPointer, 2);

The ZeroMemory procedure takes two parameters just like ReallocMem and clears value from memory, i.e. ZeroMemory(myPointer, 4(* or any other value, must not exceed the real size of the pointer! *));

MoveMemory basically moves a block of memory form one address to another, i.e. MoveMemory(myPointer, myDestinationPointer, 4);

CopyMemory the name should say everything is same as MoveMemory but instead of moving it just copies... example: CopyMemory(myPointer, mySourcePointer, 4); (* Caution: here the source and destination parameters are swapped! *)

FillMemory and FillChar are basically the same thing, FillMemory calls FillChar they both ask for the pointer(in our case myPointer) the length(the size of our pointer in memory, 4 in our case) and the byte value with which to fill, i.e. FillMemory(myPointer(* the pointer *), 4(* the size *), 255(* the value with which we fill the pointer, can be any value from 0 to 255 *));

FreeMem frees the allocated memory, it takes two parameters first the pointer(myPointer) and second the size(4), an example can be found up at the beginning along with AllocMem.
Warning according to Delphi help FreeMem should not be called directly.
Source: Delphi 7 Help
procedure FreeMem(var P: Pointer[; Size: Integer]);

In Delphi code, FreeMem destroys the variable referenced by P and returns its memory to the heap. If P does not point to memory in the heap, a runtime error occurs. If P points to a structure that includes long strings, variants, dynamic arrays, or interfaces, call Finalize before calling Freemem.
P is a variable of any pointer type previously assigned by the GetMem procedure.
Size specifies the size in bytes of the dynamic variable to dispose of; if specified, it must be exactly the number of bytes previously allocated to that variable by GetMem.
After calling FreeMem, the value of P is undefined.
Note: It is preferable to use the New and Dispose procedures rather than GetMem and FreeMem. When using New and Dispose, there is no need to explicitly call Finalize.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)