memory-alignment

Unaligned memory access: is it defined behavior or not? [duplicate]

十年热恋 提交于 2019-12-23 14:23:41
问题 This question already has an answer here : What does the standard say about unaligned memory access? (1 answer) Closed last year . Consider the following code: #include <iostream> int main() { char* c = new char('a'); char ac[4] = {'a', 'b', 'c', 'd'}; unsigned long long int* u = reinterpret_cast<unsigned long long int*>(c); unsigned long long int* uc = reinterpret_cast<unsigned long long int*>(&ac[3]); *u = 42; *uc = 42; std::cout<<*u<<" "<<*uc<<std::endl; } Is this considered as a valid

Unaligned memory access: is it defined behavior or not? [duplicate]

偶尔善良 提交于 2019-12-23 14:21:06
问题 This question already has an answer here : What does the standard say about unaligned memory access? (1 answer) Closed last year . Consider the following code: #include <iostream> int main() { char* c = new char('a'); char ac[4] = {'a', 'b', 'c', 'd'}; unsigned long long int* u = reinterpret_cast<unsigned long long int*>(c); unsigned long long int* uc = reinterpret_cast<unsigned long long int*>(&ac[3]); *u = 42; *uc = 42; std::cout<<*u<<" "<<*uc<<std::endl; } Is this considered as a valid

memory alignment issues with union

社会主义新天地 提交于 2019-12-23 14:20:44
问题 Is there guarantee, that memory for this object will be properly aligned if we create object of this type in stack? union my_union { int value; char bytes[4]; }; If we create char bytes[4] in stack and then try to cast it to integer there might be alignment problem. We can avoid that problem by creating it in heap, however, is there such guarantee for union objects? Logically there should be, but I would like to confirm. Thanks. 回答1: Well, that depends on what you mean. If you mean: Will both

How does padding of structs inside unions work?

不想你离开。 提交于 2019-12-23 13:13:43
问题 I have the following: #include <stdio.h> typedef union u_data { struct { int a; int b; int c; }; int elem[3]; } my_data; int main(void) { my_data data; data.a = 3; data.b = 5; data.c = -3; printf("%d, %d, %d\n", data.elem[0], data.elem[1], data.elem[2]); } and it works as I expected with output: 3, 5, -3 however I understand that structs can have padding in them so does that mean that the elements in the struct might not always align with the array? 回答1: First of all, there is a special rule

Why does the compiler init this variable to the wrong value? Is this an alignment issue?

ぃ、小莉子 提交于 2019-12-23 11:02:17
问题 I'm working with an embedded C compiler (ARM cortex-m3 chip) and it seems to initialize the wrong value to a struct. Why does this happen? If it's an alignment issue, shouldn't the compiler know to align an int32u to a 4-byte boundary? Note: the printf merely throws bytes out of the serial port. There is no stdio.h implementation on this chip. typedef struct { int32u startTime; int16u length; int32u offTime; } Cycle; Cycle cycle = { 315618000, 1200, 0 }; void init() { printf("\r\nInitialized!

Does VC++ support _mm_malloc?

六月ゝ 毕业季﹏ 提交于 2019-12-23 09:06:24
问题 Does Visual Studio C++ 2008/2010 support _mm_malloc officially? It is defined in malloc.h but I can't find its description in the MSDN library. 回答1: Doesn't answer your question directly, but I think you're suppose to use _aligned_malloc. If my understanding is correct, _mm_malloc is for Intel compilers. 回答2: _mm_malloc/_mm_free supported in Visual Studio 2013 with using the <malloc.h> header. 回答3: See Equivalent C code for _mm_ type functions and, more distantly related, How to allocate

Properly declare SP_DEVICE_INTERFACE_DETAIL_DATA for PInvoke

柔情痞子 提交于 2019-12-23 07:29:09
问题 The SP_DEVICE_INTERFACE_DETAIL_DATA structure: typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA { DWORD cbSize; TCHAR DevicePath[ANYSIZE_ARRAY]; } SP_DEVICE_INTERFACE_DETAIL_DATA, *PSP_DEVICE_INTERFACE_DETAIL_DATA; How do I declare it in C# to get Marshal.SizeOf work properly? I don't have a problem with allocating dynamic buffer. I only want to calculate cbSize in a proper, non-hardcoded manner. The definition at PInvoke.net is wrong. The explanation at PInvoke.net is also wrong: SP_DEVICE

How to dynamically allocate 2d array that's 16B aligned

北城以北 提交于 2019-12-23 03:42:26
问题 I'd like to allocate 2D array (square matrix) using memalign with 16B instead of using just malloc . I have A =(float **) malloc( (*dim) * sizeof(float*)); for ( i = 0 ; i < (*dim) ; i++) { A[i] = (float*) malloc(sizeof(float)*(*dim)); } how can I change code above with memalign . 回答1: With malloc() you need to request 15 extra bytes and then round-up the returned pointer to the nearest multiple of 16, e.g.: void* p = malloc(size + 15); void* paligned; if (!p) { /* handle errors */ } paligned

Why do we have alignment padding if memory is byte-addressable?

☆樱花仙子☆ 提交于 2019-12-23 03:19:10
问题 Since we can address every byte of memory individually, why do compilers take extra care to make sure that structs and it's members align to 32-bit borders in memory? I could be wrong here, but on a 32-bit system, is it not just as fast to get 4 bytes starting from say 0x0800, as it is from 0x0801? 回答1: On most architectures it is faster to perform read/write on naturally aligned data types. On some systems it will generate an exception (i.e. crash in most cases) if you try to access certain

Why does compiler align N byte data types on N byte boundaries?

谁都会走 提交于 2019-12-22 09:59:41
问题 I don't understand why the compiler aligns int on 4 byte boundaries, short on 2 byte boundaries and char on 1 byte boundaries. I understand that the if the data bus width of the processor is 4 bytes, it takes 2 memory read cycles for reading an int from an address not a multiple of 4. So, why doesn't the compiler align all data on 4 byte boundaries? For eg.: struct s { char c; short s; }; Here, 1) why does the compiler align short on a 2 byte boundary? Assuming that the processor can fetch 4