memory-alignment

Objective-C Runtime: What to put for size & alignment for class_addIvar?

久未见 提交于 2019-12-09 13:20:47
问题 The Objective-C Runtime provides the class_addIvar C function: BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) What do I put for size and alignment ? I'm adding an instance variable of type UITextPosition * , but no UITextPosition object is in scope. For size , can I just do sizeof(self) , where self is a subclass of UITextField ? I.e., can I assume that a UITextPosition object is the same size as a UITextField object? How do I get alignment

When are pad bytes copied - struct assignment, pass by value, other?

◇◆丶佛笑我妖孽 提交于 2019-12-09 00:16:16
问题 While debugging a problem, the following issue came up. (Please ignore minor code errors; the code is just for illustration.) The following struct is defined: typedef struct box_t { uint32_t x; uint16_t y; } box_t; Instances of this struct are being passed by value from function to function (obviously simplified): void fun_a(box_t b) { ... use b ... } void fun_b(box_t bb) { // pass bb by value int err = funa(bb); } void fun_c(void) { box_t real_b; box_t some_b[10]; ... ... use real_b and some

Are members of a POD-struct or standard layout type guaranteed to be aligned according to their alignment requirements?

馋奶兔 提交于 2019-12-08 15:02:45
问题 Given a POD-struct (in C++03) or a standard layout type (in C++11), with all members having a fundamental alignment requirement, is it true that every member is guaranteed to be aligned according to its alignment requirement? In other words, for all members m_k in { m0 ... mn } of standard layout type S , struct S { T0 m0; T1 m1; ... TN mn; }; is the following expression guaranteed to evaluate to true ? (offsetof(S,m_k) % alignof(decltype(S::m_k))) == 0 Please give answers for both C++03 and

How to expose aligned class with boost.python

一曲冷凌霜 提交于 2019-12-08 08:57:37
问题 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

Undefined reference to posix_memalign using mingw32

戏子无情 提交于 2019-12-07 21:31:00
问题 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 */

#pragma pack in C++

∥☆過路亽.° 提交于 2019-12-07 15:22:12
问题 why do we need to have #pragma pack for typedef structure in C++? Specifically when you are using those structure in network communication. 回答1: #pragma pack controls the alignment of members of a structure. The common default setting is 8, ensuring that members that are up to 8 bytes long get aligned on an address that's a multiple of their size. A double or 64-bit pointer for example. Reading or writing a mis-aligned double can be quite expensive, typically three times slower if it

Is there cases where a 32-bit variable could not been properly-aligned

纵饮孤独 提交于 2019-12-07 05:06:27
问题 In the following link: http://msdn.microsoft.com/en-us/library/ms684122(VS.85).aspx, it is said that "Simple reads and writes to properly-aligned 32-bit variables are atomic operations". I'm wondering if in a c++ program all 32-bit variables are by default properly-aligned. Saying differently is there any case where a 32-bit variable could not been properly-aligned. 回答1: If you don't tell the compiler to do otherwise, then it will align 32-bit variables correctly. You can write code which

What alignment issues limit the use of a block of memory created by malloc?

杀马特。学长 韩版系。学妹 提交于 2019-12-07 03:29:09
问题 I am writing a library for various mathematical computations in C. Several of these need some "scratch" space -- memory that is used for intermediate calculations. The space required depends on the size of the inputs, so it cannot be statically allocated. The library will typically be used to perform many iterations of the same type of calculation with the same size inputs, so I'd prefer not to malloc and free inside the library for each call; it would be much more efficient to allocate a

What are the alignment limitations of the standard global default operator new?

点点圈 提交于 2019-12-07 01:03:09
问题 I'm working on some older code that uses ATL's CComBSTR type. I'm changing it so that it will compile using Visual C++ Express Edition, which does not come with ATL. I used only a very small subset of CComBSTR , so doing this is fairly simple. However, when allocating the BSTR memory block, I need to fill the first four bytes with a 4 byte length prefix. I'm concerned that if I use a new char[size] expression to allocate the memory for the string, that I will cause alignment faults due to the

Common initial sequence and alignment

删除回忆录丶 提交于 2019-12-06 18:31:26
问题 While thinking of a counter-example for this question, I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B ? struct B { char byte; }; Furthermore, if we have struct A { alignas(2) char x; alignas(4) char y; }; // possible alignment, - is padding // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 // x - - - y - - - x - - - y - - - struct B { char x; char y; }; // no padding required union U { A a; B b; } u; Is