简体   繁体   English

多线程代码中的结构内存布局

[英]structure memory layout in multithreaded code

The following code is a multi-threaded and is running for thread id=0 and 1 simultaneously . 以下代码是多线程的,并且同时针对线程id = 0和1运行。

typedef struct
{
  unsigned char pixels[4];
} FourPixels;

main()

{ 

  FourPixels spixels[];


  //copy on spixels
  spixels[id] = gpixels[id];

  //example : remove blue component
  spixels[id].pixels[0] &= 0xFC;
  spixels[id].pixels[1] &= 0xFC;
  spixels[id].pixels[2] &= 0xFC;
  spixels[id].pixels[3] &= 0xFC;

}

We see that thread id =0 fetches 4 chars, and the thread id =1 fetches another set of 4 chars. 我们看到线程id = 0提取4个字符,线程id = 1提取另一组4个字符。

I want to know in memory how the structures spixels[0] and spixles[1] are put, means something like this? 我想知道如何在内存中放置spixels [0]和spixles [1],这意味着什么?

spixels[0]                                      spixels[1]                  
pixel[0] pixel[1] pixel[2] pixel[3]      pixel[0] pixel[1] pixel[2] pixel[3]  
2000        2001   2002     2003          2004     2005     2006      2007

The question is are spixel[0] and spixel[1] placed contiguously with guarantee as shown above? 问题是spixel [0]和spixel [1]是否以如上所述的方式连续放置?

Yes, they will be laid out contiguously as you say. 是的,它们将按照您所说的连续布置。 Now, probably someone will come and say that it is not guaranteed on all platforms, because the alignment of the struct could be more than its size, so you could have a gap between the two struct "bodies" due to implicit padding after the first one. 现在,可能有人会说它并不能在所有平台上得到保证,因为该结构的对齐方式可能会超出其大小,因此,由于第一个结构后的隐式填充,您可能会在两个结构“实体”之间留有空隙一。 But no matter, because the alignment on any sane compiler and platform will be just 1 byte (as in char). 但是没关系,因为在任何合理的编译器和平台上的对齐方式都仅为1个字节(如char中一样)。

If I were writing code that relied on this, I'd add a compile-time assertion that the size of two of those structs should be exactly 8 bytes, and then I'd be 100% confident. 如果我正在编写依赖于此的代码,则会添加一个编译时断言,其中两个结构的大小应恰好为8个字节,然后我将100%确信。

Edit: here's an example of how a compile-time check might work: 编辑:这是一个编译时检查如何工作的示例:

struct check {
  char floor[sizeof(FourPixels[2]) - 8];
  char ceiling[8 - sizeof(FourPixels[2])];
};

The idea is that if the size is not 8, one of the arrays will have negative size. 这个想法是,如果大小不是8,则其中一个数组的大小将为负。 If it is 8, they'll both have zero size. 如果为8,则它们的大小均为零。 Note that this is a compiler extension (GCC supports zero-length arrays for example), so you may want to look for a better way. 请注意,这是一个编译器扩展(例如,GCC支持零长度数组),因此您可能需要寻找更好的方法。 I'm more of a C++ person, and we have fancier tricks for this (in C++11 it's built in: static_assert() ). 我更多是C ++的人,对此我们有很多技巧(在C ++ 11中,它内置于: static_assert() )。

An array is guaranteed by the standard to be contiguous. 标准保证数组是连续的。 It's also guaranteed that the first entry will be on a low address in memory, and the next will be on a higher, etc. 还保证了第一个条目将在内存中的低地址上,而下一个条目将在较高的地址上,依此类推。

In the case the structures pixel array, pixel[1] will always come directly after pixel[0] . 的情况下的结构pixel阵列, pixel[1]将总是直接后来pixel[0] The same with the next entries. 与下一项相同。

Yes arrays are placed in contiguous memory location. 是的,数组放置在连续的内存位置。 This is to allow the pointer arithmetic. 这是为了允许指针算术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM