memory-alignment

Are reads and writes to unaligned fields in .NET definitely atomic?

守給你的承諾、 提交于 2019-12-17 21:02:00
问题 The C# specification (ECMA-334 and ISO/IEC 23270) has a paragraph about the atomicity of reads and writes: 12.5 Atomicity of variable references Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list shall also be atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined

CUDA kernel launch fails when using various offsets into input data

给你一囗甜甜゛ 提交于 2019-12-17 19:31:09
问题 My code is giving an error message and I am trying to track down the cause of it. To make it easier to find the problem, I have stripped away code that apparently is not relevant to causing the error message. If you can tell me why the following simple code produces an error message, then I think I should be able to fix my original code: #include "cuComplex.h" #include <cutil.h> __device__ void compute_energy(void *data, int isample, int nsamples) { cuDoubleComplex * const nminusarray =

When extending a padded struct, why can't extra fields be placed in the tail padding?

匆匆过客 提交于 2019-12-17 18:47:37
问题 Let's consider the structs : struct S1 { int a; char b; }; struct S2 { struct S1 s; /* struct needed to make this compile as C without typedef */ char c; }; // For the C++ fans struct S3 : S1 { char c; }; The size of S1 is 8, which is expected due to alignment. But the size of S2 and S3 is 12. Which means the compiler structure them as : | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| | a | b | padding | c | padding | The compiler could place c in the padding in 6 7 8 without breaking

Memory alignment : how to use alignof / alignas?

荒凉一梦 提交于 2019-12-17 17:25:55
问题 I work with shared memory right now. I can't understand alignof and alignas . cppreference is unclear : alignof returns "alignment" but what is "alignment" ? number of bytes to add for the next block to be aligned ? padded size ? Stack overflow / blogs entries are unclear too. Can someone explain clearly alignof and alignas ? 回答1: Alignment is a restriction on which memory positions a value's first byte can be stored. (It is needed to improve performance on processors and to permit use of

Do I really have to worry about alignment when using placement new operator?

六眼飞鱼酱① 提交于 2019-12-17 15:39:27
问题 I read this When should I worry about alignment? but I am still do not know if I have to worry about not aligned pointer returned by placement new operator - like in this example: class A { public: long double a; long long b; A() : a(1.3), b(1234) {} }; char buffer[64]; int main() { // (buffer + 1) used intentionally to have wrong alignment A* a = new (buffer + 1) A(); a->~A(); } __alignof(A) == 4 , (buffer + 1) is not aligned to 4 . But everything works fine - full example here: http:/

best cross-platform method to get aligned memory

浪尽此生 提交于 2019-12-17 15:37:12
问题 Here is the code I normally use to get aligned memory with Visual Studio and GCC inline void* aligned_malloc(size_t size, size_t align) { void *result; #ifdef _MSC_VER result = _aligned_malloc(size, align); #else if(posix_memalign(&result, align, size)) result = 0; #endif return result; } inline void aligned_free(void *ptr) { #ifdef _MSC_VER _aligned_free(ptr); #else free(ptr); #endif } Is this code fine in general? I have also seen people use _mm_malloc , _mm_free . In most cases that I want

Alignment requirements for atomic x86 instructions vs. MS's InterlockedCompareExchange documentation?

守給你的承諾、 提交于 2019-12-17 05:53:19
问题 Microsoft offers the InterlockedCompareExchange function for performing atomic compare-and-swap operations. There is also an _InterlockedCompareExchange intrinsic . On x86 these are implemented using the lock cmpxchg instruction. However, reading through the documentation on these three approaches, they don't seem to agree on the alignment requirements. Intel's reference manual says nothing about alignment (other than that if alignment checking is enabled and an unaligned memory reference is

Memory alignment in C-structs

三世轮回 提交于 2019-12-17 04:17:38
问题 I'm working on the 32-bit machine, so I suppose that memory alignment should be 4 bytes. Say I have struct: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; } myStruct; the real size is 6 bytes, and I suppose that aligned size should be 8, but sizeof(myStruct) returns me 6. However if I write: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; int i; } myStruct; the real size is 10 bytes, aligned is 12, and this time sizeof(myStruct) == 12 . Can

Sparc Function compilation alignment

半城伤御伤魂 提交于 2019-12-13 12:50:37
问题 I want my program such that each function in the binary has some space left after it ends. So that later if some minor change is required only that function is changed with the extra space acting as room for accounting the minor change. -falign-function can do the job but it will not give consistent space. Is there anyway to do it? Or better way to do it? 回答1: You could use an inline assembly statement to add a series of nops at the start (or end) of each function. Then later when you need to

gcc memory alignment using malloc

老子叫甜甜 提交于 2019-12-13 11:09:47
问题 I've the following struct: #define M 3 #pragma pack(push) #pragma pack(1) struct my_btree_node { struct my_btree_node *pointers[M]; unsigned char *keys[M - 1]; int data[M - 1]; unsigned char number_of_keys; }; #pragma pack(pop) The sizeof(struct my_btree_node) function returns a value of 49 byte for this struct. Does allocating memory for this struct using malloc return a 64 byte block because on 64 bit systems pointers are 16-byte-aligned or will it indeed be 49 bytes? Is there a way to