理由检查 [英] justification check
问题描述
考虑我想编写我自己的标准malloc版本,calloc,
realloc,免费。如何便携地检查它们是否正常工作?最好的
非常值得测试每种C类型的正确理由
动态分配。
-
vir
Consider I want to write my own version of standard malloc, calloc,
realloc, free. How can I portably check if they work correctly? The most
remarkable will be the test for right justification of each C type
dynamicly allocated.
--
vir
推荐答案
Victor Nazarov写道:
Victor Nazarov wrote:
考虑我想写自己的标准malloc的版本,calloc,
realloc,免费。如何便携地检查它们是否正常工作?最显着的将是对每种C型动态分配的正确理由的测试。
Consider I want to write my own version of standard malloc, calloc,
realloc, free. How can I portably check if they work correctly? The most
remarkable will be the test for right justification of each C type
dynamicly allocated.
你不能检查对齐(哪个我假设你的意思是理由)
便携式。这是在标准C中编写* alloc系列
的障碍之一。您可以使用标准C结构在特定平台上测试
对齐,也许通过将已恢复的
指针转换为整数类型并测试可分性(使用模数
运算符或位掩码)。
#include< stdio.h>
int aligned(void * p,int boundary){
return!((long)p%boundary);
}
int main(){
double d;
char c;
int x;
void * pointers [] = {& d,& c,& x,0},** p =指针;
int alignments [] = {32,16,8,4,2,0},* i;
for(; * p; p ++)
for (i = alignments; * i; i ++)
printf("%p is%saligned on%d-byte boundary\\\
,
* p, (对齐(* p,* i)?"":不是,)* i);
返回0;
}
当然,你只有ne ed检查你的
malloc实现返回的指针是否正确对齐/ any / type,即它b / b
必须满足目标上最严格的对齐要求
平台。
Jeremy。
You can''t check alignment (which I assume you mean by "justification")
portably. This is one of the impediments to writing the *alloc family
in standard C. You may be able to use standard C constructs to test
alignment on a particular platform, perhaps by converting the retured
pointer to an integer type and testing divisibility (with the modulus
operator or a bitmask).
#include <stdio.h>
int aligned(void *p, int boundary) {
return !((long)p % boundary);
}
int main() {
double d;
char c;
int x;
void *pointers[] = {&d, &c, &x, 0}, **p = pointers;
int alignments[] = {32, 16, 8, 4, 2, 0}, *i;
for (; *p; p++)
for (i = alignments; *i; i++)
printf("%p is %saligned on a %d-byte boundary\n",
*p, (aligned(*p, *i) ? "" : "not "), *i);
return 0;
}
Of course, you only need to check that the pointer returned by your
implementation of malloc is correctly aligned for /any/ type, i.e. it
must satisfy most stringent alignment requirements on the target
platform.
Jeremy.
Victor Nazarov写道:
Victor Nazarov wrote:
考虑我想写自己的标准malloc版本,calloc,
realloc,免费。如何便携地检查它们是否正常工作?最显着的将是对每种C型动态分配的正确理由的测试。
Consider I want to write my own version of standard malloc, calloc,
realloc, free. How can I portably check if they work correctly? The most
remarkable will be the test for right justification of each C type
dynamicly allocated.
你不能检查对齐(哪个我假设你的意思是理由)
便携式。这是在标准C中编写* alloc系列
的障碍之一。您可以使用标准C结构在特定平台上测试
对齐,也许通过将已恢复的
指针转换为整数类型并测试可分性(使用模数
运算符或位掩码)。
#include< stdio.h>
int aligned(void * p,int boundary){
return!((long)p%boundary);
}
int main(){
double d;
char c;
int x;
void * pointers [] = {& d,& c,& x,0},** p =指针;
int alignments [] = {32,16,8,4,2,0},* i;
for(; * p; p ++)
for (i = alignments; * i; i ++)
printf("%p is%saligned on%d-byte boundary\\\
,
* p, (对齐(* p,* i)?"":不是,)* i);
返回0;
}
当然,你只有ne ed检查你的
malloc实现返回的指针是否正确对齐/ any / type,即它b / b
必须满足目标上最严格的对齐要求
平台。
Jeremy。
You can''t check alignment (which I assume you mean by "justification")
portably. This is one of the impediments to writing the *alloc family
in standard C. You may be able to use standard C constructs to test
alignment on a particular platform, perhaps by converting the retured
pointer to an integer type and testing divisibility (with the modulus
operator or a bitmask).
#include <stdio.h>
int aligned(void *p, int boundary) {
return !((long)p % boundary);
}
int main() {
double d;
char c;
int x;
void *pointers[] = {&d, &c, &x, 0}, **p = pointers;
int alignments[] = {32, 16, 8, 4, 2, 0}, *i;
for (; *p; p++)
for (i = alignments; *i; i++)
printf("%p is %saligned on a %d-byte boundary\n",
*p, (aligned(*p, *i) ? "" : "not "), *i);
return 0;
}
Of course, you only need to check that the pointer returned by your
implementation of malloc is correctly aligned for /any/ type, i.e. it
must satisfy most stringent alignment requirements on the target
platform.
Jeremy.
" Victor Nazarov" < VV ***** @ mail.ru>在消息中写道
"Victor Nazarov" <vv*****@mail.ru> wrote in message
考虑我想编写我自己的标准malloc版本,
calloc,realloc,free。如何正确检查它们是否正常工作?最值得注意的是对每个动态分配的C类型的正确理由进行测试。
Consider I want to write my own version of standard malloc,
calloc, realloc, free. How can I portably check if they work
correctly? The most remarkable will be the test for right justification
of each C type dynamically allocated.
使用块结构来完成所有控件。在块中,添加一个
的所有基本类型的联合,以强制对齐。
union alltypes
{
void * x1;
长x2;
...(别忘了功能指针)
};
struct control
{
struct control * next;
struct control * prev;
size_t len;
};
union block
{
union alltypes aligner;
struct control controldata;
};
Use a Block structure to do all your controls. In the block, add a union of
all the basic types, to force alignment.
union alltypes
{
void *x1;
long x2;
... (don''t forget function pointers)
};
struct control
{
struct control *next;
struct control *prev;
size_t len;
};
union block
{
union alltypes aligner;
struct control controldata;
};
这篇关于理由检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!