Keith Thompson said:
CBFalconer said:
Keith said:
[...]
I cannot imagine why one needs a strictest-aligned variable of
static duration, but the following would work
typedef union { [...]
} strict_align_t;
strict_align_t myvar;
Unsigned and complex stuff can be inferred to fit this alignment.
It's very likely to work, but it's certainly not guaranteed to work.
There are arbitrarily many types, and any of them could theoretically
have stricter alignment requirements than any of the types you've
listed.
[...]
Do you think the following code you work at least 80% of the time:
---------------
#include <stdio.h>
#include <stddef.h>
typedef struct aligned_s aligned_t;
typedef union aligned_offset_u aligned_offset_t;
typedef union aligned_ptrs_u aligned_ptrs_t;
typedef union aligned_uptrs_u aligned_uptrs_t;
typedef union aligned_ints_u aligned_ints_t;
typedef union aligned_uints_u aligned_uints_t;
typedef enum aligned_enum_u aligned_enum_t;
#if ! defined(ALIGN_TYPEUNION_APPEND)
typedef union aligned_append_u aligned_append_t;
union aligned_append_u {
char whatever[1024];
};
#define ALIGN_TYPEUNION_APPEND() aligned_append_t
#endif
enum aligned_enum_u {
aligned_enum_u_v1,
aligned_enum_u_v2,
aligned_enum_u_v3
};
union aligned_ptrs_u {
char* t_char;
short* t_short;
int* t_int;
long* t_long;
float* t_float;
double* t_double;
long double* t_long_double;
long long* t_long_long;
ptrdiff_t* t_ptrdiff;
void* t_void;
float* (*t_fptr_0) (long double);
long long* (*t_fptr_1) (void*);
void* (*t_fptr_2) (void*, ptrdiff_t*);
aligned_ints_t* t_ints;
aligned_ptrs_t* t_aptrs;
aligned_offset_t* t_offset;
aligned_enum_t *t_enum;
};
union aligned_uptrs_u {
unsigned char* t_uchar;
unsigned short* t_ushort;
unsigned int* t_uint;
unsigned long* t_ulong;
size_t* t_size;
float* t_ufloat;
double* t_udouble;
unsigned long long*t_ulong_long;
unsigned int* t_uint_ptr;
unsigned long* (*t_ufptr_1) (unsigned int);
unsigned char* (*t_ufptr_2) (double, long double);
aligned_uints_t* t_uints;
aligned_uptrs_t* t_uaptrs;
};
union aligned_ints_u {
char t_char;
short t_short;
int t_int;
long t_long;
float t_float;
double t_double;
long double t_long_double;
long long t_long_long;
ptrdiff_t t_ptrdiff;
aligned_enum_t t_enum;
};
union aligned_uints_u {
unsigned char t_uchar;
unsigned short t_ushort;
unsigned int t_uint;
unsigned long t_ulong;
unsigned long long t_ulong_long;
size_t t_size;
};
union aligned_offset_u {
aligned_ints_t t_ints;
aligned_uints_t t_uints;
aligned_ptrs_t t_ptrs;
aligned_uptrs_t t_uptrs;
ALIGN_TYPEUNION_APPEND() t_append;
};
struct aligned_s {
char base;
aligned_offset_t offset;
};
#define ALIGN_MAX_ESTIMATE() ( \
sizeof(aligned_t) > sizeof(aligned_offset_t) \
? sizeof(aligned_t) - sizeof(aligned_offset_t) \
: sizeof(aligned_offset_t) \
)
int main(void) {
{
printf("estimated strict aligment: %u\n",
ALIGN_MAX_ESTIMATE());
}
puts("\n\n\n\
_______________________\npress <enter> to exit...\n");
return getchar();
}