Sign in Join
Wiki copytemporary

Nil: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Nil

OldNewLine
1+The [[Reserved word|reserved word]] `nil` represents the special [[Constant|value]] of a pointer [[Variable|variable]] not pointing anywhere in particular. In [[FPC]] it is implemented as `pointer(0)` (the numeric value `0`), however the programmer is not supposed to use this fact. In other programming languages, e. g. in C, you write `null`. The terms “null pointer” and “nil pointer” are used interchangeably, even among [[Standard Pascal|Pascal]] programmers.
2+
3+There are two popular explanations of `nil`’s etymology. One says, `nil` is short for the Latin word “nihil” meaning “nothing”. The other suggests, `NIL` is an English acronym standing for “not in list.” Maybe, since the German word „Null“ stands for the digit “zero”, and in order to avoid confusion, or to distinguish between the concept and value, the word `nil` was chosen. At any rate, this does not have any implications while programming.
4+
5+## Assignment compatibility
6+
7+`nil` can be of course [[Becomes|assigned]] to a [[Pointer|pointer]] variable, but also to other types, which are in fact pointers, but their usage is more convenient. For instance [[Dynamic array|dynamic arrays]] or [[Class|classes]]:
8+
9+``` pascal
10+program nilDemo(input, output, stderr);
11+var
12+ loc: pointer;
13+ chk: array of boolean;
14+ msg: PChar;
15+ prc: TProcedure;
16+ obj: TObject;
17+begin
18+ // point to nothing
19+ loc := nil;
20+ // clears dynamic array
21+ chk := nil;
22+ // empty string
23+ msg := nil;
24+ // procedural variable not referencing any procedure
25+ prc := nil;
26+ // loses reference to object
27+ obj := nil;
28+end.
29+```
30+
31+Note, assigning `nil` to a dynamic array is virtually equivalent to the [[Procedure|procedure]] invocation `setLength(dynamicArrayVariable, 0)`. The values of the array are lost, if the reference count of `dynamicArrayVariable` has hit zero. However, there is no comparable mechanism for other types, e. g. assigning `nil` to a `class` or `pointer` variable *will not* release (i. e. de-allocate) the memory that is possibly been occupied by the referenced structure.
32+
33+## Application
34+
35+In Pascal you usually do not write expressions like `pointerVariable =: nil` but use more explanatory [[Identifier|identifiers]]. The [[Function|function]] [[Assigned|system.assigned]] will be replaced by the exact same expression, but conceals the fact a variable is (*implemented* as) a pointer. So its usage is optional.
36+
37+The [[Routine|routine]] [[FreeAndNil|SysUtils.FreeAndNil]] will call a class’s `free` routine and assign `nil` to the handed pointer (variable of type `class`). Although it is a good idea, to clear pointers which do not point to valid objects anymore, this can make debugging more difficult since there is no pointer available pointing to the address at which a certain object used to be.
38+
39+## See also
40+
41+- [[^]]
42+- [`system.returnNilIfGrowHeapFails`](https://www.freepascal.org/docs-html/rtl/system/returnnilifgrowheapfails.html)
43+- `{$objectChecks on}`
44+- [[Nullable types|nullable types]]
45+
46+---
47+*From the Free Pascal wiki, page "Nil", as it stood on 2022-01-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history