关于“对齐”的问题在记忆中 [英] Questions about "alignment" in memory

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

问题描述

我在一段时间后发布了一个关于访问char数组作为

数组的问题。为了不超出char数组,我用足够的0x00字节填充它,以确保当作为单词访问时,
不会超出数组。我被告知这是危险的,并且可能存在对齐问题,例如,如果我想

从非偶数倍的sizeof访问char数组元素(int)。

例如,如果我有阵列:


char a [10];


我想要访问8个字节(a [2],a [3],...,a [8],a [9])作为

数组:


int b [2];


其中(b [0]包含[2]到[5]中的数据,b [1]包含[6]

到[9])


我理解这个例子中的对齐问题。我的问题

是......我可以解决这个问题...例如,创建一个

空的int数组,然后访问这个内存空间char?


这就是我在说什么

unsigned int * a_words;

char * a_bytes;


fstream in(" myfile.dat",ios :: in | ios :: binary | ios :: ate);

int filesize_bytes = in.tellg();

int filesize_words = filesize_bytes / sizeof(int)+((filesize_bytes%

sizeof(int))> 0); //如果有一个remander,加1 ...


a_words = new unsigned int [filesize_words];

a_bytes = reinterpret_cast< char *>( a_words);


in.seekg(2,ios :: beg); //note...out(word)alignment ...开始于

第3个字节

in.read(a_bytes,filesize_bytes-3);

in.close();


此时文件在内存中并且可以作为字节访问(通过

索引a_bytes [0] to filesize_bytes])或作为单词(通过索引

a_words [0到filesize_words]。


这似乎工作正常。此外,它不应该由于数组被定义为与单词对齐,因此可能会遇到字节地址,即使
$ b也可以访问
对齐问题。 $ b反过来说这不是真的。


我可以看到这个系统会出现兼容性问题

如果移植到CHAR_BIT系统!= 8.但是,对于这些系统,我不在乎。如果我只是在文件中的位上做逻辑运算符,我不会'''''''''''''''''''''''''''''''''''甚至可以看到这样做的任何endian问题。


谢谢你面对面我肯定我会用c ++执行这样的b / b
blastphomous操作。说真的,这种处理是否可以避免潜在的对齐问题?

I posted a question some time back about accessing a char array as an
array of words. In order not to overrun the char array, I padded it
with enough 0x00 bytes to ensure that when accessed as words I
wouldn''t overrun the array. I was told that this is dangerous and
that there could be alignment problems if, for example, I wanted to
access the char array elements from non-even multiples of sizeof(int).
For example, if I had the array:

char a[10];

and I wanted to access the 8 bytes (a[2], a[3],..., a[8], a[9]) as the
array:

int b[2];

where (b[0] contains the data in a[2] to a[5], and b[1] contains a[6]
to a[9])

I understand the alignment issue in this example. My question
is...can I turn this problem on its head...for example, create an
empty array of ints, then access this memory space as a char?

Here''s what I''m talking about:
unsigned int* a_words;
char* a_bytes;

fstream in("myfile.dat", ios::in | ios::binary | ios::ate);
int filesize_bytes = in.tellg();
int filesize_words = filesize_bytes / sizeof(int) + ((filesize_bytes %
sizeof(int)) > 0); // add 1 if there is a remander...

a_words = new unsigned int[filesize_words];
a_bytes = reinterpret_cast<char*>(a_words);

in.seekg(2, ios::beg); //note...out of (word) alignment...starts on
3rd byte
in.read(a_bytes, filesize_bytes-3);
in.close();

at which point the file is in memory and can be accessed as bytes (by
indexing a_bytes[0 to filesize_bytes]) or as words (by indexing
a_words[0 to filesize_words].

This seems to work fine. Additionally, it shouldn''t suffer potential
alignment problems since the array is defined to align with words, and
word addresses should be accessable to a byte address, even if the
converse of this is not true.

I can see that there will be compatibility problems with this system
if ported to a system where CHAR_BIT != 8. However, I don''t care
about these systems. If I''m only doing logical operators on the bits
in the file, I don''t even see any endian issues with doing this.

Thanks for the slap-in-the-face I''m sure I''ll get for performing such
blastphomous operations in c++. Seriously, does this treatment
circumvent potential alignment issues?

推荐答案

J。 Campbell写道:
J. Campbell wrote:
我在一段时间后发布了一个关于访问char数组作为
数组的问题。为了不超出char数组,我用足够的0x00字节填充它,以确保当作为单词访问时我不会超出数组。有人告诉我,这很危险,如果我想从非偶数倍的sizeof(int)访问char数组元素,可能存在对齐问题。
例如,如果我有阵列:

char a [10];

我想访问8个字节(a [2],a [3], ...,a [8],a [9])作为
数组:

int b [2];

其中(b [0]包含[2]到[5]中的数据,b [1]包含[6]
到[9])

我理解这个例子中的对齐问题。我的问题
是......我可以把这个问题放在头上......例如,创建一个空的int数组,然后以char形式访问这个内存空间吗?
I posted a question some time back about accessing a char array as an
array of words. In order not to overrun the char array, I padded it
with enough 0x00 bytes to ensure that when accessed as words I
wouldn''t overrun the array. I was told that this is dangerous and
that there could be alignment problems if, for example, I wanted to
access the char array elements from non-even multiples of sizeof(int).
For example, if I had the array:

char a[10];

and I wanted to access the 8 bytes (a[2], a[3],..., a[8], a[9]) as the
array:

int b[2];

where (b[0] contains the data in a[2] to a[5], and b[1] contains a[6]
to a[9])

I understand the alignment issue in this example. My question
is...can I turn this problem on its head...for example, create an
empty array of ints, then access this memory space as a char?




是的,你可以,但只有char是其他的事情。另一个解决方案

是定义一个union,里面有一个char和一个int数组。


-

WW aka Attila



Yes you can, but only with char being the "other" thing. Another solution
is to define a union, with a char and an int array inside.

--
WW aka Attila


WW写道:
WW wrote:
是的,你可以,但只有char是其他的事情。另一个解决方案是定义一个union,里面有一个char和一个int数组。
Yes you can, but only with char being the "other" thing. Another solution
is to define a union, with a char and an int array inside.



这不保证。如果对象的最近一个商店对另一个成员使用了另一个成员,则使用union对象成员的

值是实现定义的行为。结构共享

常见的初始序列。


许多实现都允许它。


有更多的可移植方式,基本上将字节

转移到or int上。


Brian Rodenborn


This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.

Many implementations do allow it.

There are more portable ways, basically shifting and or-ing the bytes
onto an int.


Brian Rodenborn

默认用户写道:
WW写道:
WW wrote:
是的,你可以,但只有char是其他的事情。另一个解决方案是定义一个union,里面有一个char和一个int数组。
Yes you can, but only with char being the "other" thing. Another
solution is to define a union, with a char and an int array inside.



这不保证。如果对象的最近一个商店对另一个成员使用了一个union对象成员的
值,那么它是实现定义的行为,而不是共享一个
共享的结构初始序列。


This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.




是的。但到目前为止我们正在谈论一个char和一个int数组。


-

WW aka Attila



yep. But we are talking about a char and an int array so far.

--
WW aka Attila

这篇关于关于“对齐”的问题在记忆中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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