In this I need C++ array class template, which is fixed-size, stack-based and doesn't require default constructor answer I posted a piece of code, that is using placement new with char array. For me, this is something absolutely normal. But according to comments this code is wrong.
Can anyone explain in more detail?
Specifically what can go wrong with the array. What I understand from the comments is that T x[size];
might not fit into char x[size*sizeof(T)];
. I don't believe this is true.
EDIT:
I'm just more and more confused. I know what alignment is in case of structures. Yes, when you have a structure the attributes start on different offsets then you might think.
OK, now we are back to arrays. You are telling me that T x[size];
is the same size as char x[size*sizeof(T)];
, yet I cannot access the char array as T array because there might be some alignment. How can there be alignment when the arrays have the same size?
EDIT 2:
OK I finally get it, it may start on a wrong address.
EDIT 3:
Thx everyone, you can stop posting :-) Phew, this total blew my mind. I just never realized this was possible.
Understanding Private Placement Private placement is an issue of stock either to an individual person or corporate entity, or to a small group of investors. Investors typically involved in private placement issues are either institutional investors, such as banks and pension funds, or high-net-worth individuals.
Benefits Preferential issues: Opportunity to raise more capital than in a private placement. Better equality / treatment of shareholders. In the case of crisis issues, the subscription price is set low, which is to the benefit of potential investors.
Key Takeaways Placement refers to the sale of securities to a group of investors, either on a public or private level. A public offering would typically involve registering with the Securities and Exchange Commission, while a private placement is exempt from registering.
An IPO differs from a private placement because an IPO is a company's introductory sale of shares to the general public whereas a private placement is a company's private offering of shares to institutional and accredited investors.
A T x[size]
array will always fit exactly into size * sizeof(T)
bytes, meaning that char buffer[size*sizeof(T)]
is always precisely enough to store such an array.
The problem in that answer, as I understood it, was that your char
array is not guaranteed to be properly aligned for storing the object of type T
. Only malloc
-ed/new
-ed buffers are guaranteed to be aligned properly to store any standard data type of smaller or equal size (or data type composed of standard data types), but if you just explicitly declare a char
array (as a local object or member subobject), there's no such guarantee.
Alignment means that on some platform it might be strictly (or not so strictly) required to allocate, say, all int
objects on, say, a 4-byte boundary. E.g. you can place an int
object at the address 0x1000
or 0x1004
, but you cannot place an int
object at the address0x1001
. Or, more precisely, you can, but any attempts to access this memory location as an object of type int
will result in a crash.
When you create an arbitrary char
array, the compiler does not know what you are planning to use it for. It can decide to place that array at the address 0x1001
. For the above reason, a naive attempt to create an int
array in such an unaligned buffer will fail.
The alignment requirements on some platform are strict, meaning that any attempts to work with misaligned data will result in run-time failure. On some other platforms they are less strict: the code will work, but the performance will suffer.
The need for the proper alignment sometimes means that when you want to create an int
array in an arbitrary char
array, you might have to shift the beginning of an int
array forward from the beginning of the char
array. For example, if the char
array resides at 0x1001
, you have no choice but to start your constructed-in-place int
array from the address 0x1004
(which is the char
element with the index 3). In order to accommodate the tail portion of the shifted int
array, the char
array would have to be 3 bytes larger than what the size * sizeof(T)
evaluates to. This is why the original size might not be enough.
Generally, if your char
array is not aligned in any way, you will really need an array of size * sizeof(T) + A - 1
bytes to accommodate an aligned (i.e. possibly shifted) array of objects of type T
that must be aligned at A-byte boundary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With