allocating memory for records

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

allocating memory for records

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Vincent Snijders
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Leonardo M. Ramé
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 <[hidden email]> wrote:

> 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  -  [hidden email]
> 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 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Marc Santhoff
In reply to this post by Vincent Snijders
Am Freitag, den 07.07.2006, 19:00 +0200 schrieb Vincent Snijders:
> Marc Santhoff wrote:


> > 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Marc Santhoff
In reply to this post by Leonardo M. Ramé
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Jonas Maebe-2
In reply to this post by Marc Santhoff

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).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Marc Santhoff
Am Freitag, den 07.07.2006, 19:51 +0200 schrieb Jonas Maebe:
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: allocating memory for records

Leonardo M. Ramé
In reply to this post by Marc Santhoff
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 <[hidden email]> wrote:

> 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  -  [hidden email]
> 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 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal