Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A <code>T x[size]</code> array will always fit exactly into <code>size * sizeof(T)</code> bytes, meaning that <code>char buffer[size*sizeof(T)]</code> is always precisely enough to store such an array. </p> <p>The problem in that answer, as I understood it, was that your <code>char</code> array is not guaranteed to be properly <em>aligned</em> for storing the object of type <code>T</code>. Only <code>malloc</code>-ed/<code>new</code>-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 <code>char</code> array (as a local object or member subobject), there's no such guarantee.</p> <p>Alignment means that on some platform it might be strictly (or not so strictly) required to allocate, say, all <code>int</code> objects on, say, a 4-byte boundary. E.g. you can place an <code>int</code> object at the address <code>0x1000</code> or <code>0x1004</code>, but you cannot place an <code>int</code> object at the address<code>0x1001</code>. Or, more precisely, you can, but any attempts to access this memory location as an object of type <code>int</code> will result in a crash. </p> <p>When you create an arbitrary <code>char</code> array, the compiler does not know what you are planning to use it for. It can decide to place that array at the address <code>0x1001</code>. For the above reason, a naive attempt to create an <code>int</code> array in such an unaligned buffer will fail.</p> <p>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. </p> <p>The need for the proper alignment sometimes means that when you want to create an <code>int</code> array in an arbitrary <code>char</code> array, you might have to <em>shift</em> the beginning of an <code>int</code> array forward from the beginning of the <code>char</code> array. For example, if the <code>char</code> array resides at <code>0x1001</code>, you have no choice but to start your constructed-in-place <code>int</code> array from the address <code>0x1004</code> (which is the <code>char</code> element with the index 3). In order to accommodate the tail portion of the shifted <code>int</code> array, the <code>char</code> array would have to be 3 bytes larger than what the <code>size * sizeof(T)</code> evaluates to. This is why the original size might not be enough.</p> <p>Generally, if your <code>char</code> array is not aligned in any way, you will really need an array of <code>size * sizeof(T) + A - 1</code> bytes to accommodate an aligned (i.e. possibly shifted) array of objects of type <code>T</code> that must be aligned at A-byte boundary.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload