## nng_msg_realloc

Reallocate a message.

### Synopsis

```c
#include <nng/nng.h>

int nng_msg_realloc(nng_msg *msg, size_t size);
```

### Description

The `nng_msg_realloc` function re-allocates a message so that it has a body of length _size_.
This message attempts to avoid extra allocations, and will reuse the existing memory when possible.

Reallocation is guaranteed to succeeed new length is less than xref:nng_msg_capacity.adoc[`nng_msg_capacity`].

TIP: `nng_msg_realloc` is suitable for creating space for direct writing of data.
When appending many small pieces of data to a message using xref:nng_msg_append.adoc[`nng_msg_append`], allocations may be reduced by first using xref:nng_msg_reserve.adoc[`nng_msg_reserve`] to create sufficient space.

NOTE: Reallocation invalidates any previously obtained pointers to the message body or header.

### Return Values

This function returns 0 on success, and non-zero otherwise.

### Errors

[horizontal]
`NNG_ENOMEM`:: Insufficient free memory exists to reallocate a message.

### See Also

xref:nng_msg_append.adoc[nng_msg_append],
xref:nng_msg_body.adoc[nng_msg_body],
xref:nng_msg_insert.adoc[nng_msg_insert],
xref:nng_msg_len.adoc[nng_msg_len],
xref:nng_msg_reserve.adoc[nng_msg_reserve],
xref:nng_msg_trim.adoc[nng_msg_trim]
