I'm new in a company where the following use of a struct is done:
#include <stdio.h>
#include <string.h>
typedef unsigned char uint8;
typedef signed char int8;
typedef unsigned short int uint16;
typedef signed short int int16;
typedef struct padded_t {
    int8 array0[11];
    int8 array1[4];
    uint16 len2;
    int8 array2[25];
    uint16 len3;
    int8 array3[6];
    // many more len/arrays follow
} padded_t;
int main(int argc, char** argv)
{
    padded_t foo;
    memset((void*)&foo, 0, sizeof(padded_t));
    int8* str = "foobar";
    int16 size = (int16)strlen(str);
    int8* ptr = (int8*)&foo.len2;
    // please note that the memcpy references only the pointer to len
    // are the following couple of lines safe?
    memcpy ((void*)ptr, &size, 2);
    memcpy ((void*)&ptr[2], (void*)str, size);
    printf("%d\n", foo.len2);
    printf("%s\n", foo.array2);
    return 0;
}
I know some things about alignment and padding, and I assume the compiler (gnu C99 for an ARM9 device) will add some paddings to make the struct aligned.
But is this code safe?
As I understand it, it will be safe as long as the uint16 len variables are immediately followed by int8 array[] variables regardless of the other struct members.
Will it only add paddings before a uint16 when the size before it is odd?
Is this use correct? And more importantly, is it safe?
 
     
     
    