Solved

Pointer Q

Posted on 2001-06-07
7
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 78
shape, triangle, dbctrlgrid 3 31
Browsing a TTreeView in Delphi 5 32
IP without any Dots 1 53
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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