memory-alignment

How to allocate and free aligned memory in C

让人想犯罪 __ 提交于 2019-12-20 08:40:00
问题 How do you allocate memory that's aligned to a specific boundary in C (e.g., cache line boundary)? I'm looking for malloc/free like implementation that ideally would be as portable as possible --- at least between 32 and 64 bit architectures. Edit to add: In other words, I'm looking for something that would behave like (the now obsolete?) memalign function, which can be freed using free. 回答1: Here is a solution, which encapsulates the call to malloc, allocates a bigger buffer for alignment

Why C array has extra bytes at tail? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-20 07:48:26
问题 This question already has answers here : What is the purpose of allocating a specific amount of memory for arrays in C++? (5 answers) Closed 2 years ago . I examine that C array maybe have some extra bytes at tail. There are my code int a = 5; int test[] = {1,2,3,4}; int b = 5; test[-1] = 11; test[4] = 11; cout << b << endl; // 11 cout << a << endl; // 5 You can see the running result there the value of b is changed through changing test[-1] 's value. But when I change test[4] 's value, the

Structure assignment in Linux fails in ARM but succeeds in x86

只愿长相守 提交于 2019-12-19 09:49:26
问题 I've noticed something really strange. say I've got the following structure defined typedef struct { uint32_t a; uint16_t b; uint32_t c; } foo; This structure is contained in a big buffer I receive from network. The following code works in x86, but I receive SIGBUS on ARM. extern void * buffer; foo my_foo; my_foo = (( foo * ) buffer)[0]; replacing the pointer dereferencing with memcpy solved the issue. Searching about SIGBUS in ARM pointed me to the fact that this is related to memory

Why does a struct consisting of a char, short, and char (in that order), when compiled in C++ with 4-byte packing enabled, come to a 6-byte struct?

喜夏-厌秋 提交于 2019-12-19 09:10:34
问题 I thought I understood how C/C++ handled struct member alignment. But I'm getting strange results for a particular arrangement in Visual Studio 2008 and 2010. Specifically, I'm finding that a struct consisting of a char, short, and char is compiled into a 6-byte struct, even with 4- or 8-byte packing enabled. I am at a loss as to why this would be. I can understand a 4-byte struct. I could perhaps understand an 8-byte struct. But I would think that a 6-byte struct would be impossible when 4

What's the purpose of unnamed bit field at the end of structure

六眼飞鱼酱① 提交于 2019-12-19 09:03:59
问题 I am learning C. In C Primer Plus , I saw an bit field example as follows: struct box_props { bool opaque : 1; unsigned int fill_color : 3; unsigned int : 4; bool show_border : 1; unsigned int border_color : 3; unsigned int border_style : 2; unsigned int : 2; }; I do understand the 4-bit unnamed bit field in the middle is used for letting the following bits start at a new byte. However, I don't understand why there is another unnamed bit field at the end of the structure. What's the purpose

Passing struct with pointer members to OpenCL kernel using PyOpenCL

半世苍凉 提交于 2019-12-19 03:58:38
问题 Let's suppose I have a kernel to compute the element-wise sum of two arrays. Rather than passing a, b, and c as three parameters, I make them structure members as follows: typedef struct { __global uint *a; __global uint *b; __global uint *c; } SumParameters; __kernel void compute_sum(__global SumParameters *params) { uint id = get_global_id(0); params->c[id] = params->a[id] + params->b[id]; return; } There is information on structures if you RTFM of PyOpenCL [1], and others have addressed

How to make tr1::array allocate aligned memory?

元气小坏坏 提交于 2019-12-19 03:40:57
问题 You can allocate a std::vector which allocates aligned heap memory by defining your own allocator. You can allocate a c-style array on the stack using declspec align. But can you declare a tr1::array which guarantees that the element at index zero will be aligned? 回答1: tr1::array (and std::array and boost::array ) are POD, so the memory occupied by the contents is coincident with the memory of the array . So, allocate the array however you need to, and construct it with placement new .

What are the differences between #pragma pack(push, n)/#pragma pack(pop) and __attribute__((__packed__, aligned(n) )) on GCC?

北战南征 提交于 2019-12-18 13:07:10
问题 On GCC specifically (that is, compiling both with GCC), what are the differences between the way the following two work? struct foo1 { char a; int b; } __attribute__((__packed__, aligned(n) )); and: #pragma pack(push, n) struct foo2 { char a; int b; }; #pragma pack(pop) They appear to behave differently: foo1 f1; foo2 f2; int& i1 = f1.b; // ok int& i2 = f2.b; // cannot bind packed field 'f2.foo2::b' to 'int&' Why is there an error in one yet not the other? Are the memory layouts the same, at

Does valarray have contiguous memory alignment?

房东的猫 提交于 2019-12-18 07:45:28
问题 Does valarray have contiguous memory alignment? I want to pass a valarray to a function (from IPPS), which only takes pointers, by passing &myValarray[0] . But therefore I should be sure, that valarray's memory alignment is contiguous. Thanks! 回答1: Assuming you're asking whether the memory managed by a valarray is guaranteed to be contiguous , then the answer is yes, at least if the object isn't const (C++03, §26.3.2.3/3 or C++11, §26.6.2.4/2): The expression &a[i+j] == &a[i] + j evaluates as

Memory alignment of arrays

不羁的心 提交于 2019-12-18 07:43:36
问题 I am having trouble aligning memory for DMA transfer on the Cell processor. I need the last 4 bits of an address to be 0. I have 4 arrays of unsigned int where each element must be aligned in memory so that its (hex) adress ends with a zero. E.g. int main() { size_t i; static unsigned int a[2] __attribute__ ((aligned (16))); static unsigned int b[2] __attribute__ ((aligned (16))); static unsigned int c[2] __attribute__ ((aligned (16))); static unsigned int d[2] __attribute__ ((aligned (16)));