V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mentalidade
V2EX  ›  问与答

问一个 lua 的问题?

  •  
  •   mentalidade · 2018-02-27 11:12:01 +08:00 · 970 次点击
    这是一个创建于 2267 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到一个 lua 代码: local RANDOM_BOUNDARY = sub(tostring({}), 10) 使用 RANDOM_BOUNDARY 作为 multipart/data 的 boundary, 打印一下,看起来仅仅最后几位是随机的,请问什么原理?

    Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
    > do
    >> local a = tostring({})
    >> local b = tostring({})
    >> local c = tostring({})
    >> print(a)
    >> print(b)
    >> print(c)
    >> end
    table: 0x7f8224f00dd0
    table: 0x7f8224f012e0
    table: 0x7f8224f01380
    > 
    > 
    > do
    >> local a = tostring({})
    >> local b = tostring({})
    >> local c = tostring({})
    >> print(a)
    >> print(b)
    >> print(c)
    >> end
    table: 0x7f8224c07190
    table: 0x7f8224c07230
    table: 0x7f8224c072d0
    
    2 条回复    2018-02-27 12:27:00 +08:00
    newtype0092
        1
    newtype0092  
       2018-02-27 11:47:52 +08:00   ❤️ 1
    这是内存地址啊,不停的创建空 table,然后取地址,都是连续的哪里随机了。
    ai277014717
        2
    ai277014717  
       2018-02-27 12:27:00 +08:00   ❤️ 1
    内存地址,正常来说你会发现 c-b 应该等于 b-a,一个 table 的内存大小应该就是这个差值乘以一个常数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3213 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:42 · PVG 21:42 · LAX 06:42 · JFK 09:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.