memory-alignment

Behind Windows x64's 44-bit virtual memory address limit

不羁岁月 提交于 2020-01-04 06:07:08
问题 http://www.alex-ionescu.com/?p=50. I read the above post. The author explains why Windows x64 supports only 44-bit virtual memory address with singly linked list example. struct { // 8-byte header ULONGLONG Depth:16; ULONGLONG Sequence:9; ULONGLONG NextEntry:39; } Header8; The first sacrifice to make was to reduce the space for the sequence number to 9 bits instead of 16 bits, reducing the maximum sequence number the list could achieve. This still only left 39 bits for the pointer — a

#pragma pack, template typedefs, and struct alignment

天涯浪子 提交于 2020-01-04 04:26:06
问题 Using Visual Studio or gcc, if I've got #pragma pack(push, 16) typedef std::map<uint32_t, uint32_t> MyIntMap; #pragma pack(pop) then later: #pragma pack(push, 8) MyIntMap thisInstance; #pragma pack(pop) What is the structure alignment of thisInstance? That is, for a typedef'd template class, does pragma pack take effect at the place of the typedef or at the place of a variable definition? If it's the latter, what's a good workaround to get a type with consistent alignment across files? 回答1:

What can I use instead of std::aligned_alloc in MS Visual Studio 2013?

為{幸葍}努か 提交于 2020-01-04 02:16:27
问题 I would like to use C++11's std::aligned_alloc , but unfortunately it isn't available with Microsoft Visual Studio 2013. I'm considering, intsead, implementing aligned_alloc on my own. How should an implementation look like? The following for example doesn't compile, because it cannot convert from void* to void*& . template<typename T> T* aligned_alloc( std::size_t size, std::size_t align ) { T* ptr = new T[size + align]; std::align(align, size, reinterpret_cast<void*>(ptr), align + size);

Is it guaranteed that array elements in C will be stored consecutively, with no padding?

╄→尐↘猪︶ㄣ 提交于 2020-01-04 02:02:26
问题 In other words: is it guaranteed that if I have an array allocated this way: void *arr = calloc(nmemb, sizeof(some_type)) Then elta , eltb , eltc will all point to the same location in memory, which will be the second element of type some_type of this array? some_type *elta = &((some_type*)arr)[1]; some_type *eltb = ((some_type*)arr)+1; some_type *eltc = (char*)arr+sizeof(some_type); The reason I’m asking this is because I’m trying to do a “container” in C, and if this doesn’t hold then I’m

How is this size alignment working

人走茶凉 提交于 2020-01-01 05:23:05
问题 I am not able to understand the below code with respect to the comment provided. What does this code does, and what would be the equivalent code for 8-aligned ? /* segment size must be 4-aligned */ attr->options.ssize &= ~3; Here, ssize is of unsigned int type. 回答1: Since 4 in binary is 100, any value aligned to 4-byte boundaries (i.e. a multiple of 4) will have the last two bits set to zero. 3 in binary is 11, and ~3 is the bitwise negation of those bits, i.e., ...1111100. Performing a

How can I simulate alignas(T)?

泄露秘密 提交于 2020-01-01 05:22:14
问题 I have an array which is used as the underlying memory of an object of type T : char memory[sizeof T]; . . . new(memory) T(whatever); How can I make sure memory is aligned correctly for T objects? In C++0x I could just say: alignas(T) char memory[sizeof T]; but Visual Studio 2010 does not support that particular feature yet. 回答1: The usual (portable) solution is to put the memory declaration in a union with whatever built-in type in T requires the most alignment. The simplest way would be to

Naming Array Elements, or Struct And Array Within a Union

南楼画角 提交于 2019-12-30 17:21:53
问题 Consider the following struct: struct Vector4D { union { double components[4]; struct { double x, y, z, t; } Endpoint; }; }; It seems to me that I have seen something similar in WinApi's IPAddress struct. The idea is to give me the possibility to use the array components both by index and by name, for example: Vector4D v; v.components[2] = 3.0; ASSERT(v.Endpoint.z == 3.0) //let's ignore precision issues for now In the C++ standard there is a guarantee that there will be no "empty" space at

Cross-platform ALIGN(x) macro?

▼魔方 西西 提交于 2019-12-29 03:37:12
问题 I would like to create a struct that has a certain alignment. I would like to use the same struct definition for both GCC and VisualC++ compilers. In VisualC++, one typically does this: __declspec(align(32)) struct MyStruct { // ... }; In GCC, one typically does this: struct MyStruct { // ... } __attribute__ ((aligned (32))); I could of course create the appropriate macros to make this work: BEGIN_ALIGNED_STRUCT(32) struct { // ... } END_ALIGNED_STRUCT(32) ; And thus be able to handle both

Aligning static string literals

爷,独闯天下 提交于 2019-12-28 03:10:08
问题 I have a static array of structures: struct CommandStruct { char* data; unsigned ans_size; }; static const CommandStruct commands[] = { { "Some literal", 28 }, { "Some other literal", 29 }, { "Yet another literal", 8 }, }; And I want the strings to be 16-byte aligned. Is it possible to achieve it directly? I might get away with defining each literal separately, like __declspec(align(16)) static const char some_command_id[] = "my literal" , but that's a mess. I need all initialization in a

SSE: why, technically, is 16-aligned data faster to move?

会有一股神秘感。 提交于 2019-12-25 18:43:16
问题 Is it a bus architecture issue? How is it circumvented in i7? I'm aware of this, I just don't think it answers the real why . 回答1: The processor is built to work with data of certain sizes and alignments. When you use data outside of those sizes and alignments, you effectively need to shift it into alignment, crop it, compute on it using the normal instructions, then shift it back into place. 来源: https://stackoverflow.com/questions/24963646/sse-why-technically-is-16-aligned-data-faster-to