Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointer Q

Posted on 2001-06-07
7
Medium Priority
?
136 Views
Last Modified: 2010-04-06
Let's say, I define this:

type TMyRecord = record
      SomeString: String[255];
      AnotherOne: String[255];
      AnInteger: Integer;
     end;
type PMyRecord = ^TMyRecord;

what's the best way to use this?

So far I use:

var pmr: PMyRecord;
begin
  GetMem(pmr, SizeOf(TMyRecord));
  //Do my Stuff
  FreeMem(pmr, SizeOf(pmr));
end;

Now where's the difference to using

New(pmr);
Dispose(pmr);

Regards,
Oli
0
Comment
Question by:Oli2
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 150 total points
ID: 6163285
Hi Oli,

Example :

Type
  MyBuffer = Array[1..5000] of Byte;

VAR
 Buffer = ^MyBuffer;


Begin
  New(Buffer );    // reserve 5000 Byte for the Buffer
 

  GetMem ( Buffer,100);   // reserve only 100 byte for the buffer
 
 
End;
0
 
LVL 1

Author Comment

by:Oli2
ID: 6163294
okay, got that.

What about my code, I'm not sure if it's right to
set the Size to SizeOf(TMyRecord) at GetMem() and
to set it to SizeOf(pmr) at FreeMem().

Is that okay, or would it be better to set the Size
to SizeOf(TMyRecord) at FreeMem() also?

Regards,
Oli
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6163311
Hi Oli2,

Just use new and dispose.

I use GetMem, Ich I receive a Packet via Socket, and the Size of this packet is smaller than the buffer.
So I dont need to allocate 50000 byte.

Best Regards

Cesario
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 6163351
Hi Oli,
The right way is to free as much memory as you have got. So:

GetMem(pmr, SizeOf(TMyRecord));
...
FreeMem(pmr, SizeOf(TMyRecord));

Regards, Geo
0
 
LVL 1

Author Comment

by:Oli2
ID: 6163369
thanx for your help.
I accept Cesario's comment as an answer since he was the first to post a comment and he answered my first question about the difference between GetMem() and New().
But also thanx to you, Geo, I hope it's okay to you that I give Cesario the points.

Regards,
Oli
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6163382
Thanx for the points ;.)
0
 
LVL 17

Expert Comment

by:geobul
ID: 6163426
Yes, it's OK.
Regards, Geo
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question