memndup

C

Public Domain

Allocates a copy of an arbitrary byte sequence

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdlib.h>
#include <string.h>

/* ``memndup(p, len)'' returns a pointer to a dynamically allocated
 * copy of the `len' bytes at `p' (or NULL on error). */

void* memndup(void* p, size_t len) {
 void* q = malloc(len);
 if (q == NULL) return NULL;
 memcpy(q, p, len);
 return q;
}