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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

MadshiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
topkapiAuthor Commented:
that is a very good idea. i'm going to try that out...
0
topkapiAuthor Commented:
thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.