Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-07

allocating memory for records

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

excuse me for asking dumb questions, but over using objects only for years I forgot:

If I need to allocate memory for a record type variable, what functions should get used?

Is the GetMem/FreeMem pair okay or is there something more appropriate?

var
  x: PMyRec
begin
  GetMem(x, sizeof(x));

would be my best guess atm.

And another related question:

If a record has a String field, how is memory allacation handled?

MyRec = record
a: integer; /* very clear */
b: string; /* not clear, how much bytes are needed? */ end;

TIA,
Marc

Vincent Snijders
to Marc Santhoff
Marc Santhoff wrote 29 lines
Hi, excuse me for asking dumb questions, but over using objects only for years I forgot: If I need to allocate memory for a record type variable, what functions should get used? Is the GetMem/FreeMem pair okay or is there something more appropriate? var x: PMyRec begin GetMem(x, sizeof(x)); would be my best guess atm. And another related question: If a record has a String field, how is memory allacation handled? MyRec = record a: integer; /* very clear */ b: string; /* not clear, how much bytes are needed? */ end;
I would use:
   new(x);
and
   dispose(x);
I guess the memory will initialized and finalized for strings correctly
in that case too, but I am not sure;

Vincent.

Leonardo M. Ram�
to Vincent Snijders

Marc,
you don't need to allocate memory for records, you can us as follows:

type
MyRec = record
a: integer; /* very clear */
b: string; /* not clear, how much bytes are needed? */

  end;
var
  myRecVar: MyRec;
begin
  myRecVar.a := 10;
  myRecVar.b := 'Hello World!';
end;
--- Vincent Snijders wrote 44 lines
Marc Santhoff wrote: > Hi, > > excuse me for asking dumb questions, but over using objects only for > years I forgot: > > If I need to allocate memory for a record type variable, what functions > should get used? > > Is the GetMem/FreeMem pair okay or is there something more appropriate? > > var > x: PMyRec > begin > GetMem(x, sizeof(x)); > > would be my best guess atm. > > > > And another related question: > > If a record has a String field, how is memory allacation handled? > > MyRec = record > a: integer; /* very clear */ > b: string; /* not clear, how much bytes are needed? */ > end; > > I would use: new(x); and dispose(x); I guess the memory will initialized and finalized for strings correctly in that case too, but I am not sure; Vincent. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Leonardo M. Ramé
http://leonardorame.blogspot.com

__________________________________________________ Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

Marc Santhoff
to Vincent Snijders: I would use: new(x); and dispose(x); I guess the memory will initialized and finalized…
Vincent Snijders wrote
Marc Santhoff wrote:
Show quoted text 15 lines
> And another related question: > > If a record has a String field, how is memory allacation handled? > > MyRec = record > a: integer; /* very clear */ > b: string; /* not clear, how much bytes are needed? */ > end; > > I would use: new(x); and dispose(x);

Ah, yes. I (k)new there was a more common solution. Nothing is more "Wirth" than new() / dispose()!

I guess the memory will initialized and finalized for strings correctly in that case too, but I am not sure;

Anyone else knowing for sure?

Thank you very much Vincent,
Marc

Marc Santhoff
to Leonardo M. Ram�: you don't need to allocate memory for records, you can us as follows: type MyRec = record…
Leonardo M. RamX wrote 16 lines
Marc, you don't need to allocate memory for records, you can us as follows: type MyRec = record a: integer; /* very clear */ b: string; /* not clear, how much bytes are needed? */ end; var myRecVar: MyRec; begin myRecVar.a := 10; myRecVar.b := 'Hello World!'; end;

Yes, I know. But in this case I definitely have to because a static variable disappears outside a function declaring it, so I need a solution that is robust and portable.

But thank you anyways, a helping hand is always welcome.

Marc

Marc Santhoff
to Jonas Maebe
Jonas Maebe wrote 9 lines
On 7 jul 2006, at 19:31, Marc Santhoff wrote: >> I guess the memory will initialized and finalized for strings >> correctly >> in that case too, but I am not sure; > > Anyone else knowing for sure? It's correct (for ansistrings and widestrings).

Wonderful, very nice and handy.

Thanks,
Marc

Leonardo M. Ram�
to Marc Santhoff: Yes, I know. But in this case I definitely have to because a static variable disappears…

You also can create an object an instantiate it in the initialization section of the unit an finalize at the end, if you use this unit, the instance of the created object will not lose it's value:

type
  MyRec = class
  public
    a: integer; /* very clear */
    b: string; /* not clear, how much bytes are needed? */
  end;
var
   myRecVar: MyRec;
initialization
  myRecVar := MyRec.Create;
  myRecVar.a := 10;
  myRecVar.b := 'Hello World!';
finalization
  myRecVar.Free;
--- Marc Santhoff wrote 31 lines
Am Freitag, den 07.07.2006, 10:09 -0700 schrieb Leonardo M. RamX: > Marc, > you don't need to allocate memory for records, you can us as follows: > > type > MyRec = record > a: integer; /* very clear */ > b: string; /* not clear, how much bytes are needed? */ > end; > > var > myRecVar: MyRec; > > begin > myRecVar.a := 10; > myRecVar.b := 'Hello World!'; > end; Yes, I know. But in this case I definitely have to because a static variable disappears outside a function declaring it, so I need a solution that is robust and portable. But thank you anyways, a helping hand is always welcome. Marc _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Leonardo M. Ramé
http://leonardorame.blogspot.com

__________________________________________________ Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com