[Qt-interest] QString and UNICODE: size determination

Constantin Makshin dinosaur-rus at users.sourceforge.net
Thu Jul 9 16:45:03 CEST 2009


QChar class has only 1 data member -- an unsigned integer containing the  
character itself. It doesn't have any virtual functions. And since  
currently it represents an UTF-16 character, there's no reason why its  
size can be different. If sizeof(QChar) != 2 (for the current  
implementation), QString objects would have useless overhead and  
optimizations like using memcpy() to copy string data wouldn't be possible.

But yes, if you use str.utf16() to retrieve Unicode data, then str.size()  
* 2 is enough.

Also I'd like to notice that the code Bob provided isn't very correct --  
it just won't compile if "UNICODE" macro isn't defined (in that case API  
macros use ANSI version of functions/structures). It's OK if he's  
targeting NT-based systems (it shouldn't be a problem in 2009 :) ), but  
then "#ifdef UNICODE" isn't needed.

As for the topic -- if you use utf16() to retrieve the data, then its size  
is "size() * 2" bytes.

On Thu, 09 Jul 2009 18:02:41 +0400, Girish Ramakrishnan  
<girish at forwardbias.in> wrote:
> Constantin Makshin wrote:
>> QString *always* stores Unicode characters, so the amount of memory used
>> by QString's contents is "str.size() * 2" or, what should be even better
>> (forward compatibility, etc.), "str.size() * sizeof(QChar)".
>
> sizeof(QChar) is the size of QChar object, which is not necessarily 2
> (but it should be on most compilers). I would suggest str.size() * 2
> instead.
>
> Girish
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-- 
Constantin "Dinosaur" Makshin



More information about the Qt-interest-old mailing list