aboutsummaryrefslogtreecommitdiffstats
path: root/docs/assembly.rst
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-08-07 18:26:28 +0800
committerchriseth <chris@ethereum.org>2018-08-07 19:37:06 +0800
commit5d5626e6d77f8ad42f0983a3f6b91be78828beee (patch)
treebb3d5f2ca5f6469d072d50934de828174f8ba0b4 /docs/assembly.rst
parentcda3fbda33bc256dce69ec8ae2cecd8e37c70f20 (diff)
downloaddexon-solidity-5d5626e6d77f8ad42f0983a3f6b91be78828beee.tar.gz
dexon-solidity-5d5626e6d77f8ad42f0983a3f6b91be78828beee.tar.zst
dexon-solidity-5d5626e6d77f8ad42f0983a3f6b91be78828beee.zip
Clarify allocation and initial value of free memoy pointer.
Diffstat (limited to 'docs/assembly.rst')
-rw-r--r--docs/assembly.rst13
1 files changed, 11 insertions, 2 deletions
diff --git a/docs/assembly.rst b/docs/assembly.rst
index 5041e72c..91ba076a 100644
--- a/docs/assembly.rst
+++ b/docs/assembly.rst
@@ -609,12 +609,21 @@ first.
Solidity manages memory in a very simple way: There is a "free memory pointer"
at position ``0x40`` in memory. If you want to allocate memory, just use the memory
-from that point on and update the pointer accordingly.
+starting from where this pointer points at and update it accordingly.
+There is no built-in mechanism to release or free allocated memory.
+Here is an assembly snippet that can be used for allocating memory::
+
+ function allocate(length) -> pos {
+ pos := mload(0x40)
+ mstore(0x40, add(pos, length))
+ }
The first 64 bytes of memory can be used as "scratch space" for short-term
allocation. The 32 bytes after the free memory pointer (i.e. starting at ``0x60``)
is meant to be zero permanently and is used as the initial value for
empty dynamic memory arrays.
+This means that the allocatable memory starts at ``0x80``, which is the initial value
+of the free memory pointer.
Elements in memory arrays in Solidity always occupy multiples of 32 bytes (yes, this is
even true for ``byte[]``, but not for ``bytes`` and ``string``). Multi-dimensional memory
@@ -698,7 +707,7 @@ We consider the runtime bytecode of the following Solidity program::
The following assembly will be generated::
{
- mstore(0x40, 0x60) // store the "free memory pointer"
+ mstore(0x40, 0x80) // store the "free memory pointer"
// function dispatcher
switch div(calldataload(0), exp(2, 226))
case 0xb3de648b {