Solved

Pointer Q

Posted on 2001-06-07
7
125 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 50 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now