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
Solved

Pointer Q

Posted on 2001-06-07
7
129 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
How to convert wav to mp3 in delphi 9 221
Delphi: ForceDirectory plain function 7 42
Convert MS Word document to a PDF file 9 90
Delphi: sending SMS on android platform 1 34
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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