memory-alignment

MinGW and packed struct alignment using C++11

人走茶凉 提交于 2019-12-11 00:39:41
问题 For the below structure, the actual (with no padding) size of the structure is 54. On a 64-bit (Windows 7) machine with MinGW (GCC) 4.8.1 x86_64, I get sizeof(BMPHeader) as 56, which is understandable. As per the requirement of the BMP file format, the structure should've no padding. I've three options (priority ordered): C++11's alignas(1) struct __attribute__ ((packed)) BMPHeader #pragma pack(1) However the last option (with least priority) alone seems to work giving me 54. Is this a bug in

How to set global memory byte alignment for all structures in JNA library?

廉价感情. 提交于 2019-12-10 23:59:30
问题 Is there any way to set global memory byte alignment for all data structures in JNA library (*.dll Java wrapper)? Sometimes I have to determine correct alignment by trial and error during implementation and currently I'm doing this in very clumsy way - I'm setting data alignment (super(ALIGN_NONE)) in each and every structure (a lot of structures in separate files). edit: The best way to solve my problem was to extend Structure: public abstract class StructureAligned extends Structure {

How to tell GCC to set structure size boundary to 4 bytes through compiler settings (not pragma's)?

不羁的心 提交于 2019-12-10 22:52:20
问题 I want my c++ program compiled under GCC to have maximum alignment of 4 bytes (of members of structures). I really can do this through #pragma pack directive. However, it's uncomfortable in my case because the project is quite big, and I would need to make a single header with #pragma pack, that has to be included everywhere. Now, the gcc compiler has an option -mstructure-size-boundary=n documented here http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#ARM-Options , it says "Permissible

Is the address of malloc + size_t * 3 aligned for any type?

此生再无相见时 提交于 2019-12-10 21:36:50
问题 I'm builing a kind of dynamic array (vector), but instead of embedding the data (typically void * ) into a struct vector , I'm reserving space for a struct vector + a chunk of bytes, an example using an array of size_t 's: #include <stdio.h> #include <stdlib.h> struct vector { size_t capacity; size_t typesize; size_t size; }; #define VECTOR(v) ((struct vector *)((unsigned char *)v - sizeof(struct vector))) static void *valloc(size_t typesize, size_t size) { struct vector *vector; unsigned

What is the difference between struct __declspec(align(16)) sse_t and struct alignas(16) sse_t?

瘦欲@ 提交于 2019-12-10 19:17:37
问题 What is the difference between struct __declspec(align(16)) sse_t{}; and struct alignas(16) sse_t{}; ? Link to an example of the alignment of structures in C++11: http://en.cppreference.com/w/cpp/language/alignas // every object of type sse_t will be aligned to 16-byte boundary struct alignas(16) sse_t { float sse_data[4]; }; // the array "cacheline" will be aligned to 128-byte boundary alignas(128) char cacheline[128]; However, there is not in MSVS2012 keyword alignas(16) , but you can use _

void* will have the same representation and memory alignment as a pointer to char

柔情痞子 提交于 2019-12-10 16:30:31
问题 I am reading a book on pointers named " understanding and using c pointers " When it comes to void * it says It has two interesting properties: A pointer to void will have the same representation and memory alignment as a pointer to char. What am confused about is isn't the memory of all the pointers same? They why instead of writing void* is same as normal pointer it explicitly mentioned char pointers? Will really appreciate any help 回答1: On most common architectures, pointer to any data

Hint to compiler that it can use aligned memcpy

主宰稳场 提交于 2019-12-10 16:17:39
问题 I have a struct consisting of seven __m256 values, which is stored 32-byte aligned in memory. typedef struct { __m256 xl,xh; __m256 yl,yh; __m256 zl,zh; __m256i co; } bloxset8_t; I achieve the 32-byte alignment by using the posix_memalign() function for dynamically allocated data, or using the (aligned(32)) attribute for statically allocated data. The alignment is fine, but when I use two pointers to such a struct, and pass them as destination and source for memcpy() then the compiler decides

Does the order of instance variable declaration matter in Objective-C?

↘锁芯ラ 提交于 2019-12-10 08:40:53
问题 I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand. 回答1: This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time. Now, however, ivars are indirectly accessed (at least in Apple's runtime); the instance now holds the

Why does this EXC_BAD_ACCESS happen with long long and not with int?

耗尽温柔 提交于 2019-12-10 08:14:06
问题 I've run into a EXC_BAD_ACCESS with a piece of code that deals with data serialization. The code only fails on device (iPhone) and not on simulator. It also fails only on certain data types. Here is a test code that reproduces the problem: template <typename T> void test_alignment() { // allocate memory and record the original address unsigned char *origin; unsigned char *tmp = (unsigned char*)malloc(sizeof(unsigned short) + sizeof(T)); origin = tmp; // push data with size of 2 bytes *(

why does size of the struct need to be a multiple of the largest alignment of any struct member

回眸只為那壹抹淺笑 提交于 2019-12-09 16:21:45
问题 I understand the padding that takes place between the members of a struct to ensure correct alignment of individual types. However, why does the data structure have to be a multiple of alignment of largest member? I don't understand the padding is needed at the end. Reference: http://en.wikipedia.org/wiki/Data_structure_alignment 回答1: Good question. Consider this hypothetical type: struct A { int n; bool flag; }; So, an object of type A should take five bytes (four for the int plus one for