Afficher un message
Vieux 12/05/2008, 23h41   #4
Chris Thomasson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: standard memory allocator alignment issue...

"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:0f-dnYX0zdkuqbXVnZ2dnUVZ_qTinZ2d@comcast.com...
> 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.

  Réponse avec citation
 
Page generated in 0,07682 seconds with 9 queries