标准内存分配器对齐问题... [英] standard memory allocator alignment issue...
问题描述
有多少C编译器提供扩展,允许执行以下hack的标准
?
__________________________________________________ __________________
#include< stdio.h>
typedef union aligner_types_u aligner_types;
typedef struct aligner_offset_s aligner_offset;
union aligner_types_u {
char char_;
短s_l;
int i_;
long l_;
double d_;
long double ld_;
float f_;
void * p_;
char(* fp0_)(char);
long double(* fp1_)(char,long double);
union aligner_types * uap_;
/ * long long ll_; * /
/ * [...] * /
};
struct aligner_offset_s {
char offset ;
aligner_types类型;
};
#define ALIGN_MAX(\
sizeof(aligner_offset)sizeof( aligner_types)\
?sizeof(aligner_offset) - sizeof(aligner_types)\
:sizeof(aligner_types)\
)
int main(){
printf(" ALIGN_MAX ==%d\\\
n,nnit enter to exit ... \ n",ALIGN_MAX);
getchar();
返回0;
}
__________________________________________________ __________________
谢谢...
BTW,ALIGN_MAX宏是必需的,因为仅使用sizeof(aligner_types)
是不够的..有多少人在运行平台
(ALIGN_MAX == 8)是真的?
Chris Thomasson < cr ***** @ comcast.netwrote in message
news:5u ************************* *****@comcast.com。 ..
有多少C编译器提供扩展,允许执行以下hack的标准
实现?
[...]
标准在各种版本的便携式意义上
给出供应商C编译器......
:^ o
Chris Thomasson写道:
有多少C编译器提供扩展,允许执行以下hack的标准
实现?
__________________________________________________ __________________
#include< stdio.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_;
void * p_ ;
char(* fp0_)(char);
long double(* fp1_)(char,long double);
union aligner_types * uap_;
/ * long long ll_; * /
/ * [...] * /
};
struct aligner_offset_s {
char offset;
aligner_types类型;
};
#define ALIGN_MAX(\
sizeof(aligner_offset)sizeof(aligner_types)\
?sizeof(aligner_offset) - sizeof(aligner_types)\
:sizeof(aligner_types)\
)
int main(){
printf(" ALIGN_MAX ==%d\\\
\\\
hit enter to exit。 .. \ n",ALIGN_MAX);
getchar();
返回0;
}
__________________________________________________ __________________
谢谢...
BTW,因为使用了sizeof(aligner_types)所以需要ALIGN_MAX宏/>
单独是不够的...有多少人在运行平台
(ALIGN_MAX == 8)是真的?
观察#1:否则是不可能的。分支
的ALIGN_MAX''扩展将被评估,所以你可能只需要把'42''放在那里。
观察#2:ALIGN_MAX计算结构中的字节数
,减去一个'char''元素,减去
填充前的填充字节数,减去*之后的数字
填充字节*。我从来没有见过一个
编译器,其最终用语不为零,但是......
观察#3:在某些平台上程序会说
" ALIGN_MAX == 0",因为这是printf()调用中未定义行为的可能结果之一
。 />
观察#4:我不完全确定,但我认为
"扩展名"你寻求的是< stddef.h>中的偏移量。
-
Eric Sosman
es ***** @ ieee-dot-org.inva 盖子
" ; Eric Sosman < es ***** @ ieee-dot-org.invalidwrote in message
news:0f ********************* *********@comcast.com。 ..
Chris Thomasson写道:
>有多少C编译器提供允许的扩展标准
实施以下黑客攻击?
_________________________________________________ ___________________
[...]
> _________________________________________________ ___________________
[...]
< blockquote class =post_quotes>
> BTW,ALIGN_MAX宏是必需的,因为单独使用sizeof(aligner_types)
是不够的......有多少人运行平台
(ALIGN_MAX == 8)是真的吗?
观察#1:否则是不可能的。分支
的ALIGN_MAX''扩展将被评估,所以你可能只需要把'42''放在那里。
观察#2:ALIGN_MAX计算结构中的字节数
,减去`char'的一个在联合之前减去
填充字节数,减去*之后的填充字节数*
。我从来没有见过一个
编译器,其最终用语非零,但是......
观察#3:在某些平台上,程序会说
ALIGN_MAX == 0,因为这是可能的结果之一
的未定义行为printf()调用。
完全同意你所说的一切。至于printf,至少我应该将它boned格式化为无符号整数:%u。 ; ^(
观察#4:我不完全确定,但我认为你寻求的
扩展名是< stddef.h>中的offsetof宏。
你得到了它:
__________________________________________________ _________________
# 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双倍);
/ * long long ll _; * /
/ * [...] * /
};
struct aligner_offset_s {
char offset;
aligner_types类型;
};
#define ALIGN_MAX offsetof(aligne r_offset,types)
int main(){
printf(" ALIGN_MAX ==%u\\\
n \\ nnit enter enter to exit ... \ n" ,ALIGN_MAX);
getchar();
返回0;
}
__________________________________________________ _________________
我试图想出一些便携式的东西。 hack可以尝试
确定多个不同的b $ b编译器中整数类型的最大对齐。
How many C compilers provide extensions which allow for a standard
implementation of the following hack?
__________________________________________________ __________________
#include <stdio.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_;
void *p_;
char (*fp0_) (char);
long double (*fp1_) (char, long double);
union aligner_types* uap_;
/* long long ll_; */
/* [...] */
};
struct aligner_offset_s {
char offset;
aligner_types types;
};
#define ALIGN_MAX ( \
sizeof(aligner_offset) sizeof(aligner_types) \
? sizeof(aligner_offset) - sizeof(aligner_types) \
: sizeof(aligner_types) \
)
int main() {
printf("ALIGN_MAX == %d\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}
__________________________________________________ __________________
Thanks...
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?
"Chris Thomasson" <cr*****@comcast.netwrote in message
news:5u******************************@comcast.com. ..How many C compilers provide extensions which allow for a standard
implementation of the following hack?[...]
Standard in the sense of being portable within the various versions of a
given vendors C compiler...
:^o
Chris Thomasson wrote:How many C compilers provide extensions which allow for a standard
implementation of the following hack?
__________________________________________________ __________________
#include <stdio.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_;
void *p_;
char (*fp0_) (char);
long double (*fp1_) (char, long double);
union aligner_types* uap_;
/* long long ll_; */
/* [...] */
};
struct aligner_offset_s {
char offset;
aligner_types types;
};
#define ALIGN_MAX ( \
sizeof(aligner_offset) sizeof(aligner_types) \
? sizeof(aligner_offset) - sizeof(aligner_types) \
: sizeof(aligner_types) \
)
int main() {
printf("ALIGN_MAX == %d\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}
__________________________________________________ __________________
Thanks...
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.
Observation #4: I''m not entirely sure, but I think the
"extension" you seek is the offsetof macro in <stddef.h>.
--
Eric Sosman
es*****@ieee-dot-org.invalid
"Eric Sosman" <es*****@ieee-dot-org.invalidwrote in message
news:0f******************************@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.
这篇关于标准内存分配器对齐问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!