memory-alignment

What is the required alignment of long double in 64-bit linux, mac, and others?

此生再无相见时 提交于 2020-08-05 06:30:46
问题 I have been trying to find out exactly what is the required alignment of long double in x86-64 mac and linux. I have found various different things that hint at different requirements, but can't find an authorative source. I am finding on Mac OS X, gcc will generate code sometimes which copy long doubles using movaps, which requires 16-byte alignment. But clang does not and gcc is very old, so maybe this is just an old bug which is not going to get fixed? 回答1: The Linux x86_64 ABI specifies

Is the size of an object equivalent to the size of another based upon the same alignment and/or representation?

萝らか妹 提交于 2020-07-23 06:45:32
问题 The C standard states (emphasize mine): 28 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment

atomic_ref when external underlying type is not aligned as requested

╄→尐↘猪︶ㄣ 提交于 2020-06-27 18:35:08
问题 I read on p0019r8 the following: atomic_ref(T& obj); Requires : The referenced object shall be aligned to required_alignment . cppreference interprets this as UB when not aligned: The behavior is undefined if obj is not aligned to required_alignment. So how would you expect from an implementation to handle it? And implementation can check compile-time alignof , but in reality a type might be aligned more that alignof . An implementation can interpret pointer bits and check runtime 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 is the alignment of the physical memory guaranteed?

醉酒当歌 提交于 2020-05-15 04:57:07
问题 malloc() returns a memory suitably aligned for any in-built type. In cases where more specific alignment is required (like 16 or 32 bytes) it can be done at the application level. But this alignment is at the virtual memory level. How is it guaranteed that the underlying physical memory is also at the same alignment ? 回答1: Virtual memory is implemented at the page level, so every VM page maps to a physical memory page when it's loaded into memory. So everything more finely grained than the

Why are all arrays aligned to 16 bytes on my implementation?

核能气质少年 提交于 2020-05-08 09:27:31
问题 My very simple code shows below #include <iostream> #include <stdalign.h> int main() { char array_char[2] = {'a', 'b'}; float array_float[2] = {1, 2}; std::cout << "alignof(array_char): " << alignof(array_char) << std::endl; std::cout << "alignof(array_float): " << alignof(array_float) << std::endl; std::cout << "address of array_char: " << (void *) array_char << std::endl; std::cout << "address of array_float: " << array_float << std::endl; } The output of this code is alignof(array_char): 1

Why are all arrays aligned to 16 bytes on my implementation?

元气小坏坏 提交于 2020-05-08 09:27:24
问题 My very simple code shows below #include <iostream> #include <stdalign.h> int main() { char array_char[2] = {'a', 'b'}; float array_float[2] = {1, 2}; std::cout << "alignof(array_char): " << alignof(array_char) << std::endl; std::cout << "alignof(array_float): " << alignof(array_float) << std::endl; std::cout << "address of array_char: " << (void *) array_char << std::endl; std::cout << "address of array_float: " << array_float << std::endl; } The output of this code is alignof(array_char): 1

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