V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wayh
V2EX  ›  编程

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

  •  
  •   wayh · Aug 3, 2021 · 1388 views
    This topic created in 1729 days ago, the information mentioned may be changed or developed.

    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 replies    2021-08-03 15:23:52 +08:00
    icyalala
        1
    icyalala  
       Aug 3, 2021   ❤️ 3
    不要依赖 undefined behavior 的实现,对于 UB,编译器怎么解释都有可能。
    如果你开了 warning,就会有类似的提示 "Multiple unsequenced modifications"。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   970 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:41 · PVG 04:41 · LAX 13:41 · JFK 16:41
    ♥ Do have faith in what you're doing.