Why is gcc allowed to speculatively load from a struct?

前端 未结 6 752
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-06 20:29

Example Showing the gcc Optimization and User Code that May Fault

The function \'foo\' in the snippet below will load only one of the struct members A or B; well at le

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 21:29

    No, if *P is allocated correctly P->B will never be in unallocated memory. It might not be initialized, that is all.

    The compiler has every right to do what they do. The only thing that is not allowed is to oops about the access of P->B with the excuse that it is not initialized. But what and how they do all of this is under the discretion of the implementation and not your concern.

    If you cast a pointer to a block returned by malloc to Pair* that is not guaranteed to be wide enough to hold a Pair the behavior of your program is undefined.

提交回复
热议问题