diff options
author | Araq <rumpf_a@web.de> | 2013-01-23 13:22:06 -0800 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-01-23 13:22:06 -0800 |
commit | 3462040d39ad68b3b79bd66e103ae6b14d066200 (patch) | |
tree | 9ed65eb297cc8c87bd0d6dea1bf93c934a5b490d /doc | |
parent | c1536c12f5fb1f8435bf4b5dd662c2d82e7f7866 (diff) | |
parent | 351e66b76766bd5119b0af91b769fcc7768af097 (diff) | |
download | Nim-3462040d39ad68b3b79bd66e103ae6b14d066200.tar.gz |
Merge pull request #315 from gradha/pr_adds_example_of_addr_cast_usage
Adds example of addr/cast usage and indexes addr.
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/manual.txt | 27 | ||||
-rwxr-xr-x | doc/tut1.txt | 16 |
2 files changed, 27 insertions, 16 deletions
diff --git a/doc/manual.txt b/doc/manual.txt index 80a5642c5..ab8813284 100755 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -2232,7 +2232,6 @@ Type casts ---------- Example: - .. code-block:: nimrod cast[int](x) @@ -2243,11 +2242,27 @@ only needed for low-level programming and are inherently unsafe. The addr operator ----------------- -The `addr` operator returns the address of an l-value. If the -type of the location is ``T``, the `addr` operator result is -of the type ``ptr T``. Taking the address of an object that resides -on the stack is **unsafe**, as the pointer may live longer than the -object on the stack and can thus reference a non-existing object. +The `addr`:idx: operator returns the address of an l-value. If the type of the +location is ``T``, the `addr` operator result is of the type ``ptr T``. An +address is always an untraced reference. Taking the address of an object that +resides on the stack is **unsafe**, as the pointer may live longer than the +object on the stack and can thus reference a non-existing object. You can get +the address of variables, but you can't use it on variables declared through +``let`` statements: + +.. code-block:: nimrod + + let t1 = "Hello" + var + t2 = t1 + t3 : pointer = addr(t2) + echo repr(addr(t2)) + # --> ref 0x7fff6b71b670 --> 0x10bb81050"Hello" + echo cast[ptr string](t3)[] + # --> Hello + # The following line doesn't compile: + echo repr(addr(t1)) + # Error: expression has no address Procedures diff --git a/doc/tut1.txt b/doc/tut1.txt index 2ae7e9540..8edd5c33c 100755 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -1303,14 +1303,10 @@ untraced references are *unsafe*. However for certain low-level operations Traced references are declared with the **ref** keyword, untraced references are declared with the **ptr** keyword. -The empty ``[]`` subscript notation can be used to *derefer* a reference, -meaning to retrieve the item the reference points to. The ``addr`` operator -returns the address of an item. An address is always an untraced reference: -``addr`` is an *unsafe* feature. - -The ``.`` (access a tuple/object field operator) -and ``[]`` (array/string/sequence index operator) operators perform implicit -dereferencing operations for reference types: +The empty ``[]`` subscript notation can be used to *derefer* a reference, +meaning to retrieve the item the reference points to. The ``.`` (access a +tuple/object field operator) and ``[]`` (array/string/sequence index operator) +operators perform implicit dereferencing operations for reference types: .. code-block:: nimrod @@ -1327,8 +1323,8 @@ dereferencing operations for reference types: To allocate a new traced object, the built-in procedure ``new`` has to be used. To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and -``realloc`` can be used. The documentation of the system module contains -further information. +``realloc`` can be used. The documentation of the `system <system.html>`_ +module contains further information. If a reference points to *nothing*, it has the value ``nil``. |