memory-alignment

Plain Old Data and `std::memcpy` alignment issues

依然范特西╮ 提交于 2019-12-25 07:17:34
问题 Trying to respond to another question, I've proposed a solution that use std::memcpy() to store generic types in a buffer of char s. My doubt is about possible memory alignment issues storing POD (I know that with not-POD type, as std::string , is very very dangerous). In short: there are memory alignment issues with the following program? And if they are, it's possible to write something similar (that store POD values in a char buffer) that is safe? And how? #include <cstring> #include

MPI Derived data type for a struct with flexible size

时间秒杀一切 提交于 2019-12-24 12:47:12
问题 I am trying to send/recv in C++ a data structure that looks like this: /* PSEUDOCODE */ const int N = getN(); // not available at compile time const int M = getM(); struct package{ int foo; double bar; /* I know array members do not work this way, this is pseudocode. */ int flop[N]; double blep[M]; }; Since M and N are constant during runtime, I can do MPI_Type_create_struct() and the new datatype woule be good throughout. My question is how to implement the data structure as described above.

Fortran90 derived types with mpi, alignment issue?

人盡茶涼 提交于 2019-12-24 12:07:47
问题 I got problem with the following basic code: program foo use mpi implicit none type bartype real(8) :: x integer :: i end type bartype integer :: mpi_bar_type integer :: & count=2, & blocklengths(2)=(/1,1/), & types(2)=(/mpi_double_precision, & mpi_integer/) integer(kind=mpi_address_kind) :: displs(2) type(bartype) :: bar, bararray(4) integer :: rank, ierr, i, test(4), addr0 call mpi_init(ierr) call mpi_comm_rank(mpi_comm_world, rank, ierr) call mpi_get_address(bar, addr0) call mpi_get

Declare, manipulate and access unaligned memory in C++ [closed]

纵然是瞬间 提交于 2019-12-24 10:49:16
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I recently posted a question about unaligned memory access, but given the answer, I am a little lost. I often hear that "aligned memory access is far more efficient than unaligned access", but I am actually not sure what is unaligned memory. Consequently: What is unaligned memory?

Why “any primitive object of K bytes must have an address that is a multiple of K”?

别说谁变了你拦得住时间么 提交于 2019-12-24 10:26:56
问题 Computer Systems: a Programmer's Perspective says The x86-64 hardware will work correctly regardless of the alignment of data. However, Intel recommends that data be aligned to improve memory system performance. Their alignment rule is based on the principle that any primitive object of K bytes must have an address that is a multiple of K. We can see that this rule leads to the following alignments: K Types 1 char 2 short 4 int, float 8 long, double, char * Why is it that "any primitive

JNA structure mapping with no alignment and padding

一个人想着一个人 提交于 2019-12-24 01:48:14
问题 I have the below structures in JNA. I want to remove the padding and alignment in C using pragma pack. When I run it from C it runs fine. I'm using this DLL to be called from Java. When I call it the JVM is crashing. Would my settings I did in my DLLs apply when its called from Java? I tried adding setAlignment type to None. Still no help. In C this structure is weighing 405. It adds up perfectly meaning there is no padding or alignment. In java when I hover over the _report value while

C/C++ Struct memory layout equivalency

↘锁芯ラ 提交于 2019-12-24 00:23:49
问题 Consider the following C struct and C++ struct declarations: extern "C" { // if this matters typedef struct Rect1 { int x, y; int w, h; } Rect1; } struct Vector { int x; int y; } struct Rect2 { Vector pos; Vector size; } Are the memory layouts of Rect1 and Rect2 objects always identical? Specifically, can I safely reinterpret_cast from Rect2* to Rect1* and assume that all four int values in the Rect2 object are matched one on one to the four int s in Rect1 ? Does it make a difference if I

Always same effect of #pragma pack(16) and #pragma pack(8)?

痴心易碎 提交于 2019-12-23 22:52:06
问题 I am trying to align data members by using #pragma pack (n). Take the following as an example: #include <iostream> using namespace std; #pragma pack(8) // or (16) struct A { int a; char b; char c; char d; char e; char f; double g; }; int main() { cout << sizeof(A) << endl; return 0; } Both will print 24 for #pragma pack(8) and #pragma pack(16) . I can understand the result for n=8 with the data alignment, of my understanding, as follows: Bytes: |1 2 3 4|5|6|7|8|9|10 11 12 13 14 15 16|17 18 19

Aligned vs. Packed attributes

纵然是瞬间 提交于 2019-12-23 19:00:30
问题 I am working on firmware for a 16-bit PIC and writing in C (Microchip C30 compiler). My device receives a long list of bytes from an external device, and then I am trying to copy those bytes into a structure. The structure is defined as follows: typedef struct __attribute__((__packed__)) { char F1Nickname[17]; char F2Nickname[17]; DWORD F1CurrentPos; DWORD F2CurrentPos; WORD F1CurrentTemp; WORD F2CurrentTemp; DWORD F1MaxPos; DWORD F2MaxPos; BYTE F1TempCompOn; BYTE F2TempCompOn; BYTE CheckSum;

__declspec(align) for multiple declarations

匆匆过客 提交于 2019-12-23 18:06:49
问题 Sorry for the very simple question, couldn't find a googleable answer. Is this declaration syntax: __declspec(align(16)) float rF[4]; __declspec(align(16)) float gF[4]; __declspec(align(16)) float bF[4]; Equivalent to this: __declspec(align(16)) float rF[4], gF[4], bF[4]; Or will only the first variable be aligned in the latter syntax? If it matters, these are local variables inside a global method. 回答1: Yes. A __declspec is part of the storage class and applies to all declarators in the