[fpc-pascal] Pascal pointer life cycle

Jonas Maebe jonas.maebe at elis.ugent.be
Wed May 5 16:21:06 CEST 2010

On 05 May 2010, at 11:28, spir ☣ wrote:

> So, I have 3 questions:
> -1- Why isn't an unallocated pointer nil?

Pascal, like C and many other languages, does not automatically initialise *all* variables in a program. It's a basic design decision of the language.

> -2- Why is it at all possible to allocate a pointer without setting the target's value?

In many cases allocation and initialisation are two separate actions (e.g. a library routine performs the allocation while user code later on initialises it with the value).

> In other word why doesn't new() take a second argument, value. Or, why not have this argument optional and recommend its use?

You'll have to ask Niklaus Wirth, who defined the language. Keep in mind that everything you add to the language makes it harder to learn (and harder to implement in the compiler).

> -3- Why is it at all possible to deallocate a pointer without "nil-izing" it? Why not have dispose() set it to nil?

It would create unnecessary overhead in many cases. And as you noticed, in this case you can easily create a routine that does this for you if you prefer such behaviour.


More information about the fpc-pascal mailing list