可以将多维性STD ::数组视为连续数据块吗?
多维std ::数组
是否可以被视为连续的数据块?也就是说,这是合法的:
#include <array>
#include <iostream>
#include <cstring>
// just to show what's happening, otherwise unrelated
template<typename T>
void print_array(const T &data)
{
for(const auto &row : data) {
for(const auto &item: row) {
std::cout << static_cast<char>('a' + item);
}
std::cout << '\n';
}
std::cout.flush();
}
// function to fill a block of bytes
void filler(uint8_t *data, size_t size) {
while(size != 0) {
*(data++) = size % 27; // just a changing number
--size;
}
}
// test code
int main()
{
std::array<std::array<uint8_t, 30>, 20> data;
std::memset(&data, 0, sizeof(data)); // use &data, is this legal?
print_array(data); // seems to print all 'a' ok...
std::cout << std::endl;
filler(data.begin()->begin(), 30*20); // use pointer to first item, legal?
print_array(data); // seems to print alphabet pattern ok...
}
打印输出是人们期望的,首先是字母矩形a
,然后是一个装有字符a
的矩形 - - z
。
至少讨论了同一主题在这里https://stackoverflow.com/questions/11205186/treat-c-cstyle-array-aray-as-stdarray"> here ,但我认为多维阵列可能是另一种情况。
此外,即使static_assert(sizeof(data)== sizeof(uint8_t)*20*30)
保留,因此数据在没有空白的情况下连续不断,甚至可以保证任何东西吗?还是使用相同的指针通过非法索引访问不同的数组?
Can multi-dimensional std::array
be treated as a continuous block of data? That is, is this legal:
#include <array>
#include <iostream>
#include <cstring>
// just to show what's happening, otherwise unrelated
template<typename T>
void print_array(const T &data)
{
for(const auto &row : data) {
for(const auto &item: row) {
std::cout << static_cast<char>('a' + item);
}
std::cout << '\n';
}
std::cout.flush();
}
// function to fill a block of bytes
void filler(uint8_t *data, size_t size) {
while(size != 0) {
*(data++) = size % 27; // just a changing number
--size;
}
}
// test code
int main()
{
std::array<std::array<uint8_t, 30>, 20> data;
std::memset(&data, 0, sizeof(data)); // use &data, is this legal?
print_array(data); // seems to print all 'a' ok...
std::cout << std::endl;
filler(data.begin()->begin(), 30*20); // use pointer to first item, legal?
print_array(data); // seems to print alphabet pattern ok...
}
Print-out is what one might expect, first a character rectangle of letter a
, then a rectangle filled with characters a
-z
.
The same subject is discussed at least here and here, but I think multi-dimensional arrays may be a different case.
Additionally, even if static_assert(sizeof(data) == sizeof(uint8_t)*20*30)
holds, so the data is continous in memory with no gaps, does even this guarantee anything? Or is using the same pointer to access different arrays through indexing illegal anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要查看 this 问题及其所接受的答案。基本上,实现将具有普通数组
t [n]
作为std :: Array
的第一个成员> sizeof(data)!= 30 * 20 。这似乎有点牵强,但是有报道说: link 。因此,如果您希望代码是“合法的”,则必须独立对待每个内部阵列并迭代它们,而是在每个阵列上遍历它们,可能会在每个阵列的末端跳动一些空间。如果这是您真正需要做的事情,我非常怀疑,您可以做一个static_assert,以查看
std :: array
的大小是否应该是应该的。You may want to take a look into this question and its accepted answer. Basically, implementations will have a plain array
T[N]
as the first member of thestd::array
but nothing stops them putting members after that, in which casesizeof(data) != 30 * 20
.This might seem a bit farfetched, but there were reports of this happening: link. So if you want your code to be "legal", you will have to treat each inner array independently and iterate over them instead, possibly jumping some space at the end of each. If this is something you really need to do, which I highly doubt, you could do a static_assert to see if the size of
std::array
is what it should be.