Home > Back-end >  "Inheritance" in C's structs?
"Inheritance" in C's structs?

Time:09-30

Here I'm a bit confused about this code:

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

struct test_struct {
    uint8_t f;
    uint8_t weird[];
};

int main(void) {
    struct {
        struct test_struct tst;
        uint8_t weird[256];
    } test_in = {};

    printf("%u\n", test_in.weird[0]); // 0

    test_in.tst.weird[0] = 1;

    printf("%u\n", test_in.weird[0]); // 1

    return 0;
}

I didn't know that it is possible to use struct's fields this way, so I have two questions:

  • How is it called in C?
  • And, of course, how does it work? (Why weird field was changed when I don't change it directly, I thought these are two different fields?)

CodePudding user response:

Here I'm a bit confused about this code:

The short answer is: the code has undefined behavior.

How is it called in C? How does it work?

  • struct test_struct is defined with its last member as an array of unspecified length: uint8_t weird[]; This member is called a flexible array member, not to be confused with a variable length array.

6.7.2 Type specifiers

[...]

20     As a special case, the last member of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

  • if you allocate such a structure from the heap with extra space for array elements, these elements can be accessed via the weird member up to the number of elements thus allocated.

  • The C Standard mandates that such a structure can only be defined as a member of another structure or union if it appears as the last member of said aggregate. In the posted code, the programmer violates this constraint, so accessing elements of test_in.tst.weird has undefined behavior, and so does accessing elements of test_in.weird.

  • The programmer also assumes that the test_in.tst.weird array and the test_in.weird array overlap exactly, which may be the case but is not guaranteed, nor supported: code relying on this type of aliasing has undefined behavior as well.

  • In your example, assuming the compiler accepts the empty initializer {} (part of the next C Standard and borrowed from C ), it seems to work as expected, but this is not guaranteed and alignment issues may cause it to fail as shown in the modified version below:

#include <stdint.h>
#include <stdio.h>

struct test_struct {
    uint8_t f;
    uint8_t weird[];
};

struct test_struct1 {
    int x;
    uint8_t f;
    uint8_t weird[];
};

int main(void) {
    struct {
        struct test_struct tst;
        uint8_t weird[256];
    } test_in = {};

    struct {
        struct test_struct1 tst;
        uint8_t weird[256];
    } test_in1 = {};

    printf("modifying test_in.weird[0]:\n");
    printf("%u\n", test_in.weird[0]); // 0
    test_in.tst.weird[0] = 1;
    printf("%u\n", test_in.weird[0]); // 1

    printf("modifying test_in1.weird[0]:\n");
    printf("%u\n", test_in1.weird[0]); // 0
    test_in1.tst.weird[0] = 1;
    printf("%u\n", test_in1.weird[0]); // 0?

    return 0;
}

Output:

chqrlie$ make 220930-flexible.run
clang -O3 -std=c11 -Weverything -o 220930-flexible 220930-flexible.c
220930-flexible.c:17:28: warning: field 'tst' with variable sized type 'struct test_struct' not at
      the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
        struct test_struct tst;
                           ^
220930-flexible.c:19:17: warning: use of GNU empty initializer extension [-Wgnu-empty-initializer]
    } test_in = {};
                ^
220930-flexible.c:22:29: warning: field 'tst' with variable sized type 'struct test_struct1' not
      at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
        struct test_struct1 tst;
                            ^
220930-flexible.c:24:18: warning: use of GNU empty initializer extension [-Wgnu-empty-initializer]
    } test_in1 = {};
                 ^
4 warnings generated.
modifying test_in.weird[0]:
0
1
modifying test_in1.weird[0]:
0
0

CodePudding user response:

struct test_struct {
    uint8_t f;
    uint8_t weird[];
};

int main(void) {
    struct {
        struct test_struct tst;
        uint8_t weird[256];
    } test_in = {};

Effectively, before there were FAM's in the language, what you've declared is:

int main(void) {
    struct {
        struct { uint8_t f; } tst;
        union {
            uint8_t weird0[1]; // any non-zero size up to 256
            uint8_t weird1[256];
        } overlay;
    } test_in = {};

CodePudding user response:

On the contrary as described in the comments section above, a declaration like

int array[];

is not a Variable Length Array, it's either called Arrays of unknown size (cppreference) or Arrays of Length Zero (gcc).

An example of a VLA would be:

void foo(size_t n)
{
    int array[n]; //n is not available at compile time
}

Based on the comment below (from the cppreference - see provided link):

Within a struct definition, an array of unknown size may appear as the last member (as long as there is at least one other named member), in which case it is a special case known as flexible array member. See struct (section Explanation) for details:

struct s { int n; double d[]; }; // s.d is a flexible array member 
struct s *s1 = malloc(sizeof (struct s)   (sizeof (double) * 8)); // as if d was double d[8]

CodePudding user response:

The provided code is just invalid.

You declared a structure with a flexible array member

struct test_struct {
    uint8_t f;
    uint8_t weird[];
};

From the C Standard (6.7.2.1 Structure and union specifiers)

18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.

As it is seen from the quote such a member must be the last element of a structure. So the above structure declaration is correct.

However then in main you declared another unnamed structure

int main(void) {
    struct {
        struct test_struct tst;
        uint8_t weird[256];
    } test_in = {};
    //...

that contains as member an element of the structure with the flexible array element that now is not the last element of the unnamed structure. So such a declaration is invalid.

Secondly, you are using empty braces to initialize an object of the unnamed structure. Opposite to C in C you may not use empty braces to initialize objects.

  • Related