在C ++中的结构对齐 [英] Structure alignment in C++

查看:163
本文介绍了在C ++中的结构对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct Vector 
{
   float x, y, z;
};

func(Vector *vectors) {...}

usage:
load float *coords = load(file);
func(coords);

我有一个关于C ++中结构对齐的问题。我将一组点传递给函数 func()。是否可以按上面所示的方式做,或者这是依赖于平台相关的行为? (它至少与我目前的编译器工作)可以有人推荐一个好的文章的主题吗?

I have a question about the alignment of structures in C++. I will pass a set of points to the function func(). Is is OK to do it in the way shown above, or is this relying on platform-dependent behavior? (it works at least with my current compiler) Can somebody recommend a good article on the topic?

或者,是更好直接创建一组点,数据?

Or, is it better to directly create a set of points while loading the data from the file?

感谢

推荐答案

- 依赖。然而,大多数编译器给你一种方式来指定一个结构应该是压缩(即,在内存中排列,字段之间没有填充字节)。例如:

Structure alignment is implementation-dependent. However, most compilers give you a way of specifying that a structure should be "packed" (that is, arranged in memory with no padding bytes between fields). For example:

struct Vector {
    float x;
    float y;
    float z;
} __attribute__((__packed__));

上述代码将使得gcc编译器将结构打包到内存中,文件并稍后读回。

The above code will cause the gcc compiler to pack the structure in memory, making it easier to dump to a file and read back in later. The exact way to do this may be different for your compiler (details should be in your compiler's manual).

我总是在单独的行上列出压缩结构的成员,以便为了编译器而执行此操作的确切方法可能有所不同。清楚他们应该出现的顺序。对于大多数编译器,这应该等效于 float x,y,z; 但是我不确定这是否是依赖于实现的行为。为了安全起见,我将每行使用一个声明。

I always list members of packed structures on separate lines in order to be clear about the order in which they should appear. For most compilers this should be equivalent to float x, y, z; but I'm not certain if that is implementation-dependent behavior or not. To be safe, I would use one declaration per line.

如果您正在从文件读取数据,则需要在将数据传递给 func

If you are reading the data from a file, you need to validate the data before passing it to func. No amount of data alignment enforcement will make up for a lack of input validation.

编辑

进一步阅读您的代码后,我更了解您正在尝试做什么。您有一个包含三个 float 值的结构,并且您使用 float * 访问它,就好像它是浮点数组。这是非常糟糕的做法。你不知道你的编译器可能在你的结构的开始或结束处使用什么样的填充。即使是一个打包的结构,它是不安全的处理结构像一个数组。如果数组是你想要的,那么使用数组。最安全的方法是从文件中读取数据,将其存储到 struct Vector 类型的新对象中,并将其传递给 func 。如果 func 定义为以一个 struct Vector * 作为参数,并且您的编译器允许您传递 float * 没有griping,那么这确实是依赖于实现的行为,你不应该依赖。

After further reading your code, I understand more what you are trying to do. You have a structure that contains three float values, and you are accessing it with a float* as if it were an array of floats. This is very bad practice. You don't know what kind of padding that your compiler might be using at the beginning or end of your structure. Even with a packed structure, it's not safe to treat the structure like an array. If an array is what you want, then use an array. The safest way is to read the data out of the file, store it into a new object of type struct Vector, and pass that to func. If func is defined to take a struct Vector* as an argument and your compiler is allowing you to pass a float* without griping, then this is indeed implementation-dependent behavior that you should not rely on.

这篇关于在C ++中的结构对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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