Skip to content

[CLAY_TEXT]: Should Clay_String that is marked as non-statically allocated be copied? #542

@OetkenPurveyorOfCode

Description

@OetkenPurveyorOfCode

I wanted to try Clay for a simple counter example app.

Clay_RenderCommandArray Counter() {
    Clay_BeginLayout();
    char number_string[165] = {0};
    size_t len = snprintf(number_string, 164, "value: %"PRId64, count);
    Clay_String text = {
        .isStaticallyAllocated = false,
        .chars=number_string,
        .length=len
    };
    CLAY_TEXT(text, CLAY_TEXT_CONFIG({
        .fontId = 0,
        .fontSize = 45,
        .textColor = { 255, 255, 255, 255 }
    }));
    return Clay_EndLayout();
}

However, since number_string does not live long enough and is deallocated when it leaves its scope, this app displays a garbage string/no string.

Of course, it is possible for the user to allocate the string memory on the heap or statically like so.

Clay_RenderCommandArray Counter() {
    Clay_BeginLayout();
    static char number_string[165] = {0};
    size_t len = snprintf(number_string, 164, "value: %"PRId64, count);
    Clay_String text = {
        .isStaticallyAllocated = false,
        .chars=number_string,
        .length=len
    };
    CLAY_TEXT(text, CLAY_TEXT_CONFIG({
        .fontId = 0,
        .fontSize = 45,
        .textColor = { 255, 255, 255, 255 }
    }));
    return Clay_EndLayout();
}

Then the app works, obviously.

In my opinion, having the isStaticallyAllocated flag in the string could be used so that both examples work, by copying strings that are marked as not statically allocated into some Clay internal memory. Otherwise it begs the question why that boolean in Clay_String is even there.

So, is this a bug or am I using Clay wrong when I use stack allocated strings?

BTW, I am using the sokol renderer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions