16

Consider the following homogeneous struct:

struct myStruct {
    void* a;
    char* b;
    int* c;
};

It is homogeneous, I believe, because all of the datatypes are pointers.

Given this struct would the following code be valid and portable across C99?

int main()
{
    void* x = NULL;
    char* y = "hello";
    int* z = malloc(sizeof(int) * 10);
    z[2] = 10;

    void** myArray = malloc(sizeof(void*) * 3);
    myArray[0] = x;
    myArray[1] = y;
    myArray[2] = z;

    struct myStruct* s = (struct myStruct*)myArray;

    printf("%p %s %d\n", s->a, s->b, s->c[2]);

    return 0;
}

I understand that structs will often add padding between components in order to keep the size of the struct consistent, however, because the types of the pointers are all the same, is it a safe assumption that no padding will be added? I'm not necessarily asking if there is a 100% guarantee (I understand this is completely implementation specific, and that a compiler could add padding for obscure reasons), more I am asking for what reasons padding might be added to a homogeneous struct, if any reasons at all.

0

4 Answers 4

19

No it's not portable. The size of pointers may actually differ which means there may be padding in your structure.

On most modern platforms this won't be a problem, but there's nothing in the standard that says all pointers have to be equal in size. Only that pointers are implicitly convertible from and to void *.

A good example of a platform where pointer sizes differs is DOS (which is still actively used, for example on embedded systems) and other 16-bit segmented systems.

5
  • Okay that answers it. will accept when time limit is up. If they are all void* would it be portable? Commented Mar 21, 2017 at 7:10
  • 3
    @BradenSteffaniak That is a little harder to answer. I want to say probably but I wouldn't want to bet on it. Commented Mar 21, 2017 at 7:12
  • 2
    @Some programmer dude: Actually if memory serves this was perfectly safe in DOS, at least the Turbo C compiler I used, as well as on the embedded compilers I have used with disparate (non-function) pointer sizes. Typically the different memory spaces are tagged with explicit qualifiers (near and far in the DOS case) whereas untagged ones reach the same memory space. This does break all sorts of assumptions though, for instance I am presently writing code on a system where undecorated pointers/size_t/intptr_t are 8-bits while most everything is reached through extended 16-bit pointers.
    – doynax
    Commented Mar 21, 2017 at 9:14
  • 2
    Also, is not the compiler perfectly free to insert pad bytes inside a struct if it feels that layout will be more efficient to address? Probably not likely with pointers, but for any other data type it could be an issue.
    – T.E.D.
    Commented Mar 21, 2017 at 14:50
  • @BradenSteffaniak: It would be portable (in the sense that current compilers would do the right thing) but not conforming, since padding might theoretically exist between structure members.
    – alecov
    Commented Mar 21, 2017 at 16:49
12

The code violates aliasing rules. The types void* and struct myStruct are not compatible, and no exceptions apply in this case. The dereference of the struct pointer in the printf statement causes undefined behavior:

printf("%p %s %d\n", s->a, s->b, s->c[2]);

This is true even if the structure has the same size as three void pointers, has no padding, and has the same alignment requirements.

3
  • Fair point. Do you have any examples of counter-intuitive/undefined behavior from dereferencing a homogeneous struct pointer that was cast from an array? Commented Mar 21, 2017 at 9:03
  • @BradenSteffaniak Usually the compiler will reuse old values, instead of reloading new ones. But don't rely on this. Such behavior is a bug, which can lead to bad things.
    – 2501
    Commented Mar 21, 2017 at 9:09
  • @BradenSteffaniak: Both gcc and clang seem to recognize the form of aliasing used here, even in tests where most other forms of aliasing would break, but the authors of gcc have stated quite emphatically that such precedents mean nothing. Unless gcc's documentation says somewhere that it supports such aliasing even though the Standard doesn't require it, I see no reason to expect that future versions won't withdraw such support.
    – supercat
    Commented Mar 21, 2017 at 16:18
1

Nothing prevents the compiler to use different alignment for structures and arrays. For example on SPARC with GCC it's possible to align structures to either 4 or 8 byte boundary using -mfaster-structs / -mnofaster-structs switches, while arrays of pointers are aligned to the size of their element (again, 4 or 8 bytes, depending on the kind of pointers you use). In case of a mismatch, your cast

struct myStruct* s = (struct myStruct*)myArray;

will produce an invalid pointer and result in UB.

0

(...) because the types of the pointers are all the same, is it a safe assumption that no padding will be added?

No, not at all. Padding (packing) is completely unrelated issue to a struct being homogeneous or not. It works simply "as long as a member is shorter than current packing, it's padded to compensate". Eg, homogeneous struct of singular chars will almost always be heavily padded.

You can do #pragma pack(16) and your struct members will be spaced 16 bytes apart. Or you can set the packing to 1 byte and then mix void*s with single chars and still have no padding (but pointer alignment headache instead).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.