Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant string arrays

Is it possible to have a (fixed) array which stores its elements in the read-only segment of the executable and not on the stack? I came up with this code but unfortunately it is very unflexible when it comes to adding, moving or deleting items. How do I verify that the strings are indeed stored in the read-only segment? I tried readelf -a file but it doesn't list the strings.

typedef struct {
        int len;
        int pos[100];
        char data[500];
} FixedStringArray;

const FixedStringArray items = {
        4,
        { 9, 14, 19, 24 },
        "LongWord1Word2Word3Word4"
} ;

char* GetItem(FixedStringArray *array, int idx, int *len) {
        if (idx >= array->len) {
                /* Out of range */
                *len = -1;
                return NULL;
        }

        if (idx > 0) {
                *len = array->pos[idx] - array->pos[idx - 1];
                return & array->data[array->pos[idx - 1]];
        }

        *len = array->pos[idx];
        return & array->data[0];
}

void PrintItem(FixedStringArray array, int idx) {
        int len;
        char *c;
        int i = 0;

        c = GetItem(&array, idx, &len);

        if (len == -1) return;

        while (i < len) {
                printf("%c", *c);
                *c++;
                i++;
        }
}

I am considering a script that automatically generates a struct for each array and uses the correct length for pos and data. Are there any concerns in terms of memory usage? Or would it be better to create one struct (like above) to fit all strings?

like image 792
user206268 Avatar asked Nov 29 '09 15:11

user206268


People also ask

What are constant arrays?

In Excel, an array constant is a way to write an array of literal data in your Excel formulas. Think of it this way, a range of A1:A3 is actually an array of data.

How do you declare a constant array?

Use a const assertion to declare a const array in TypeScript, e.g. const arr = [10, 5] as const . Const assertions enable us to set the elements of an array to readonly , indicating to the language that the type in the expression will not be widened (e.g. from [1, 2] to number[] ).

Can array be constant in Golang?

The quick answer is that Go does not support constant arrays, maps or slices.

What is array of string in C?

In C programming String is a 1-D array of characters and is defined as an array of characters. But an array of strings in C is a two-dimensional array of character types. Each String is terminated with a null character (\0).


1 Answers

I'm not sure I understand your question, but do you mean:

const char * const array[] = { "LongWord1", "Word2", "Word3", "Word4" };

This declares a constant array of pointers to constant characters.

OK, to avoid strlen, how about:

struct Str {
    size_t len;
    char *str;
};
#define STR(s) { sizeof(#s) - 1, #s }
const struct Str[] = { STR(LongWord1), STR(Word2), STR(Word3), STR(Word4) };
like image 112
Richard Pennington Avatar answered Sep 30 '22 19:09

Richard Pennington