Skip to content

arena_memdup does not accept const pointers #13

@Pecora0

Description

@Pecora0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions