Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: standard me...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 32 Topic 26155 of 27670
Post > Topic >>

Re: standard memory allocator alignment issue...

by "Chris Thomasson" <cristom@[EMAIL PROTECTED] > May 12, 2008 at 02:41 PM

"Eric Sosman" <esosman@[EMAIL PROTECTED]
> wrote in message 
news:0f-dnYX0zdkuqbXVnZ2dnUVZ_qTinZ2d@[EMAIL PROTECTED]
> 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 "****table" hack that can attempt to 
determine maximum alignment for integral types across a number of
different 
compilers.
 




 32 Posts in Topic:
standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-11 23:13:16 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-11 23:24:31 
Re: standard memory allocator alignment issue...
Eric Sosman <esosman@[  2008-05-12 08:14:40 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 14:41:20 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 14:51:41 
Re: standard memory allocator alignment issue...
Eric Sosman <Eric.Sosm  2008-05-12 18:22:03 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 15:41:29 
Re: standard memory allocator alignment issue...
Ian Collins <ian-news@  2008-05-13 11:22:53 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 16:36:44 
Re: standard memory allocator alignment issue...
Eric Sosman <Eric.Sosm  2008-05-12 18:08:39 
Re: standard memory allocator alignment issue...
Ian Collins <ian-news@  2008-05-13 11:32:26 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 16:41:37 
Re: standard memory allocator alignment issue...
Ian Collins <ian-news@  2008-05-13 11:56:18 
Re: standard memory allocator alignment issue...
Spiros Bousbouras <spi  2008-05-12 16:02:41 
Re: standard memory allocator alignment issue...
richard@[EMAIL PROTECTED]  2008-05-12 23:13:30 
Re: standard memory allocator alignment issue...
Keith Thompson <kst-u@  2008-05-12 16:20:39 
Re: standard memory allocator alignment issue...
Chris Torek <nospam@[E  2008-05-13 00:46:18 
Re: standard memory allocator alignment issue...
Keith Thompson <kst-u@  2008-05-13 00:46:55 
Re: standard memory allocator alignment issue...
Ben Bacarisse <ben.use  2008-05-13 01:53:49 
Re: standard memory allocator alignment issue...
Eric Sosman <esosman@[  2008-05-12 21:26:56 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-12 19:00:01 
Re: standard memory allocator alignment issue...
Keith Thompson <kst-u@  2008-05-13 09:23:10 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-28 23:57:44 
Re: standard memory allocator alignment issue...
"Chris Thomasson&quo  2008-05-29 00:02:17 
Re: standard memory allocator alignment issue...
Ben Bacarisse <ben.use  2008-05-13 03:07:49 
Re: standard memory allocator alignment issue...
Ben Bacarisse <ben.use  2008-05-13 03:36:49 
Re: standard memory allocator alignment issue...
Spiros Bousbouras <spi  2008-05-13 10:53:22 
Re: standard memory allocator alignment issue...
Eric Sosman <Eric.Sosm  2008-05-13 14:26:59 
Re: standard memory allocator alignment issue...
Keith Thompson <kst-u@  2008-05-13 12:32:07 
Re: standard memory allocator alignment issue...
CBFalconer <cbfalconer  2008-05-13 16:24:00 
[OT] Re: standard memory allocator alignment issue...
Eric Sosman <Eric.Sosm  2008-05-13 17:11:24 
Re: [OT] Re: standard memory allocator alignment issue...
Richard Heathfield <rj  2008-05-13 22:07:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 22:21:17 CDT 2008.