V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wayh
V2EX  ›  编程

这种写法,编译器应该怎么去理解

  •  
  •   wayh · 2021-08-03 15:07:40 +08:00 · 919 次点击
    这是一个创建于 968 天前的主题,其中的信息可能已经有所发展或是发生改变。

    C:

    int a[2] = {0, 0};
    int count = 0;
    
    a[count] = ++count;
    
    // {0,1}
    

    java:

    int[] a = {0, 0};
    int count = 0;
    
    a[count] = ++count;
    
    // {1,0}
    

    js:

    let a = [0,0]
    let count = 0
    
    a[count] = ++count
    
    // [1,0]
    

    我在用 WebStorm 调试 js 的时候发现这个问题,默认运行就和上面写的 js 行为一致,调试的时候就变成了和 C 的行为一致,这到底算哪个的 BUG 呢

    1 条回复    2021-08-03 15:23:52 +08:00
    icyalala
        1
    icyalala  
       2021-08-03 15:23:52 +08:00   ❤️ 3
    不要依赖 undefined behavior 的实现,对于 UB,编译器怎么解释都有可能。
    如果你开了 warning,就会有类似的提示 "Multiple unsequenced modifications"。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1191 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:19 · PVG 07:19 · LAX 16:19 · JFK 19:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.