diff options
-rw-r--r-- | docs/abi-spec.rst | 12 | ||||
-rw-r--r-- | docs/control-structures.rst | 25 | ||||
-rw-r--r-- | docs/frequently-asked-questions.rst | 30 |
3 files changed, 31 insertions, 36 deletions
diff --git a/docs/abi-spec.rst b/docs/abi-spec.rst index 68ca8ec4..26293a1f 100644 --- a/docs/abi-spec.rst +++ b/docs/abi-spec.rst @@ -611,15 +611,15 @@ Through ``abi.encodePacked()``, Solidity supports a non-standard packed mode whe This packed mode is mainly used for indexed event parameters. -As an example, the encoding of ``int8(-1), bytes1(0x42), uint16(0x2424), string("Hello, world!")`` results in: +As an example, the encoding of ``int16(-1), bytes1(0x42), uint16(0x03), string("Hello, world!")`` results in: .. code-block:: none - 0xff42242448656c6c6f2c20776f726c6421 - ^^ int8(-1) - ^^ bytes1(0x42) - ^^^^ uint16(0x2424) - ^^^^^^^^^^^^^^^^^^^^^^^^^^ string("Hello, world!") without a length field + 0xffff42000348656c6c6f2c20776f726c6421 + ^^^^ int16(-1) + ^^ bytes1(0x42) + ^^^^ uint16(0x03) + ^^^^^^^^^^^^^^^^^^^^^^^^^^ string("Hello, world!") without a length field More specifically: - Each value type takes as many bytes as its range has. diff --git a/docs/control-structures.rst b/docs/control-structures.rst index f32e7879..46b3a7f1 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -264,6 +264,31 @@ Complications for Arrays and Structs The semantics of assignments are a bit more complicated for non-value types like arrays and structs. Assigning *to* a state variable always creates an independent copy. On the other hand, assigning to a local variable creates an independent copy only for elementary types, i.e. static types that fit into 32 bytes. If structs or arrays (including ``bytes`` and ``string``) are assigned from a state variable to a local variable, the local variable holds a reference to the original state variable. A second assignment to the local variable does not modify the state but only changes the reference. Assignments to members (or elements) of the local variable *do* change the state. +In the example below the call to ``g(x)`` has no effect on ``x`` because it needs +to create an independent copy of the storage value in memory. However ``h(x)`` modifies ``x`` because a reference and +not a copy is passed. + +:: + + pragma solidity >=0.4.16 <0.6.0; + + contract C { + uint[20] x; + + function f() public { + g(x); + h(x); + } + + function g(uint[20] memory y) internal pure { + y[2] = 3; + } + + function h(uint[20] storage y) internal { + y[3] = 4; + } + } + .. index:: ! scoping, declarations, default value .. _default-value: diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index ff2f1fac..11db780c 100644 --- a/docs/frequently-asked-questions.rst +++ b/docs/frequently-asked-questions.rst @@ -96,36 +96,6 @@ In this example:: } } -Can a contract pass an array (static size) or string or ``bytes`` (dynamic size) to another contract? -===================================================================================================== - -Sure. Take care that if you cross the memory / storage boundary, -independent copies will be created:: - - pragma solidity >=0.4.16 <0.6.0; - - contract C { - uint[20] x; - - function f() public { - g(x); - h(x); - } - - function g(uint[20] memory y) internal pure { - y[2] = 3; - } - - function h(uint[20] storage y) internal { - y[3] = 4; - } - } - -The call to ``g(x)`` will not have an effect on ``x`` because it needs -to create an independent copy of the storage value in memory. -On the other hand, ``h(x)`` successfully modifies ``x`` because only -a reference and not a copy is passed. - What does the following strange check do in the Custom Token contract? ====================================================================== |