如何访问数组的多个元素一气呵成? [英] How to access multiple elements of array in one go?

查看:122
本文介绍了如何访问数组的多个元素一气呵成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有uint8_t有数据[256]数组。但是每个元件是单个的字节。
我的数据总线是32位长。所以,如果我想访问32位,我做的:

I have an array uint8_t data[256]. But each element is single byte. My data bus is 32 bit long. So, If I want to access 32 bits, I do:

DATA =数据[I] +(数据第[i + 1];&下; 8)+(数据第[i + 2]所述;&下; 16)+(数据第[i + 3]与下;&下; 24 );

DATA = data[i]+(data[i+1]<<8)+(data[i+2]<<16)+(data[i+3]<<24);

但是,这转化成4个单独的读出要求在每一1字节的存储器

But this translates into 4 separate read requests in the memory of 1 Byte each.

我如何可以访问所有的4个字节1交易的形式?

How can I access all the 4 bytes in the form of 1 transaction?

推荐答案

只投数据 uint32_t的

uint8_t data[256] = {1,2,3,4,5,6,7,8} ;

int main(int argc, char **argv)
{
  int index = 1 ;
  uint32_t d = *(uint32_t *)(data + index) ;
  printf ("%08x\n", d) ;
}

这是一个小端架构输出将被

Output on a little endian architecture will be

05040302

这是一个大端架构输出将被

Output on a big endian architecture will be

02030405

但是取决于你的编程'正在运行的处理器的achitecture,你可能(如果你,如果你的处理器不支持未对齐的内存寻址解决的一个未对齐的内存,甚至崩溃的性能损失)遇到内存对齐问题。

However depending on the achitecture of the processor your progam is running on, you might run into memory alignment problems (performance hit if you address an unaligned memory, or even a crash if your processor doesn't support unaligned memory addressing).

这篇关于如何访问数组的多个元素一气呵成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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