Is undefined behavior only an issue if you are deploying on several platforms?

前端 未结 12 1035
谎友^
谎友^ 2020-12-24 10:47

Most of the conversations around undefined behavior (UB) talk about how there are some platforms that can do this, or some compilers do that.

What i

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 11:11

    "Software that doesn't change, isn't being used."

    If you are doing something unusual with pointers, there's probably a way to use casts to define what you want. Because of their nature, they will not be "whatever the compiler did with the UB the first time". For example, when you refer to memory pointed at by an uninitialize pointer, you get a random address that is different every time you run the program.

    Undefined behavior generally means you are doing something tricky, and you would be better off doing the task another way. For instance, this is undefined:

    printf("%d %d", ++i, ++i);
    

    It's hard to know what the intent would even be here, and should be re-thought.

提交回复
热议问题