SunStudio C++ 中的对齐编译器

发布于 2024-12-28 18:25:02 字数 388 浏览 1 评论 0 原文

我需要为按 4 字节对齐的 2 字节变量声明类型别名。

在 GCC、XL C/C++ (AIX)、aCC (HP-UX) 中,我可以使用以下代码:

typedef uint16_t AlignedType __attribute__ ((aligned (4)));

在 Windows 中,我可以使用:

typedef __declspec(align(4)) unsigned __int16 AlignedType;

How can I statements in SunStudio C++ 11?

“pragmaalign”不适合,因为它仅适用于全局或静态变量,并且需要变量名称。

I need to declare type alias for 2 bytes variable aligned by 4 bytes.

In GCC, XL C/C++ (AIX), aCC (HP-UX) I can use this code:

typedef uint16_t AlignedType __attribute__ ((aligned (4)));

In Windows I can use:

typedef __declspec(align(4)) unsigned __int16 AlignedType;

How can I declare same type in SunStudio C++ 11?

"pragma align" isn't suitable because it works only for global or static variable and It requires variable name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

病女 2025-01-04 18:25:02

从 Sun C 5.9 (Sun ONE Studio 12) 开始,支持对齐属性:

typedef uint16_t AlignedType __attribute__ ((aligned (4)));

不幸的是,C++ 不支持此属性(至少在 Sun C++ 5.10 中)。

As of Sun C 5.9 (Sun ONE Studio 12), the aligned attribute is supported:

typedef uint16_t AlignedType __attribute__ ((aligned (4)));

Unfortunately this attribute is not supported in C++ (at least through Sun C++ 5.10).

各自安好 2025-01-04 18:25:02

至少值得尝试:

typedef union {
  uint16_t value;
  uint32_t _dummy;
} AlignedType;

这当然会让访问变得更加痛苦,并且会终止直接赋值,因此可能会破坏整个代码库。此外,它纯粹基于这样的假设:包含更大的类型(由于其大小而被假定具有 32 位的“本机对齐”)使得 union 作为一个整体在 32 位上对齐。

It might be worth at least trying:

typedef union {
  uint16_t value;
  uint32_t _dummy;
} AlignedType;

This of course makes accessing a bit more painful, and kills direct assignment so it might break your entire code base. Also, it's purely based on the assumption that including a larger type, which is assumed to have "native alignment" of 32 bits due to being of that size, makes the union as a whole align on 32 bits.

我的奇迹 2025-01-04 18:25:02

为了便于将来参考,当编译器赶上时,C++11 具有标准对齐属性,请参阅 alignasN3242)。

For future references, when the compilers catch up, C++11 has standard alignment attributes, see alignas ([dcl.align] in N3242).

注定孤独终老 2025-01-04 18:25:02

从 Sun C++ 5.12 SunOS_sparc 2011/11/16 开始,根据 DRH 的响应,C++ 似乎支持 gcc 语法:

typedef uint16_t AlignedType8 __attribute__ ((aligned (8)));
typedef uint16_t AlignedType4 __attribute__ ((aligned (4)));
typedef uint16_t AlignedType2 __attribute__ ((aligned (2)));
cout << __alignof__(AlignedType8) << ' ' << __alignof__(AlignedType4) << ' ' << __alignof__(AlignedType2) << endl;

输出为:

8 4 2

As of Sun C++ 5.12 SunOS_sparc 2011/11/16, the gcc syntax appears to be supported for C++ as per DRH's response:

typedef uint16_t AlignedType8 __attribute__ ((aligned (8)));
typedef uint16_t AlignedType4 __attribute__ ((aligned (4)));
typedef uint16_t AlignedType2 __attribute__ ((aligned (2)));
cout << __alignof__(AlignedType8) << ' ' << __alignof__(AlignedType4) << ' ' << __alignof__(AlignedType2) << endl;

The output is:

8 4 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文