FAQ Database Discussion Community


std::string and data alignment

c++,memory-alignment
I'm planning to use std::string as a generic data buffer (instead of roll my own). I need to pack all kinds of POD into it, including user defined structs, is the memory buffer allocated by std::string properly aligned for such purpose ? If it's unspecified in C++ standard, what's the...

Memory Allocation in Linux Kernel

linux,memory,memory-management,linux-kernel,memory-alignment
I had an interview today and was asked this question. What Kernel Memory allocation strategy would you use, if you were asked to allocate memory of size 2KB and that allocated memory should be page aligned. KMALLOC handles smaller memory allocation strategies but the lowest unit that it supports is...

aligning a struct member in C to 32 bytes,

c,arrays,memory-alignment
I have the following code extract in C, typedef struct Item{ int a; int b; int c; double d; double e; double* f; double* g; } Item; I have an array of this struct item declared as, Item* result[/*no.of threads*/]; When I initialise this struct, I want it to align...

Memory allocation for public and private fields - GCC way

c++,c++11,gcc,g++,memory-alignment
This is not a duplicate of this question, I read the answers and I still have some questions about this subject. I tested some classes like this one: class A { private: int b; public: char c; int a; private: char e; }; And I've seen that the fields are...

Using different structure alignment settings

c,visual-studio,structure,memory-alignment
What are the problems of mixing (Visual Studio) C/C++ projects which have different structure alignment options set? I know that I can obviously set the options differently in different projects, but does this have an affect on the behaviour of the output library/application? Say I have a library, with structure...

Tell C++ that pointer data is 16 byte aligned

c++,gcc,sse,memory-alignment
I wrote some code with static arrays and it vectorizes just fine. float data[1024] __attribute__((aligned(16))); I would like to make the arrays dynamically allocated. I tried doing something like this: float *data = (float*) aligned_alloc(16, size*sizeof(float)); But the compiler (GCC 4.9.2), no longer can vectorize the code. I assume this...

Pointer to aligned memory

