memory-alignment

Struct Reordering by compiler [duplicate]

喜欢而已 提交于 2019-12-06 16:31:56
问题 This question already has answers here : Why can't C compilers rearrange struct members to eliminate alignment padding? [duplicate] (11 answers) Closed 3 years ago . Suppose I have a struct like this: struct MyStruct { uint8_t var0; uint32_t var1; uint8_t var2; uint8_t var3; uint8_t var4; }; This is possibly going to waste a bunch (well not a ton) of space. This is because of necessary alignment of the uint32_t variable. In actuality (after aligning the structure so that it can actually use

How to expose aligned class with boost.python

陌路散爱 提交于 2019-12-06 15:07:21
When trying to expose aligned class like this: class __declspec(align(16)) foo { public: void foo_method() {} }; BOOST_PYTHON_MODULE(foo_module) { class_<foo>("foo") .def("foo_method", &foo::foo_method); } You end up with error (msvs 2010): error C2719: 'unnamed-parameter': formal parameter with __declspec(align('16')) won't be aligned, see reference to class template instantiation 'boost::python::converter::as_to_python_function<T,ToPython>' being compiled The solution I found so far, is to use smart pointer to store object: BOOST_PYTHON_MODULE(foo_module) { class_<foo, boost::shared_ptr<foo>

Undefined reference to posix_memalign using mingw32

﹥>﹥吖頭↗ 提交于 2019-12-06 11:35:21
I'm using Debian Squeeze, cross compiling for windows targets using mingw32. For a Linux target, I can use posix_memalign to allocate aligned memory. I can't seem to find a way to get this to work for windows targets; I get errors about undefined references. I have tried several alternative functions, to no avail. Example Code: #include <stdio.h> #include <stdlib.h> #include <malloc.h> int main(void) { char *foo; /* works on linux */ posix_memalign(&foo, 1024, 1024); /* deprecated linux */ memalign(1024, 1024); valloc(1024); /* should work on windows only */ _aligned_malloc(1024, 1024); }

Detecting Aligned Memory requirement on target CPU

允我心安 提交于 2019-12-06 06:48:33
问题 I'm currently trying to build a code which is supposed to work on a wide range of machines, from handheld pockets and sensors to big servers in data centers. One of the (many) differences between these architectures is the requirement for aligned memory access. Aligned memory access is not required on "standard" x86 CPU, but many other CPU need it and produce an exception if the rule is not respected. Up to now, i've been dealing with it by forcing the compiler to be cautious on specific data

Memory alignment for fast FFT in Python using shared arrays

China☆狼群 提交于 2019-12-06 03:25:05
问题 I write an image processing app that needs to do multiple things and it has to do them as much real-time as possible. Acquisition of the data and their processing runs in separate processes (mainly for performance reasons). The data itself is quite large (2MPix 16-bit grayscale images). I can share arrays between processes as it is described in this post: How do I pass large numpy arrays between python subprocesses without saving to disk? (I use the shmarray script from the numpy-shared

Does std::allocator handle over-aligned types in C++17?

半城伤御伤魂 提交于 2019-12-06 02:42:09
问题 C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator ? Does it handle over-aligned types? 回答1: In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2 T* allocate(size_t n); Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T . Compared to C++14, the sentence It is implementation-defined whether over-aligned types are supported has

Why alignment is power of 2?

[亡魂溺海] 提交于 2019-12-05 20:22:50
问题 There is a quote from cppreference: Every object type has the property called alignment requirement, which is an integer value (of type std::size_t, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated. I understand, this reference is non-normative. But there is no something about value of alignof(T) in the standard, rather than it is no more than alignof(std::max_align_t) . It is not obviously, that alignment is

Find holes in C structs due to alignment

懵懂的女人 提交于 2019-12-05 16:55:28
问题 Is there a way in gcc or clang (or any other compiler) to spit information about whether a struct has holes (memory alignment - wise) in it ? Thank you. ps: If there is another way to do it, please do inform me. 回答1: You can use pahole to output information about holes in structures and optionally attempt packing them. You may want to read "Poke-a-hole and friends" and the pahole announcement for more information 回答2: I Don't know any automatic tool, but this could be helpful example:

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

*爱你&永不变心* 提交于 2019-12-05 16:43:36
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 a 16-bit boundary and I anticipate alignment issues. It's been a while since I've done any thing like

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

≡放荡痞女 提交于 2019-12-05 15:48:01
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 *((unsigned short*)tmp) = 1; tmp += sizeof(unsigned short); // attempt to push data of type T *((T*)tmp) = (T)1