Question: What is the best method (performance) to put an `int` into a `char` array?
This is my current code:

```
data[0] = length & 0xff;
data[1] = (length >> 8)  & 0xff;
data[2] = (length >> 16) & 0xff;
data[3] = (length >> 24) & 0xff;

```

`data` is a `char` array (shared ptr) and `length` is the `int`.

Answer: 
Just use `reinterpret_cast`. Use the data array as if it were an int pointer.

```
    char data[sizeof(int)];
    *reinterpret_cast<int*>(data) = length;

```

BTW `memcpy` is much slower than this, because it copies byte by byte using a loop.  

In the case of an integer, this will just be a straightforward assignment.
This is the only way. Theoretically you could use `memcpy` or `reinterpret_cast` but you can't due to problems with [endians](http://en.wikipedia.org/wiki/Endianness).