Is the “struct hack” technically undefined behavior?

前端 未结 8 1693
误落风尘
误落风尘 2020-11-22 13:13

What I am asking about is the well known \"last member of a struct has variable length\" trick. It goes something like this:

struct T {
    int len;
    char         


        
8条回答
  •  执笔经年
    2020-11-22 13:42

    The standard is quite clear that you cannot access things beside the end of an array. (and going via pointers does not help, as you are not allowed to even increment pointers past one after array end).

    And for "working in practise". I've seen gcc/g++ optimizer using this part of the standard thus generating wrong code when meeting this invalid C.

提交回复
热议问题