memory-alignment

Safe, efficient way to access unaligned data in a network packet from C

三世轮回 提交于 2019-12-22 08:46:53
问题 I'm writing a program in C for Linux on an ARM9 processor. The program is to access network packets which include a sequence of tagged data like: <fieldID><length><data><fieldID><length><data> ... The fieldID and length fields are both uint16_t. The data can be 1 or more bytes (up to 64k if the full length was used, but it's not). As long as <data> has an even number of bytes, I don't see a problem. But if I have a 1- or 3- or 5-byte <data> section then the next 16-bit fieldID ends up not on

Why is dynamically allocated memory always 16 bytes aligned?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 06:29:09
问题 I wrote a simple example: #include <iostream> int main() { void* byte1 = ::operator new(1); void* byte2 = ::operator new(1); void* byte3 = malloc(1); std::cout << "byte1: " << byte1 << std::endl; std::cout << "byte2: " << byte2 << std::endl; std::cout << "byte3: " << byte3 << std::endl; return 0; } Running the example, I get the following results: byte1: 0x1f53e70 byte2: 0x1f53e90 byte3: 0x1f53eb0 Each time I allocate a single byte of memory, it's always 16 bytes aligned. Why does this happen

Why is dynamically allocated memory always 16 bytes aligned?

和自甴很熟 提交于 2019-12-22 06:28:11
问题 I wrote a simple example: #include <iostream> int main() { void* byte1 = ::operator new(1); void* byte2 = ::operator new(1); void* byte3 = malloc(1); std::cout << "byte1: " << byte1 << std::endl; std::cout << "byte2: " << byte2 << std::endl; std::cout << "byte3: " << byte3 << std::endl; return 0; } Running the example, I get the following results: byte1: 0x1f53e70 byte2: 0x1f53e90 byte3: 0x1f53eb0 Each time I allocate a single byte of memory, it's always 16 bytes aligned. Why does this happen

How to set the right alignment for an OpenCL array of structs?

拥有回忆 提交于 2019-12-21 20:55:26
问题 I have the following structure: C++: struct ss{ cl_float3 pos; cl_float value; cl_bool moved; cl_bool nextMoved; cl_int movePriority; cl_int nextMovePriority; cl_float value2; cl_float value3; cl_int neighbors[6]; cl_float3 offsets[6]; cl_float off1[6]; cl_float off2[6]; }; OpenCL: typedef struct{ float3 nextPos; float value; bool moved; bool nextMoved; int movePriority; int nextMovePriority; float value2; float value3; int neighbors[6]; float3 offsets[6]; float off1[6]; float off2[6]; } ss;

How to align C for-loop body w/ GCC?

柔情痞子 提交于 2019-12-21 07:28:10
问题 In our embedded architecture we have a 64-bit IAB (Instruction Alignment Buffer). In order to optimize the fetch sequence, it is required that the body of a loop will start aligned to an 8-byte boundary. It is easy to achieve this in assembly using the .balign directive, but I cannot find a syntax that will hint the C compiler to align the code. Trying to precede the for loop with inline assembly with the .balign directive doesn't work as it aligns the for loop prolog (setup) and not the loop

Why class size increases when int64_t changes to int32_t

♀尐吖头ヾ 提交于 2019-12-21 03:12:27
问题 In my first example I have two bitfields using int64_t . When I compile and get the size of the class I get 8. class Test { int64_t first : 40; int64_t second : 24; }; int main() { std::cout << sizeof(Test); // 8 } But when I change the second bitfeild to be a int32_t the size of the class doubles to 16: class Test { int64_t first : 40; int32_t second : 24; }; int main() { std::cout << sizeof(Test); // 16 } This happens on both GCC 5.3.0 and MSVC 2015. But why? 回答1: In your first example

Parsing binary message stream in C/C++

隐身守侯 提交于 2019-12-21 01:56:45
问题 I'm writing a decoder for a binary protocol (Javad GRIL protocol). It consists of about a hundred messages, with data in the following format: struct MsgData { uint8_t num; float x, y, z; uint8_t elevation; ... }; The fields are ANSI-encoded binary numbers which follow each other with no gaps. The simplest way to parse such messages is to cast an input array of bytes to the appropriate type. The problem is that the data in stream are packed, i.e. unaligned. On x86 this can be solved by using

The future of C++ alignment: passing by value?

橙三吉。 提交于 2019-12-20 17:32:44
问题 Reading the Eigen library documentation, I noticed that some objects cannot be passed by value. Are there any developments in C++11 or planned developments that will make it safe to pass such objects by value? Also, why is there no problem with returning such objects by value? 回答1: They could do this in C++11: class alignas(16) Matrix4f { // ... }; Now the class will always be aligned on a 16-byte boundary. Also, maybe I'm being silly but this shouldn't be an issue anyway. Given a class like

Why is memory alignment required? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-20 10:46:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Purpose of memory alignment I read some articles on net about memory alignment and could understand that from properly aligned memory (take 2-byte alignment) we can fetch data fastly in one go. But if we have memory like a single hardware piece, then given an address, why cannot we read 2-byte directly from that position. like: I thought over it. I think that if the memory is in odd-even banks kind of then the

Why do some types (e.g. Float80) have a memory alignment bigger than word size?

冷暖自知 提交于 2019-12-20 09:45:39
问题 To make it specific, I only want to know why on my 64 bit mac, the Swift compiler says the alignment of some types like Float80 is 16. To check the memory alignment requirement of a type, I use the alignof function. sizeof(Float80) // ~> 16 bytes, it only needs 10 bytes, but because of hardware design decisions it has to be a power of 2 strideof(Float80) // ~> 16 bytes, clear because it is exact on a power of 2, struct types with Float80 in it, can be bigger alignof(Float80) // ~> 16 bytes,