struct-member-alignment

Alignment of a simple class to allow array access without UB

情到浓时终转凉″ 提交于 2020-06-13 07:54:50
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

不想你离开。 提交于 2020-06-13 07:54:08
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

牧云@^-^@ 提交于 2020-06-13 07:53:47
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

How do I organize members in a struct to waste the least space on alignment?

房东的猫 提交于 2020-04-07 11:10:14
问题 [Not a duplicate of Structure padding and packing. That question is about how and when padding occurs. This one is about how to deal with it.] I have just realized how much memory is wasted as a result of alignment in C++. Consider the following simple example: struct X { int a; double b; int c; }; int main() { cout << "sizeof(int) = " << sizeof(int) << '\n'; cout << "sizeof(double) = " << sizeof(double) << '\n'; cout << "2 * sizeof(int) + sizeof(double) = " << 2 * sizeof(int) + sizeof(double

Structure padding with union members of std::bitset

给你一囗甜甜゛ 提交于 2019-12-13 23:56:36
问题 After I had solved my issue to this question I went on to expand this version of my code to incorporate the unions of the data fields from my previous template versions with this version and I have this so far: main.cpp #include <iostream> #include <type_traits> #include "Register.h" int main() { using namespace vpc; std::cout << std::boolalpha; std::cout << "std::bitset<64> is trivially copyable " << std::is_trivially_copyable<std::bitset<64>>::value << '\n' << "QWord is trivially copyable "