Difference between revisions of "Nil"

From Lazarus wiki
Jump to navigationJump to search
(refactor)
Line 1: Line 1:
 
{{Nil}}
 
{{Nil}}
  
'''Nil''' is an empty [[Pointer|pointer]]. The nil pointer is pointer that does not point to any object, [[Variable|variable]] ... It is pointer that is currently pointing to nothing.  
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> represents the special [[Constant|value]] of a pointer [[Variable|variable]] not pointing anywhere particular.
 +
In [[FPC]] it is implemented as <syntaxhighlight lang="pascal" enclose="none">pointer(0)</syntaxhighlight> (the numeric value <syntaxhighlight lang="pascal" enclose="none">0</syntaxhighlight>), however the programmer is not supposed to use this fact.
 +
In other programming languages, e.g in C, someone writes <syntaxhighlight lang="C" enclose="none">null</syntaxhighlight>.
 +
The terms “null pointer” or “nil pointer” are used interchangeably, even among [[Standard Pascal|Pascal]] programmers.
  
 +
There are two popular explanations of <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight>'s etymology.
 +
One says, nil is short for the Latin word “nihil” meaning “nothing”.
 +
The other suggests, <syntaxhighlight lang="pascal" enclose="none">NIL</syntaxhighlight> 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 distinguish between the concept and value, the word <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> was chosen.
 +
At any rate, this does not have any implications while programming.
  
== Read more ==
+
== assignment compatibility ==
 +
<syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> can be of course [[Becomes|assigned]] to a [[Pointer|<syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight>]] 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]]:
 +
<syntaxhighlight lang="pascal" line highlight="8,10,12,14">
 +
program nilDemo(input, output, stderr);
 +
var
 +
loc: pointer;
 +
chk: array of boolean;
 +
msg: PChar;
 +
obj: TObject;
 +
begin
 +
loc := nil;
 +
// clears dynamic array
 +
chk := nil;
 +
// empty string
 +
msg := nil;
 +
// loses reference to object
 +
obj := nil;
 +
end.
 +
</syntaxhighlight>
 +
Note, assigning <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> to a dynamic array is equivalent to the statement <syntaxhighlight lang="pascal" enclose="none">setLength(dynamicArrayVariable, 0)</syntaxhighlight>.
 +
The values of the array are lost.
 +
However, there is no comparable mechanism for other types, e.g. assigning <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> to a <syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight> or <syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight> variable ''will not'' release (de-allocate) the memory that is possibly been occupied by the referenced structure.
  
* [[Assigned]] returns [[False|false]] if a reference is nil.
+
== application ==
* [[FreeAndNil]] free memory for an object and set it to nil.
+
In a Pascal's manner you usually do not write the expression <syntaxhighlight lang="pascal" enclose="none">pointerVariable = nil</syntaxhighlight> but use more explanatory identifiers.
 +
The routine [[Assigned|<syntaxhighlight lang="pascal" enclose="none">system.assigned</syntaxhighlight>]] will be replaced by the exact same statement, but conceals the fact a variable is (''implemented'' as) a pointer.
 +
So its usage is optional.
 +
 
 +
The routine [[FreeAndNil|<syntaxhighlight lang="pascal" enclose="none">sysUtils.freeAndNil</syntaxhighlight>]] will call a class's <syntaxhighlight lang="pascal" enclose="none">free</syntaxhighlight> routine and assign <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> to the handed pointer (variable of type <syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>).
 +
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 a certain object used to be.
 +
 
 +
== see also ==
 +
* [[^|<syntaxhighlight lang="pascal" enclose="none">^</syntaxhighlight>]]
 +
* {{Doc|package=RTL|unit=system|identifier=returnnilifgrowheapfails|text=<syntaxhighlight lang="pascal" enclose="none">system.returnNilIfGrowHeapFails</syntaxhighlight>}}
 +
* <syntaxhighlight lang="pascal" enclose="none">{$objectChecks on}</syntaxhighlight>

Revision as of 18:04, 17 November 2018

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

The reserved word nil represents the special value of a pointer variable not pointing anywhere 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, someone writes null. The terms “null pointer” or “nil pointer” are used interchangeably, even among Pascal programmers.

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 distinguish between the concept and value, the word nil was chosen. At any rate, this does not have any implications while programming.

assignment compatibility

nil can be of course assigned to a pointer variable, but also to other types, which are in fact pointers, but their usage is more convenient. For instance dynamic arrays or classes:

 1program nilDemo(input, output, stderr);
 2var
 3	loc: pointer;
 4	chk: array of boolean;
 5	msg: PChar;
 6	obj: TObject;
 7begin
 8	loc := nil;
 9	// clears dynamic array
10	chk := nil;
11	// empty string
12	msg := nil;
13	// loses reference to object
14	obj := nil;
15end.

Note, assigning nil to a dynamic array is equivalent to the statement setLength(dynamicArrayVariable, 0). The values of the array are lost. However, there is no comparable mechanism for other types, e.g. assigning nil to a class or pointer variable will not release (de-allocate) the memory that is possibly been occupied by the referenced structure.

application

In a Pascal's manner you usually do not write the expression pointerVariable = nil but use more explanatory identifiers. The routine system.assigned will be replaced by the exact same statement, but conceals the fact a variable is (implemented as) a pointer. So its usage is optional.

The routine 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 a certain object used to be.

see also