首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有什么理由使用uint256吗?

有什么理由使用uint256吗?
EN

Ethereum用户
提问于 2019-04-03 00:39:53
回答 1查看 1.9K关注 0票数 1

我在学习如何掌握这个网站。https://github.com/ethereumbook/ethereumbook/blob/develop/09smart-contracts-security.asciidoc

所有的示例代码都使用uint 256。

例如,

代码语言:javascript
复制
    pragma solidity ^0.4.19

    contract EtherStore {

        //unsigned integer type of 256 bits
        uint256 public withdrawalLimit = 1 ether;

        mapping(address => uint256) public lastWithdrawTime;
        mapping(address => uint256) public balances;

        function depositFunds() public payable {
            balances[msg.sender] += msg.value;
            }

        function withdrawFunds (uint256 _weiToWithdraw) public {

            ...
            ...
        }  
    }

uint256 public withdrawalLimit =1醚;

它使用256位,只是分配‘整数1’,我认为这是浪费内存和气体。

如果这个值处理到魏,这些值只需要60位。

我不知道为什么这本书用256位来保存“1”

(谢谢你的帮助:)

EN

回答 1

Ethereum用户

回答已采纳

发布于 2019-04-03 01:02:36

EVM本机工作在256位字.使用较小的,通常会消耗更多的气体,因为有额外的工作要做:不需要的部分需要掩盖。

一个可能的例外情况是将多个值打包到同一个存储槽中。例如struct MyStruct { uint128 foo; uint128 bar; }。在这里,取决于您的访问模式,您可以将两个值存储在同一个存储槽中,也可能不会节省汽油。

注意,在这个特定的代码中,最好的选择是使用常量。这样的话,这个值就不需要被保存在存储中了,这将是一个非常大的节气。

票数 3
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/69201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档