memory-alignment

OpenCV's cv::Mat & CvMat row alignment

人盡茶涼 提交于 2019-11-30 15:27:21
could someone pls explain to me how the row alignment of OpenCV's CvMat (or its C++ version cv::Mat ) works? For instance, let's assume I have a matrix CvMat *cvmat = cvCreateMat(2,3,CV_8UC1); cvSet2D( cvmat, 0, 0, cvScalar(1) ); cvSet2D( cvmat, 0, 1, cvScalar(2) ); cvSet2D( cvmat, 0, 2, cvScalar(3) ); cvSet2D( cvmat, 1, 0, cvScalar(4) ); cvSet2D( cvmat, 1, 1, cvScalar(5) ); cvSet2D( cvmat, 1, 2, cvScalar(6) ); According to the documentation of CvMat , rows should be aligned by 4 bytes, i.e. first row of the matrix should be padded by one zero and the second should start at the offset +4).

Understanding stack allocation and alignment

独自空忆成欢 提交于 2019-11-30 15:12:34
问题 I'm trying to understand how stack alignment works as described in what is "stack alignment"? but I have trouble getting a small example to demonstrate the said behaviour. I'm examining the stack allocation of my function foo: void foo() { int a = 0; char b[16]; b[0] = 'a'; } I compiled the source file with gcc -ggdb example.c -o example.out (i.e without any compiler flags) and the assembler dump from gdb reads: (gdb) disassemble foo Dump of assembler code for function foo: 0x08048394 <+0>:

Misaligned address using virtual inheritance

我怕爱的太早我们不能终老 提交于 2019-11-30 14:31:07
The following apparently valid code produces a misaligned address runtime error using the UndefinedBehaviorSanitizer sanitiser. #include <memory> #include <functional> struct A{ std::function<void()> data; // seems to occur only if data is a std::function } ; struct B{ char data; // occurs only if B contains a member variable }; struct C:public virtual A,public B{ }; struct D:public virtual C{ }; void test(){ std::make_shared<D>(); } int main(){ test(); return 0; } Compiling and executing on a macbook with clang++ -fsanitize=undefined --std=c++11 ./test.cpp && ./a.out produces the output

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

半世苍凉 提交于 2019-11-30 12:59:39
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 least? You don't say which version of GCC you're using, but you can find the appropriate manual on

Does the alignas specifier work with 'new'?

China☆狼群 提交于 2019-11-30 08:27:02
问题 My question is rather simple; Does the alignas specifier work with 'new'? That is, if a struct is defined to be aligned, will it be aligned when allocated with new? 回答1: If your type's alignment is not over-aligned, then yes, the default new will work. "Over-aligned" means that the alignment you specify in alignas is greater than alignof(std::max_align_t) . The default new will work with non-over-aligned types more or less by accident; the default memory allocator will always allocate memory

Aligned and unaligned memory accesses?

送分小仙女□ 提交于 2019-11-30 06:31:23
问题 What is the difference between aligned and unaligned memory access? I work on an TMS320C64x DSP, and I want to use the intrinsic functions (C functions for assembly instructions) and it has ushort & _amem2(void *ptr); ushort & _mem2(void *ptr); where _amem2 does an aligned access of 2 bytes and _mem2 does unaligned access. When should I use which? 回答1: An aligned memory access means that the pointer (as an integer) is a multiple of a type-specific value called the alignment. The alignment is

Prohibit unaligned memory accesses on x86/x86_64

倖福魔咒の 提交于 2019-11-30 05:01:33
I want to emulate the system with prohibited unaligned memory accesses on the x86/x86_64. Is there some debugging tool or special mode to do this? I want to run many (CPU-intensive) tests on the several x86/x86_64 PCs when working with software (C/C++) designed for SPARC or some other similar CPU. But my access to Sparc is limited. As I know, Sparc always checks alignment in memory reads and writes to be natural (reading a byte from any address, but reading a 4-byte word only allowed when address is divisible by 4). May be Valgrind or PIN has such mode? Or special mode of compiler? I'm

C++ Multiple Inheritance Memory Layout with “Empty classes”

时光总嘲笑我的痴心妄想 提交于 2019-11-30 03:51:15
问题 I know the memory layout of multiple inheritance is not defined, so I should not rely on it. However, can I rely on it in a special case. That is, a class has only one "real" super class. All others are "empty classes", i.e., classes that neither have fields nor virtual methods (i.e. they only have non-virtual methods). In this case, these additional classes should not add anything to the memory layout of the class. (More concisely, in the C++11 wording, the class has standard-layout ) Can I

How can I align a struct to a specifed byte boundary?

匆匆过客 提交于 2019-11-30 03:24:35
问题 I need to align a struct to a 16 byte boundary in Rust. It seems possible to give hints about alignment thorough the repr attribute, but it doesn't support this exact use case. A functional test of what I'm trying to achieve is a type Foo such that assert_eq!(mem::align_of::<Foo>(), 16); or alternatively, a struct Bar with a field baz such that println!("{:p}", Bar::new().baz); always prints a number divisible by 16. Is this currently possible in Rust? Are there any work-arounds? 回答1: huon's

How to align std::array contained data?

两盒软妹~` 提交于 2019-11-30 01:55:48
问题 Since std::array does not allow changing its allocator, is there a way to ensure that the pointer to the data address is aligned? For instance, in GNU g++ 4.8.4 and 6.1.0, the code below #include <array> #include <iostream> int main(void) { std::array<bool, 10> a; std::array<char, 10> b; std::array<int,10> c; std::array<long long, 10> d; std::array<float, 10> e; std::array<double, 10> f; std::cout << "array<bool,10>.data() = " << a.data() << std::endl; std::cout << "array<char,10>.data() = "