memory-alignment

What is meant by the most restrictive type in C?

旧巷老猫 提交于 2019-12-01 02:55:11
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. On some machines, the most restrictive type is a double ; on others, int or long suffices. In their code, the union header is aligned using the type long . What is meant by the most restrictive type? Is it perhaps the largest type (e.g., double ), or is there another approach? CPUs often require that

Accessing struct members with array subscript operator

微笑、不失礼 提交于 2019-12-01 01:14:19
Let have a type T and a struct having ONLY uniform elements of T type. struct Foo { T one, T two, T three }; I'd like to access them in fallowing way: struct Foo { T one, T two, T three T &operator [] (int i) { return *(T*)((size_t)this + i * cpp_offsetof(Foo, two)); } }; where cpp_offsetof macro (it is considered to be correct) is: #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8) The C++ standard doesn't guarantee it, but can we assume that members are distanced by a fixed offset and above is correct, cross-platform solution? 100%

Why aligning of long long union member is bigger than the containing union/struct? Is this correct?

旧巷老猫 提交于 2019-12-01 00:27:12
问题 From this question one could start to believe that alignment of a union is not less than the largest alignment of it's individual members. But I have a problem with the long long type in gcc/g++. The full example can be found here, but here are the relevant parts for my question: union ull { long long m; }; struct sll { long long m; }; int main() { #define pr(v) cout << #v ": " << (v) << endl pr(sizeof(long long)); pr(__alignof__(long long)); pr(sizeof(ull)); pr(__alignof__(ull)); pr(sizeof

What is meant by the most restrictive type in C?

安稳与你 提交于 2019-11-30 23:48:01
问题 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. On some machines, the most restrictive type is a double ; on others, int or long suffices. In their code, the union header is aligned using the type long . What is meant by the most restrictive type?

Passing struct with pointer members to OpenCL kernel using PyOpenCL

拥有回忆 提交于 2019-11-30 23:38:57
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 this question too [2] [3] [4]. But none of the OpenCL struct examples I've been able to find have

Accessing struct members with array subscript operator

六眼飞鱼酱① 提交于 2019-11-30 20:53:33
问题 Let have a type T and a struct having ONLY uniform elements of T type. struct Foo { T one, T two, T three }; I'd like to access them in fallowing way: struct Foo { T one, T two, T three T &operator [] (int i) { return *(T*)((size_t)this + i * cpp_offsetof(Foo, two)); } }; where cpp_offsetof macro (it is considered to be correct) is: #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8) The C++ standard doesn't guarantee it, but can we assume

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

流过昼夜 提交于 2019-11-30 20:08:25
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 infer that all the superclasses will have no offset? E.g.: #include <iostream> class X{ int a; int b; }

How to align std::array contained data?

烈酒焚心 提交于 2019-11-30 18:15:38
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() = " << (void*) b.data() << std::endl; std::cout << "array<int,10>.data() = " << c.data() << std::endl; std:

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

泄露秘密 提交于 2019-11-30 15:51:27
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_b[] ... ... funb(real_b); funb(some_b[3]); ... box_t copy_b = some_b[5]; ... } In some cases, two

OpenCV's cv::Mat & CvMat row alignment

本小妞迷上赌 提交于 2019-11-30 15:50:37
问题 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.