- Is there some standard library function like this that I don't know about?
As of now, there isn't, but there are numerous libraries and even compiler extensions that provide their own portable implementation for memdup
. Check out publib and Glib as well as a number of exogeneous allocators. The Gnulib also includes it as previously mentioned. These are a set of memory functions (memdup
, memcpy
, malloc
, calloc
, realloc
, strdup
, ...), prefixed with x
. They are meant to also provide out-of-memory checking.
- If not, is there a succinct and preferably standard way to do this without explicit calls to malloc and memcpy?
What is wrong with using memcpy
/ memmove
and malloc
?
Starting from C99, you can declare a Variable-Length Array as the buffer and use a loop to copy each byte individually:
uint8_t membuf[size]; /* Beware of Stack Overflows */
for(int i = 0; i < size; i++)
membuf[i] = mem[i];
Though, a standard memcpy
implementation might perform even better due to heavy optimizations (especially if the -O3 compilation flag has been set). Optimizations include: Loop unrolling, inlining and vectorization.
All in all, I would say it is best if you just copy/paste this code and reuse it forever. Kind of how C is meant to be used in the first place.
void* memdup (const void* mem, size_t size)
{
void* out = malloc(size);
if(out == NULL)
return NULL;
memcpy(out, mem, size);
return out;
}
- C does not include
strdup
- it is not a standard C function. It is a POSIX function that just happens to be very common. Being so easy to implement, there are probably not enough reasons for memdup
to be standardized. Moreover, there is a use-case ambiguity associated with this function. One should write their own memdup
-like function with different implementation details depending on their specific requirements, such as memdup_hardcpy
and memdup_deepcpy
perhaps.
strdup
just be amalloc
and amemcpy
? – Cuirbouillimalloc()
andmemcpy()
) Seems a bit too trivial to be worth making a function for.strdup()
is a bit more complicated if you want to avoid too many passes over the string. – Sulfonmethanestrdup()
have you seen that doesn't require exactly 2 passes over the input string? (once to size the buffer, once to copy) – Haighstruct myStruct *bar = (struct myStruct *)malloc(sizeof(struct myStruct)); memcpy(bar, foo, sizeof(struct myStruct));
to just a single, relatively short line where it's obvious what's going on. – Submarinermalloc
. Aside from that, C doesn't includestrdup
. POSIX does, though, so it's widespread. But you can't rely on it being available. Andstruct myStruct *bar = malloc(sizeof *bar); *bar = *foo;
doesn't look too bad, does it? – Cirostrdup
in C. It is not a standard function. It is provided by your implementation on its own accord. If your implementation decided to proividestrdup
, there's a chance it providesmemdup
as well. If it does not, you can always implement it yourself. – Noblesmemdup()
- all names beginning withmem
are reserved for future use by the Standard Library. – Azerbaijan