flappix
flappix

Reputation: 2217

C++ reduce string memory usage

I need to store a lot of short strings with a constant length. I noticed that a string object allocates 8 bytes, even if it holds some chars, which let me run into memory trouble.

Is there a way to tell C++ that it should allocate only n (constant) bytes for the string? Or do I have to use char arrays?

Upvotes: 0

Views: 2815

Answers (3)

A pointer and count is 8 bytes on a 32-bit system anyway. It's hard to make a string be smaller than that. A string with SSO will likely be larger than 8 bytes. (Perhaps 4 bytes - 1 bit for the count. 1 bit to indicate small string or not. 12 bytes of small string buffer or 4 bytes pointer + 4 bytes capacity => A total of 16 bytes.)

If they are constant length (and given you probably don't need most of std::string's functionality), you probably need to write your own text-id class. (Probably with conversions to and from std::string.)

Possibly a suitable guide: https://akrzemi1.wordpress.com/2015/05/14/handling-short-codes-part-i/

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57678

Since the strings are constant size, you may want allocate a 2 dimensional array (each row is a string). Allocate the array once, at initialization. This is the most compact form.

If the quantity of strings is unknown, consider using a std::vector of arrays of character. I recommend reserving a large size when the vector is created, to reduce the number or reallocations.

Also, ask yourself if the strings need to be stored in memory for the duration of the program. Will you be accessing (searching) them? Can the data be placed into a file or database?

Upvotes: 1

NEOatNHNG
NEOatNHNG

Reputation: 934

std::string does have the constructor that takes a char* and a size but some std::string implementations do not use small string optimisation so they always contain a pointer to the actual data which takes some space. If you use GCC it uses copy on write to save space which also requires a pointer. If there are many duplicates of the strings you could maybe save space by using a map or a set of strings to eliminate duplicates. Or you could just use a char array which is the most basic form with the lowest overhead but also less comfort.

Upvotes: 0

Related Questions