Most memory-efficient way to read & store list of strings in C -


i'd know what's memory efficient way read & store list of strings in c.

each string may have different length, pre-allocating big 2d array wasteful. want avoid separate malloc each string, there may many strings.

the strings read large buffer list data-structure i'm asking about.

is possible store strings separately single allocation of right size?

one idea have store them contiguously in buffer, have char * array pointing different parts in buffer, have '\0's in delimit. i'm hoping there's better way though.

struct list {   char *index[32];   char buf[]; }; 

the data-structure , strings strictly read-only.

here's mildly efficient format, assuming know length of strings in advance:

|| total size |  string 1 | string 2 | ........ | string n | len(string n) | ... | len(string 2) | len(string 1) || 

you can store lengths either in fixed-width integers or in variable-width integers, point can jump end , scan lengths relatively efficiently, , length sum can compute offset of string. know when reached last string when there no remaining space.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -