运营商:alignof [英] Operator: alignof

查看:106
本文介绍了运营商:alignof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定系统上,一种类型的对齐

要求可能并且允许小于该类型的大小。例如,

这里是一个假设的设置:


int是4个字节,但只需要在2字节边界上对齐。 />

如果我们有某种对齐的话操作员,然后我们就可以做下面的




int main()

{

int array [2];


int * p = array;

/ *以下函数在别处定义。我在这里使用

来使这个片段尽可能简单。 * /


AdvancePointerByBytes(p,alignof(int));


* p = 5;

}

在comp.lang.c上,Hallvard B Furuseth设计了这样一个操作员,并且很多人都使用它。


我已经将它改编为C ++,我编写了一个示例程序,包含

两个头文件和一个源文件。有什么想法吗?

/ * bEGINNING OF advance_ptr.hpp * /


#ifndef INCLUDE_ADVANCE_PTR_HPP

#define INCLUDE_ADVANCE_PTR_HPP


#include< cstddef>


模板< class T>

inline void AdvancePtr(T *& p ,std :: size_t const bytes)

{

p = reinterpret_cast< T *>(

const_cast< unsigned char *>(

reinterpret_cast< const unsigned char *>(p)+ bytes



);

}


模板< class T>

inline void RetreatPtr(T *& p,std :: size_t const bytes)

{

p = reinterpret_cast< T *>(

const_cast< unsigned char *>(

reinterpret_cast< const unsigned char *>(p) - bytes



);

}


#endif


/ * END OF advance_ptr.hpp * /

/ *开始对齐alignof.hpp * /


#ifndef INCLUDE_ALIGNOF_HPP

#define INCLUDE_ALIGNOF_HPP


#include< cstddef>


模板< class T>

struct StructForAlign {


unsigned char first_byte;

T obj;


};


#define alignof (type)(offsetof(StructForAlign< type>,obj))


#endif


/ * END OF alignof.hpp * /

/ *开始使用main.cpp * /

#include< iostream>


#include" advance_ptr.hpp" ;


#include" alignof.hpp"

int main()

{

长双数组[2];


long double * p = array;


AdvancePtr(p,alignof(long double));


* p = 23235.2352;


std :: cout<< * p<< ''\ n'';

}

/ * END OF main.cpp * /

-


Frederick Gotham

It is possible and permissible on a given system, that the alignment
requirements of a type are less than the size of that type. For instance,
here''s a hypothetical set-up:

int is 4 bytes, but need only be aligned on a 2 byte boundary.

If we had some sort of "alignof" operator, then we would be able to do
the following:

int main()
{
int array[2];

int *p = array;
/* The following function is defined elsewhere. I use it
here to make this snippet as simple as possible. */

AdvancePointerByBytes( p, alignof(int) );

*p = 5;
}
Over on comp.lang.c, Hallvard B Furuseth devised such an operator, and a
lot of people use it.

I''ve adapted it to C++, and I''ve written a sample program consisting of
two headers and one source file. Any thoughts on it?
/* BEGINNING OF advance_ptr.hpp */

#ifndef INCLUDE_ADVANCE_PTR_HPP
#define INCLUDE_ADVANCE_PTR_HPP

#include <cstddef>

template<class T>
inline void AdvancePtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) + bytes
)
);
}

template<class T>
inline void RetreatPtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) - bytes
)
);
}

#endif

/* END OF advance_ptr.hpp */
/* BEGINNING OF alignof.hpp */

#ifndef INCLUDE_ALIGNOF_HPP
#define INCLUDE_ALIGNOF_HPP

#include <cstddef>

template<class T>
struct StructForAlign {

unsigned char first_byte;
T obj;

};

#define alignof(type) ( offsetof( StructForAlign< type >, obj ) )

#endif

/* END OF alignof.hpp */
/* BEGINNING OF main.cpp */
#include <iostream>

#include "advance_ptr.hpp"

#include "alignof.hpp"

int main()
{
long double array[2];

long double *p = array;

AdvancePtr( p, alignof(long double) );

*p = 23235.2352;

std::cout << *p << ''\n'';
}
/* END OF main.cpp */
--

Frederick Gotham

推荐答案

Frederick Gotham写道:
Frederick Gotham wrote:
在给定的系统上是可行的和允许的,类型的对齐要求小于该类型的大小。例如,
这里是一个假设的设置:
It is possible and permissible on a given system, that the alignment
requirements of a type are less than the size of that type. For instance,
here''s a hypothetical set-up:




我不明白你想要做什么。假设你

有这样一个运算符它将用于什么?



I don''t understand what you are trying to do. Presuming you
have such an operator what would it be used for?


在文章< kS ********* **********@news.indigo.ie>,

Frederick Gotham< fg ******* @ SPAM.com>写道:
In article <kS*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.com> wrote:
如果我们有某种对齐的话。运算符,然后...
If we had some sort of "alignof" operator, then ...




这是目前在C ++ 0X工作草案中:

http://www.open-std.org/jtc1/ sc22 / wg ... 2006 / n2009.pdf


