2
function element() public  returns (uint) {
  uint test = 20;
  return test;
}

Where would test variable be created ? in memory or stack ?

The funny thing is from testing assembly, I can see that it's not in memory. I am just curious why ?

This is what I found on the internet. enter image description here

1

This description distinguishes between storage and non-storage.

It seems to be using 'memory' in order to refer to non-storage.

It's probably not the best choice of words, as the keyword memory is used in order to indicate that a given variable is allocated on the heap.

In other words, you have 3 data sections:

  1. Storage, where consistent data is located
  2. Stack, where non-consistent data of primitive types is located
  3. Heap, where non-consistent data of non-primitive types is located

Where:

  • 'consistent data' is data which remains valid after the execution is completed
  • 'primitive type' is every variable type except for arrays and structures

The description that you've found simply refers to all non-consistent data, regardless of its actual type, as data which resides memory and not in storage.


UPDATE:

The decision of whether to allocate a variable on the stack or on the heap actually depends on whether that variable is of constant length (which is known during compilation) or not.

For example, the following variables are of constant length:

uint256[2] a;

Foo b;

// Where:
struct Foo {
    uint256 x;
    uint256[2] y;
}

While the following variables are of non-constant length:

uint256[] a;

Foo b;

// Where:
struct Foo {
    uint256 x;
    uint256[] y;
}

The usage of memory is required in order to tell the compiler that a given variable is not in storage, as the compiler assumes storage by default for certain types like arrays and structures.

| improve this answer | |
  • 1
    You probably want to wait for some feedback from a few other members here, as I'm not 100% sure of how accurate my answer is. – goodvibration Oct 2 at 14:08
  • 1
    You can also find more relevant information in this post that I just found. – goodvibration Oct 2 at 14:13
  • Will read it all that very soon and give you my thoughts too here. Before that, if you have time, could you look at this ? ethereum.stackexchange.com/questions/87975/… – Nika Kurashvili Oct 2 at 14:15
  • @NikaKurashvili: I already have. You've asked a similar question before that one, to which I gave you an answer. You should obviously use a similar technical solution (as the one that I gave you on that answer), since those two questions differ only by the fact that you are trying to reference a structure instead of an array. There's a section in the official documentation which explains how the slots are mapped in memory. – goodvibration Oct 2 at 14:24
  • 1
    @NikaKurashvili: There is no support for mapping anywhere other than storage, so this type is obviously not relevant here, as you can obviously not declare it in memory (neither stack nor heap). With regards to strings, I think that they too will reside on the heap, because they are not of constant size. – goodvibration Oct 2 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.