Skip to content

Instantly share code, notes, and snippets.

@gnidan
Last active November 18, 2023 02:19
Show Gist options
  • Save gnidan/b1890c68c8e0825d4a699929ccb2018e to your computer and use it in GitHub Desktop.
Save gnidan/b1890c68c8e0825d4a699929ccb2018e to your computer and use it in GitHub Desktop.
solidity allocation examples

storing things with solidity for fun and confusion

uint64[] with 9 elements

storing [ 10, 11, 12, 13, 14, 15, 16, 17, 18 ]:

   ...
[...0002]: 0000000000000000000000000000000000000000000000000000000000000009
   ...
[...5ace]: 000000000000000d000000000000000c000000000000000b000000000000000a
[...5acf]: 00000000000000110000000000000010000000000000000f000000000000000e
[...5ad0]: 0000000000000000000000000000000000000000000000000000000000000012
   ...

uint240[] with 3 elements

storing [ 9, 10, 11 ]:

  ...
[...0003]: 0000000000000000000000000000000000000000000000000000000000000003
  ...
[...f85b]: 0000000000000000000000000000000000000000000000000000000000000009
[...f85c]: 000000000000000000000000000000000000000000000000000000000000000a
[...f85d]: 000000000000000000000000000000000000000000000000000000000000000b
  ...

string smaller than wordsize

storing "hello world":

  ...
[...0000]: 68656c6c6f20776f726c64000000000000000000000000000000000000000016
  ...

string larger than wordsize

storing "solidity storage is a fun lesson in endianness":

  ...
[...0001]: 000000000000000000000000000000000000000000000000000000000000005d
  ...
[...0cf6]: 736f6c69646974792073746f7261676520697320612066756e206c6573736f6e
[...0cf7]: 20696e20656e6469616e6e657373000000000000000000000000000000000000
  ...

memory allocation

uint64[] with 9 elements

storing [ 13, 14, 15, 16, 17, 18, 19, 20, 21 ]:

   ...
[...0260]: 0000000000000000000000000000000000000000000000000000000000000009
[...0280]: 000000000000000000000000000000000000000000000000000000000000000d
[...02a0]: 000000000000000000000000000000000000000000000000000000000000000e
[...02c0]: 000000000000000000000000000000000000000000000000000000000000000f
[...02e0]: 0000000000000000000000000000000000000000000000000000000000000010
[...0300]: 0000000000000000000000000000000000000000000000000000000000000011
[...0320]: 0000000000000000000000000000000000000000000000000000000000000012
[...0340]: 0000000000000000000000000000000000000000000000000000000000000013
[...0360]: 0000000000000000000000000000000000000000000000000000000000000014
[...0380]: 0000000000000000000000000000000000000000000000000000000000000015
   ...

uint240[] with 3 elements

storing [ 22, 23, 24 ]:

  ...
[...03a0]: 0000000000000000000000000000000000000000000000000000000000000003
[...03c0]: 0000000000000000000000000000000000000000000000000000000000000016
[...03e0]: 0000000000000000000000000000000000000000000000000000000000000017
[...0400]: 0000000000000000000000000000000000000000000000000000000000000018
  ...

string smaller than wordsize

storing "hello world":

  ...
[...0420]: 000000000000000000000000000000000000000000000000000000000000000b
[...0440]: 68656c6c6f20776f726c64000000000000000000000000000000000000000000
  ...

string larger than wordsize

storing "solidity allocation is a fun lesson in endianness":

  ...
[...0460]: 0000000000000000000000000000000000000000000000000000000000000031
[...0480]: 736f6c696469747920616c6c6f636174696f6e20697320612066756e206c6573
[...04a0]: 736f6e20696e20656e6469616e6e657373000000000000000000000000000000
  ...
@cgewecke
Copy link

cgewecke commented May 3, 2018

Oh this is great - this might be the only clear presentation of this topic anywhere.

@gnidan
Copy link
Author

gnidan commented May 3, 2018

Thanks @cgewecke! I'll have to add more examples :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment