Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaring struct at null memory location #15155

Open
zemse opened this issue May 28, 2024 · 0 comments
Open

Declaring struct at null memory location #15155

zemse opened this issue May 28, 2024 · 0 comments
Labels

Comments

@zemse
Copy link
Contributor

zemse commented May 28, 2024

Abstract

Currently, memory is allocated always when someone is declaring a struct. Following is an example when someone might not want this behaviour.

struct MyStruct {
  uint256 a;
}

function run() {
  // fmp: 0x80
  bytes memory data;
  // fmp: 0x80, data: 0x60
  data = abi.encode(1); // allocates 2 words on memory i.e. len and actual data word
  // fmp: 0xC0, data: 0x80
  MyStruct memory v;
  // fmp: 0xE0, data: 0x80, v: 0xC0
  assembly { v := add(data, 0x20) }
  // fmp: 0xE0, data: 0x80, v: 0xA0
}

Motivation

In some cases, the dev knows that a certain memory slice will be always immutable (probably because the original variable will be dropped). However, the struct declaration advances free memory pointer on declaration. If there would be a way to prevent that, it would be great.

Specification

MyStruct memory v = null;
assembly { v := add(data, 0x20) }

A keyword null can be introduced in Solidity, which would make the struct variable point to 0x00 memory location.

Backwards Compatibility

None to my knowledge.

@zemse zemse added the feature label May 28, 2024
@zemse zemse changed the title Declaring struct at certain memory location Declaring struct at null memory location May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant