Re: standard memory allocator alignment issue...
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Tue, 13 May 2008 11:32:26 +1200
Chris Thomasson wrote:
"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxxx> wrote in messageHow would this code with the situation where sizeof(long long) == 8 and
news:0f-dnYX0zdkuqbXVnZ2dnUVZ_qTinZ2d@xxxxxxxxxxxxxx
Chris Thomasson wrote:[...]
How many C compilers provide extensions which allow for a standard
implementation of the following hack?
____________________________________________________________________
[...]____________________________________________________________________
BTW, the ALIGN_MAX macro is needed because using a
sizeof(aligner_types) alone is not sufficient... How many people are
running platforms where (ALIGN_MAX == 8) is true?
Observation #1: It is impossible that the "else" branch
of ALIGN_MAX' expansion will be evaluated, so you might as
well just put `42' there.
Observation #2: ALIGN_MAX computes the number of bytes
in the struct, minus one for the `char' element, minus the
number of padding bytes before the union, minus the number
of padding bytes *after* the union. I've never seen a
compiler where that final term would be non-zero, but ...
Observation #3: On some platforms the program will say
"ALIGN_MAX == 0", because that's one of the likely outcomes
of the undefined behavior in the printf() call.
Totally agree with everything you said. As for printf, at least I should
have it formatted for an unsigned integer: %u. ;^(
Observation #4: I'm not entirely sure, but I think the
"extension" you seek is the offsetof macro in <stddef.h>.
You got it:
___________________________________________________________________
#include <stdio.h>
#include <stddef.h>
typedef union aligner_types_u aligner_types;
typedef struct aligner_offset_s aligner_offset;
union aligner_types_u {
char char_; short s_l; int i_; long l_;
double d_; long double ld_; float f_;
union aligner_types* uap_;
void *p_; char (*fp0_) (char);
long double (*fp1_) (char, long double);
/* long long ll_; */
/* [...] */
};
struct aligner_offset_s {
char offset;
aligner_types types;
};
#define ALIGN_MAX offsetof(aligner_offset, types)
int main() {
printf("ALIGN_MAX == %u\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}
___________________________________________________________________
I am trying to come up with somewhat "portable" hack that can attempt to
determine maximum alignment for integral types across a number of
different compilers.
sizeof(long double) = 12? In that case, 16 would probably be a sensible
value for ALIGN_MAX. Not easy to calculate as a compile time constant.
--
Ian Collins.
.
- Follow-Ups:
- Re: standard memory allocator alignment issue...
- From: Chris Thomasson
- Re: standard memory allocator alignment issue...
- References:
- standard memory allocator alignment issue...
- From: Chris Thomasson
- Re: standard memory allocator alignment issue...
- From: Eric Sosman
- Re: standard memory allocator alignment issue...
- From: Chris Thomasson
- standard memory allocator alignment issue...
- Prev by Date: Re: standard memory allocator alignment issue...
- Next by Date: Re: standard memory allocator alignment issue...
- Previous by thread: Re: [OT] Re: standard memory allocator alignment issue...
- Next by thread: Re: standard memory allocator alignment issue...
- Index(es):
Relevant Pages
|