Difference between revisions of "Nil/ru"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{Nil}}
 
{{Nil}}
  
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.
+
[[Reserved_word/ru|Зарезервированное слово]] <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> представляет собой специальное [[Constant/ru|значение]] [[Variable|переменной]]-указателя, не указывающей ни на что конкретное. В [[FPC/ru|FPC]] это реализовано как <syntaxhighlight lang="pascal" enclose="none">pointer(0)</syntaxhighlight> (числовое значение <syntaxhighlight lang="pascal" enclose="none">0</syntaxhighlight>), однако программист не должен использовать этот факт. На других языках программирования, например на C, кто-то пишет <syntaxhighlight lang="C" enclose="none">null</syntaxhighlight>. Термины “null pointer” или “nil pointer” используются взаимозаменяемо, даже среди программистов на Паскале.
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.
+
Есть два популярных объяснения этимологии <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight>. Кто-то говорит: <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> означает латинское слово «nihil», означающее «ничего». Другой предполагает, что <syntaxhighlight lang="pascal" enclose="none">NIL</syntaxhighlight> - это английская аббревиатура, обозначающая “not in list” (нет в списке). Может быть, поскольку немецкое слово «Null» обозначает цифру «ноль», чтобы избежать путаницы или для различия между понятием и значением, было выбрано слово <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight>. Во всяком случае, это не имеет никакой разницы при программировании.
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.
+
__TOC__
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 <syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight> was chosen.
 
At any rate, this does not have any implications while programming.
 
  
 
== assignment compatibility ==
 
== assignment compatibility ==

Revision as of 21:45, 9 May 2019

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

Зарезервированное слово nil представляет собой специальное значение переменной-указателя, не указывающей ни на что конкретное. В FPC это реализовано как pointer(0) (числовое значение 0), однако программист не должен использовать этот факт. На других языках программирования, например на C, кто-то пишет null. Термины “null pointer” или “nil pointer” используются взаимозаменяемо, даже среди программистов на Паскале.

Есть два популярных объяснения этимологии nil. Кто-то говорит: nil означает латинское слово «nihil», означающее «ничего». Другой предполагает, что NIL - это английская аббревиатура, обозначающая “not in list” (нет в списке). Может быть, поскольку немецкое слово «Null» обозначает цифру «ноль», чтобы избежать путаницы или для различия между понятием и значением, было выбрано слово nil. Во всяком случае, это не имеет никакой разницы при программировании.

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	prc: TProcedure;
 7	obj: TObject;
 8begin
 9	// point to nothing
10	loc := nil;
11	// clears dynamic array
12	chk := nil;
13	// empty string
14	msg := nil;
15	// procedural variable not referencing any procedure
16	prc := nil;
17	// loses reference to object
18	obj := nil;
19end.

Note, assigning nil to a dynamic array is virtually equivalent to the 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.

application

In a Pascal's manner you usually do not write expressions like pointerVariable = nil but use more explanatory identifiers. The routine 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.

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