搜索alignment_of。


在此期间,解决方法如:你的,boost :: alignment_of和

std :: tr1 :: alignment_of可用。


-Howard



This is currently in the C++0X working draft:

http://www.open-std.org/jtc1/sc22/wg...2006/n2009.pdf

Search for alignment_of.

In the meantime, workarounds such as yours, boost::alignment_of, and
std::tr1::alignment_of are available.

-Howard


* Frederick Gotham:
* Frederick Gotham:
在给定系统上,可以允许类型的对齐要求小于该类型的大小。例如,
这是一个假设的设置:
int是4个字节,但只需要在2字节边界上对齐。


是的,当然:对于大多数类型和平台,对齐小于该类型对象的

大小。

C ++作为一种语言不能解决对齐问题。


对齐要求来自硬件(系统总线,内存

接口)和来自操作系统(一些可以修复对齐故障,成本和

在Windows中可以配置)。


如果我们有某种对齐操作员,那么我们就可以做到以下几点:

int main()
{int int [2];
int * p = array;

/ *以下函数在别处定义。我在这里使用它来使这个片段尽可能简单。 * /

AdvancePointerByBytes(p,alignof(int));

* p = 5;
}


你想做什么? ''array''已经适当地对齐了

''int''。此外,通过类型

的对齐值推进指针并不会使指针与该类型良好对齐。相反,您需要

将地址视为整数,并将其提升到对齐值的下一个多个
,如果尚未达到这样的倍数。喜欢


typedef unsigned long ulong; //假设ulong就够了。

char数组[1000];

ulong const a =(ulong)(& a [0])+ alignment(int) - 1; // ...

int * p =(int *)(a - a%alignment(int));


用于2 ^ x对齐通常是通过位操作完成的。


在comp.lang.c上,Hallvard B Furuseth设计了这样一个操作符,很多人都使用它。

我已经将它改编为C ++了,我编写了一个包含两个头文件和一个源文件的示例程序。有什么想法吗?

/ *开始提前预测_ptr.hpp * /
#ifndef INCLUDE_ADVANCE_PTR_HPP
#define INCLUDE_ADVANCE_PTR_HPP

#include < cstddef>

模板<类T>
内联void AdvancePtr(T *& p,std :: size_t const bytes)
{
p = reinterpret_cast< ; T *>(
const_cast< unsigned char *>(
reinterpret_cast< const unsigned char *>(p)+ bytes

);
}
It is possible and permissible on a given system, that the alignment
requirements of a type are less than the size of that type. For instance,
here''s a hypothetical set-up:

int is 4 bytes, but need only be aligned on a 2 byte boundary.
Yes, of course: for most types and platforms alignment is less than the
size of an object of the type.

C++ as a language does not address alignment.

Alignment requirements arise from the hardware (system bus, memory
interface) and from the OS (some can fix alignment faults, at cost, and
in Windows this can be configured).

If we had some sort of "alignof" operator, then we would be able to do
the following:

int main()
{
int array[2];

int *p = array;
/* The following function is defined elsewhere. I use it
here to make this snippet as simple as possible. */

AdvancePointerByBytes( p, alignof(int) );

*p = 5;
}
What are you trying to do? ''array'' is already suitably aligned for
''int''. Also, advancing a pointer by the alignment value for a type
doesn''t make the pointer well-aligned for that type. Instead you need
to treat the address as an integer and advance it to the next multiple
of the alignment value, if not already at such multiple. Like

typedef unsigned long ulong; // Assuming ulong is enough.
char array[1000];
ulong const a = (ulong)(&a[0]) + alignment(int) - 1; // ...
int* p = (int*)(a - a%alignment(int));

which for 2^x alignment is usually done by bit-operations.

Over on comp.lang.c, Hallvard B Furuseth devised such an operator, and a
lot of people use it.

I''ve adapted it to C++, and I''ve written a sample program consisting of
two headers and one source file. Any thoughts on it?
/* BEGINNING OF advance_ptr.hpp */

#ifndef INCLUDE_ADVANCE_PTR_HPP
#define INCLUDE_ADVANCE_PTR_HPP

#include <cstddef>

template<class T>
inline void AdvancePtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) + bytes
)
);
}




这是IMO混合不兼容的抽象级别。对齐是

低级功能的一部分,你并不真正关心指针最终指向的类型为什么的
类型。它是IMO

Ungood和几乎Evil(TM)提供客户代码简单这个低级别的东西与更高级别的代码混合起来的功能。


虽然我没有使用它,但基于一般理由我建议你带一个

看看引用其他线程的Boost实用程序。


-

答:因为它弄乱了订单人们通常会阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是usenet和电子邮件中最烦人的事情是什么?



This is IMO mixing incompatible levels of abstraction. Alignment is
part of low-level functionality where you don''t really care about the
type T that the pointer will finally end up pointing to. It''s IMO
Ungood and almost Evil(TM) to offer client code "easy" functionality to
mix such low-level things with higher level code.

Although I haven''t used it, on general grounds I recommend you take a
look at the Boost utility referenced else-thread.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于运营商:alignof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