Sign in Join

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

Mailing lists / fpc-pascal / 2004-07

Behavior of system.New procedure

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Luiz Américo

If someone can help me with this...
Take the following example:

type
TRec = record
   Apointer:Pointer;
   AInt:Integer;
end;
PRec = ^TRec;
var
   Arec:PRec;
   Return:Boolean;
begin
   New(ARec);
   Return:=ARec^.APointer = nil; (Always True ?)
   Return:=ARec^.Aint = 0; (Always True ?)
end.

My question is:
after allocating a new instance for Arec with New proc, the record fields (APointer and Aint) will be always zero filled or will receive random values?

Thanks in advance

Luiz

Jérémie LEFRANCOIS
to Luiz Américo

You are not to presume anything.

Perhaps it will be random, perhaps it will be zero filled - depends on the compiler amongst other things.
You must do as if it contains the most unexpected data, and therefore initialise the fields *yourself*, and the safer is to do it the quicker.

Regards.

En réponse à Luiz Américo :

Show quoted text 29 lines
If someone can help me with this... Take the following example: type TRec = record Apointer:Pointer; AInt:Integer; end; PRec = ^TRec; var Arec:PRec; Return:Boolean; begin New(ARec); Return:=ARec^.APointer = nil; (Always True ?) Return:=ARec^.Aint = 0; (Always True ?) end. My question is: after allocating a new instance for Arec with New proc, the record fields (APointer and Aint) will be always zero filled or will receive random values? Thanks in advance Luiz

__________________
Jérémie Lefrançois
06 73 27 35 97
Consultant
Altran Technologies

Jeff Pohlmeyer
Show quoted text 7 lines
> After allocating a new instance for Arec with New proc, > the record fields (APointer and Aint) will be always > zero filled or will receive random values? You must do as if it contains the most unexpected data, and therefore initialise the fields *yourself*, and the safer is to do it the quicker.

As a shortcut to initialize all fields to zero (or nil) you can just do this:

  New(ARec);
  FillChar(ARec^, SizeOf(TRec), #0);

__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage! http://promotions.yahoo.com/new_mail

Luiz Américo
to Jeff Pohlmeyer
Jeff Pohlmeyer wrote 18 lines
>>After allocating a new instance for Arec with New proc, >>the record fields (APointer and Aint) will be always >>zero filled or will receive random values? >You must do as if it contains the most unexpected data, >and therefore initialise the fields *yourself*, and the >safer is to do it the quicker. As a shortcut to initialize all fields to zero (or nil) you can just do this: New(ARec); FillChar(ARec^, SizeOf(TRec), #0);

Thanks for the responses.

Luiz