Sign in Join

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

Mailing lists / fpc-pascal / 2011-12

initializing pointer values

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Unknown author

On implementing a linked list I hit to some unexpected (doesn't mean incorrect though) behaviour.

 ptest = ^test;
 test = record
 p:
pointer;
 end;
 new(mytest);

When I allocate "mytest" the pointer
variable "p" is initialized as $ffffffff instead of nil as I did suspect. In my code though I am relying on the fact that new pointers are indeed nil so I can allocate them accordingly. How can I force and rely on the fact that p is always nil? What is the correct procedure here?

TIA, Darius

Reply by email

Michael Van Canneyt
to Unknown author
Show quoted text 19 lines
On implementing a linked list I hit to some unexpected (doesn't mean incorrect though) behaviour.     ptest = ^test;   test = record     p: pointer;   end;     new(mytest);   When I allocate "mytest" the pointer variable "p" is initialized as $ffffffff instead of nil as I did suspect. In my code though I am relying on the fact that new pointers are indeed nil so I can allocate them accordingly. How can I force and rely on the fact that p is always nil? What is the correct procedure here?

Newly allocated memory is never zeroed out. You must do this manually with FillChar or FillWord. This is only done automatically for class instances.

I will add a note to the docs.

Michael.

Reply by email

Unknown author
to Michael Van Canneyt
Michael Van Canneyt wrote
On Fri, 16 Dec 2011,
[1] wrote
> On implementing a linked list I

hit to some unexpected (doesn't mean incorrect though) behaviour. ptest = ^test; test = record p: pointer; end; new(mytest); When I allocate "mytest" the pointer variable "p" is initialized as $ffffffff instead of nil as I did suspect. In my code though I am relying on the fact that new pointers are indeed nil so I can allocate them accordingly. How can I force and rely on the fact that p is always nil? What is the correct procedure here?

Newly allocated memory is never zeroed out. You

must do this manually with FillChar or FillWord.

This is only done

automatically for class instances.

Show quoted text 5 lines
I will add a note to the docs. Michael.

I wasn't sure about this, but now I know.

Thanks Michael.

Links:
------
[1]

Reply by email

Unknown author
to Michael Van Canneyt: Newly allocated memory is never zeroed out. You must do this manually with FillChar or…
Show quoted text 34 lines
On Fri, 16 Dec 2011, wrote: > > On implementing a linked list I hit to some unexpected (doesn't mean > incorrect though) behaviour. > >   > >   ptest = ^test; >   test = record >     p: pointer; >   end; > >   > >   new(mytest); > >   > > When I allocate "mytest" the pointer variable "p" is initialized as > $ffffffff instead of nil as I did suspect. In my code though I am > relying on the fact that new pointers are indeed nil so I can allocate > them accordingly. How can I force and rely on the fact that > p is always nil? What is the correct procedure here? Newly allocated memory is never zeroed out. You must do this manually with FillChar or FillWord. This is only done automatically for class instances. I will add a note to the docs. Michael.

Also you can Zero out records using:

var r: TSomeRec = ();

Or access the record fields and nil out the ones you want, but () gets it all done in one shot.

Reply by email