Handling short codes (part I)--Andrzej Krzemieński

First part of a discussion about designing a type storing character strings of length N (known at compile-time and sufficiently small).

Handling short codes - part I

by Andrzej Krzemieński

From the article:

In my work, we often deal with codes [...] The initial choice was to represent them in programs by type std::string, a general-purpose type for storing sequences of characters. But, as with any general-purpose tools, they fail to take into account the particular usage cases which often offer room for improvements [...] The following is an attempt at the design of a type that would take advantage of the information that we are only storing character strings of length N, where N is sufficiently small (say, no longer than 8)...

Add a Comment

Comments are closed.

Comments (1)

0 0

Yakk said on May 20, 2015 07:20 AM:

The demonstrated code object == fails on strings that are not a power of 2 in practice. For example, N=3.

The size of the union is the max of 3 and 4 -- so 4 bytes (extra overhead). Only 3 of the bytes are set (by the string constructor), but == compares all 4. The 4th byte is in practice going to be left uninitialized in optimized builds, generating unpredictable results.

This can be fixed with masking. The mask may vary based on endianness.