arena icon indicating copy to clipboard operation
arena copied to clipboard

arena_memdup does not accept const pointers

Open Pecora0 opened this issue 5 months ago • 0 comments

The signature of arena_memdup is

void *arena_memdup(Arena *a, void *data, size_t size);

but I think it should be

void *arena_memdup(Arena *a, const void *data, size_t size);

Is there any reason data should not be a const pointer? (I am asking genuinely)

Here is an example where arena_memdup causes some friction:

#define ARENA_IMPLEMENTATION
#include "arena.h"

#define FOO "Hello world"

typedef struct {
    const char *str;
    size_t count;
} String_View;

Arena a = {0};

int main() {
    String_View foo = {
        .str = FOO,
        .count = 5,
    };
    // note that foo does not point to a zero terminated string so we can not use arena_strdup to duplicate it

    String_View bar;
    bar.count = foo.count;
    bar.str = arena_memdup(&a, foo.str, foo.count);

    printf("%.*s\n", (int) bar.count, bar.str);
}

This code throws a warning when compiled with gcc. Using const would fix that.

Pecora0 avatar Jul 26 '25 19:07 Pecora0