c++ - Using any buffer to store any data, with placement new and memcpy -


It's a while that I'm reading documents about alignment, aliasing, padding and placement new, but still not me Be sure to solve this problem - this is the first time that I am facing a memory issue at this level, so I do not believe.

The problem is: I have a buffer, an array of data that uses some type typeT , such as

  typeT buff [N];   

Inside that buffer, I have to store some other data, type typeU at the beginning of buff . Let the element of the of of , which is equal to or equal to M element of type is.

EDIT: This problem depends on the data type, but my question is normal, and hold for POD and non-POD. But you can assume that non-POD is just POD type aggregation, there is no dynamic memory to copy, and all the data is within the bytes of the sizeof (typeX) structure. That's it, Do not worry about the deep / shallow transcription .

I am more interested in this matter where typeT and typeU are different alignments

Question 1 : (The most important question) is that always is safe to use the new placement to store my data, considering this fact on buffer Can I access those data using TyU type always , like in the following code?

  typeU * allocData = new (buff) type u [k]; AllocData [0] = foo;   

Question 2 : This always copy any data from typeU * inside a buffer Using memcpy using typeT * , I can always access the data using the following code in typeU * Am I Typu Promptata [k]; Mammakpi (Bref, Preddata, Saiff (Typu) * K); // Is it safe to do? TypeU * accessData = reinterpret_cast & lt; Typeus * & gt; (Buff); Access data [1] = foo; Question 3 : (at least the important question) Is it true that it is not always safe for only the cast pointer and using those people Write data? Like

  typeU * castData = (typeU *) buff; CastData [2] = Something;   

Even if I always use the first field of memory (first m elements) using the typeU and use the second code (element of M from N) While doing TypeT ?

I hope it is clear what I mean ... I searched a lot, but I am still confused.

Also, please note that I am talking about a C ++ 98 or 03, not 11, with the minimum subset of STL, there is no boost and not always on x86 - But I can use memcpy and placement new, as I said.

provided that the size of typeT and typeU And the alignment requirements are the same, you can not use the allocated transmission for the typeT to hold the value of uninitialized typeU .

If typeT and / or typeU are constructors or destructors, you will need to make sure that they are called properly and in correct order. Static_assert (typeof) == size (typeU) static_assert (alignof (typeT) == alignof (typeU) ty PET T [10]; & amp;; [0] - & Gt; ~ typeT (); // Delete the placement to type t [0] Type * u = p = & amp; t [0]; New (P) type u (); // composition type u [t] [0] Type UU & amp; = * P; u.doStuff (); p- & gt; ~ typeU (); // Placement removed on TYPE [0] new (& amp; t [0]) Type T (); // on resume type T [0]

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -