Twifty
Twifty

Reputation: 3378

Size of implementation specific std::mbstate_t

The docs on this are rather lacking so I'm hoping the community can run a simple test and post results here so that I, and anybody else, has a reference.

#include <cwchar>
sizeof( std::mbstate_t );

If you could post the results here and also mention which compiler you are using, I would be very grateful.

On VS2010 it's declared as typedef int mbstate_t; and it's size is 4 bytes for both 32 and 64 bit builds.

I'm asking this because mbstate_t is a member of streampos. I need to use this member to store the conversion state of an encoding. The minimum space I can get away with is 3 bytes so I need to know if any implementation is going to break my code.

Thanks in advance.

Upvotes: 0

Views: 372

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409176

From the C11 specification (7.29.1/2):

   mbstate_t

which is a complete object type other than an array type that can hold the conversion state information necessary to convert between sequences of multibyte characters and wide characters;

So while I was wrong in that is can be an array, it could be anything else (including a structure containing an array). The language in the specification doesn't say anything about how it should be implemented, just that it's "a complete object type other than an array type".


From the C++11 specification (multiple places, for example 21.2.3.1/4):

The type mbstate_t is defined in <cwchar> and can represent any of the conversion states that can occur in an implementation-defined set of supported multibyte character encoding rules.


In conclusion, you can not rely on mbstate_t being an integer type, or of a specific size, if you want to be portable. If you want to be portable, you have to let the standard library manage the state for you.

Upvotes: 0

Mateusz Andrzejewski
Mateusz Andrzejewski

Reputation: 842

You just want know the results of the sizeof?

Qt 5.1 with GCC x86 32bit under Debian:

size = 8

Upvotes: 1

neuro
neuro

Reputation: 15180

gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 on x86_64

size = 8

gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 on armv7l

size = 8

Upvotes: 1

Related Questions