sizeof block pointed by pointer

i have a

  P : Pointer;

variable

i want to know how big the memory block is, wich P points to.

like when i call
  GetMem(P, 10);
this will be 10 bytes.

but all i have is the pointer.

How do i find out the size of the memoryblock?
topkapiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MadshiConnect With a Mentor Commented:
Well, if you set your own memory manager, why don't you allocate 4 bytes more than requested and write the length of the allocated block to these 4 bytes? (Of course you must give back then "pointer + 4" as the result of GetMem.) This way it should work with ALL current and future Delphi versions...

Regards, Madshi.
0
 
kretzschmarCommented:
hi topkapi,

i guess no chance by an untyped pointer, but maybe i'm wrong, therefore listening.

meikl ;-)
0
 
LischkeCommented:
I don't know why you need this but you can use this stuff. Be carful, though, as this is based on the internal memory manager handling. If there is another memory manager set then this will surely differ. Also between version might be changes (this is information from D5):

type
  PUsed = ^TUsed;
  TUsed = packed record
    sizeFlags: Integer;
  end;

const
  cAlign        = 4;
  cThisUsedFlag = 2;
  cPrevFreeFlag = 1;
  cFillerFlag   = Integer($80000000);
  cFlags        = cThisUsedFlag or cPrevFreeFlag or cFillerFlag;
  cSmallSize    = 4*1024;
  cDecommitMin  = 15*1024;


function GetPointerSize(P: Pointer): Cardinal;

begin
  Result := PUsed(PChar(P) - SizeOf(TUsed)).sizeFlags and not cFlags - SizeOf(TUsed);
end;

Ciao, Mike
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
topkapiAuthor Commented:
im experimenting with GetMem and FreeMem, in combination with a wrapper around the delphi TMemoryManager...

nothing fancy, just to track memory usage in ann application.
0
 
topkapiAuthor Commented:
that is a very good idea. i'm going to try that out...
0
 
topkapiAuthor Commented:
thanks!
0
All Courses

From novice to tech pro — start learning today.