c++,c++11,memory-alignment
I often use compiler-based vectorization, e.g., for AVX. I am trying to come up with a cleaner way without relying on compiler-based extensions (such as Intel's #pragma vector aligned) by relying on C++11 alignment features. If you consider the code below, e.g., aligned::array<double,48> my_array; allows me to declare an array...

C11 alignas vs. clang -Wcast-align

c,clang,memory-alignment,c11
So I have the following minimized C11 code that defines a struct containing a uint16_t (which means the struct it should be aligned to 2 bytes) and I want to cast a char buffer to a pointer to that struct. With warnings turned all up, clang rightly complained that the...

AVX - storing __256 vector back to the memory (void**) in C,

c,vector,void-pointers,memory-alignment,avx
I have the following code extract written in C, double* res; posix_memalign((void **)&res, 32, sizeof(double)*4); __m256 ymm0, ymm1, ymm2, ymm3; ymm0 = _mm256_load_pd(vector_a); ymm1 = _mm256_load_pd(vector_b); ymm2 = _mm256_mul_pd(ymm1, ymm2); ymm3 = _mm256_store_pd((double*)res, ymm3); <--- problem line, When I compile, I get the following error message, error: assigning to '__m256'...

Allocating a struct dirent without malloc()

c,memory-management,memory-alignment,readdir,strict-aliasing
I need to use readdir_r() to read the contents of a directory in a multithreaded program. Since the size of struct dirent is filesystem dependent, man readdir_r recommends name_max = pathconf(dirpath, _PC_NAME_MAX); if (name_max == -1) /* Limit not defined, or error */ name_max = 255; /* Take a guess...

Enforce struct size alignment

c,memory-alignment,c11
Is there a way to force the size of a struct to be a multiple of the maximum required alignment for any type? Here are two different attempts: #include <stdio.h> #include <stddef.h> #include <stdalign.h> typedef struct { char c; } Block1; typedef struct { char c; max_align_t data[]; } Block2;...

Under Xcode 6.3, NULL C++ reference address evaluates as non-zero

c++,xcode,pointers,memory-alignment,type-safety
Similar to this question: XCode 6.3 Warning : Comparison of address of 'myObject' not equal to null pointer is always true with C++, I found that previously working code for evaluating null pointers stopped working: struct AStruct { int x, y; char *name; }; AStruct& GetStruct() { return *(AStruct*)0; }...

Intel SSE Intrinsics _mm_load_si128 segmentation fault,

c,sse,simd,memory-alignment,intrinsics
I'm currently working with a 5 x 5 matrix using SSE features. I'm trying to load x4 128bit integer values to the xmm registers as follows, #include <emmintrin.h> #include <smmintrin.h> //===================================== Initialising matrix int* aligned_matrix; posix_memalign((void **)&aligned_matrix, 16, sizeof(int) * 25); for (ssize_t i = 0; i < 25; i++)...

Strange data alignment

c++,64bit,memory-alignment
As far as I know, data alignment is putting data in 64bits / 32bits chunks in memory for CPU performance, I am using a 64 bits linux machine, and I did some tests and got some strange results (I can't explain the behavior). Here are the structures I used :...

Why does some types (Float80) have a memory alignment bigger than word size?

swift,memory-alignment,float80
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...

ARM Cortex-M heap alignment

heap,memory-alignment,cortex-m,eabi
After being bitten by stack alignment problems I started wondering whether the heap should be aligned to 8-octet boundaries, as well. The ARM Cortex EABI states that for all calls to external functions the stack has to be 8-aligned. I could not find any information on whether there are any...

Fastest way to work with unaligned data on a word-aligned proccessor?

c,arm,memory-alignment,micro-optimization,cortex-m
I'm doing a project on an ARM Cortex M0, which does not support unaligned(by 4bytes) access, and I'm trying to optimize the speed of operations on unaligned data. I'm storing Bluetooth Low Energy Access addresses (48bit) as 6-byte arrays in some packed structs acting as packet buffers. Because of the...

Is this a data alignment crash? (potentially involving stack misalignment, XNAMath, Visual Studio 2103)

c++,visual-studio-2013,sse,memory-alignment,xna-math-library
My Win32, DirectX game is crashing in release mode within code that is manipulating vectors and matrices. Specifically the crash occurs on this instruction: 014E2752 unpcklps xmm1,xmmword ptr [esp+3Ch] First-chance exception at 0x014E2752 in RodinaRelease.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF I'm not too experienced with digging into assembly and...

Is a misaligned load due to a cast undefined behavior?

c,casting,undefined-behavior,memory-alignment
Is a misaligned load due a a cast from void* undefined behavior? Here's what I am seeing with Clang and its sanitizers: bufhelp.h:146:29: runtime error: load of misaligned address 0x7fff04fdd0e1 for type 'const uintptr_t' (aka 'const unsigned long'), which requires 8 byte alignment 0x7fff04fdd0e1: note: pointer points here 00 00...

What is meant by the most restrictive type in C?

c,architecture,malloc,long-integer,memory-alignment
The book The C Programming Language talks about "the most restrictive type" in section 8.7, Example — A Storage Allocator: Although machines vary, for each machine there is a most restrictive type: if the most restrictive type can be stored at a particular address, all other types may be also....

Structure packing duplicity

c++,structure,memory-alignment,preprocessor-directive,compiler-options
I am writing code to a specification which defines structures without packing, for example: struct LASHeader_1p2 { char FileSig[4]; //= "LASF"; // 4 unsigned __int16 FileSource; // 2 6 unsigned __int16 Reserved_Unused; // 2 8 unsigned __int32 Project_ID_Data1; // 4 12 unsigned __int16 Project_ID_Data2; // 2 14 unsigned __int16 Project_ID_Data3;...

Is it safe to read and write on an array of 32 bit data byte by byte?

c,memory,memory-alignment
So I have a void * data of 32 bit unsigned integers which represents the pixels. Is it okay for me to access one of the pixels with a char * and modify the values directly? Or is it better to store my new pixel in a temporary uint32_t variable...