Sign in Join

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

Mailing lists / fpc-pascal / 2010-04

re-newing a pointer

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

spir ☣

Hello,

It seems that once a pointer has been assigned nil, it needs to be (re)allocated using new() before using it to (re)set its target:

...
p := nil;	// pointer to Integer
...
new(p) ; p^ := 1;

Is this hypothesis correct?

Use case: Linked List
nil is used in the "toNext" field of a list's last node, as a kind of end mark. When a new node is appended, I need to "renew" the toNext pointer to let the now forelast node hold a reference to the new one:

	// l is the list, n the new node
	// currently, l.toLast^.toNext = nil
	p := l.toLast;
	new(p^.toNext) ; p^.toNext^ := n;
	l.toLast^ := n;

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Jonas Maebe
to spir ☣
spir ☣ wrote
It seems that once a pointer has been assigned nil, it needs to be (re)allocated using new() before using it to (re)set its target:

You might want to read some tutorials on pointers before continuing you experiments. See e.g.:
* http://lazarus-ccr.sourceforge.net/pascal/pas5f.html
* http://packetstormsecurity.nl/programming-tutorials/Pascal/pascal-tutorial/chap12.htm (the source files mentioned in the text are at http://packetstormsecurity.nl/programming-tutorials/Pascal/pascal-tutorial/ )
* http://geociti.es/SiliconValley/park/3230/pas/pasles02.html
* http://delphi.about.com/od/objectpascalide/a/pointers.htm

Jonas

Reply by email