Oops!!! wchar_t in Android is 4 bytes!!

2,727 views
Skip to first unread message

Eric Wang

unread,
Oct 16, 2009, 4:49:17 AM10/16/09
to andro...@googlegroups.com
It made me confused for hours!!!
Why the wchar_t is 4 bytes? not unsigned short as traditional? How can I port my c++ codes with wchar stuff to android platform?
Frustrated...

Stuart Adamson

unread,
Oct 16, 2009, 8:36:30 AM10/16/09
to andro...@googlegroups.com
wchar_t is 4 bytes on many platforms (android, iphone etc) and 2 bytes
on others (symbian, windows mobile etc)



On 10/16/09, Eric Wang <eric....@gmail.com> wrote:
> It made me confused for hours!!!Why the wchar_t is 4 bytes? not unsigned

Sean Hodges

unread,
Oct 16, 2009, 8:46:01 AM10/16/09
to andro...@googlegroups.com

Hey Eric,

It does indeed seem strange. From the wchar.h source code in Bionic:

* IMPORTANT: Any code that relies on wide character support is essentially
* non-portable and/or broken. the only reason this header exist
* is because I'm really a nice guy. However, I'm not nice enough
* to provide you with a real implementation. instead wchar_t == char
* and all wc functions are stubs to their "normal" equivalent...
*/

Basically, there is no support for wchar in Android's libc. What you
get is a char, the stub header only exists to keep stdlib.h happy
(even though it is not used). I think that depending on wchar will be
a problem for you when porting, as hinted by the Unicode standard
spec:

"The width of wchar_t is compiler-specific and can be as small as 8
bits. Consequently, programs that need to be portable across any C or
C++ compiler should not use wchar_t for storing Unicode text. The
wchar_t type is intended for storing compiler-defined wide characters,
which may be Unicode characters in some compilers."

I suggest you convert your wchar references to char. If you need
unicode support, then you'll need an alternative solution to the wchar
datatype when building for Android. It's not ideal, but I guess it's
the nature of these things.

Eric Wang

unread,
Oct 16, 2009, 9:18:50 AM10/16/09
to andro...@googlegroups.com
Thanks Sean and Stuart,

I have little experience with Linux programming, didn't know wchar_t is 4 bytes(I tested in RedHat & Ubuntu, both 4 bytes).
As Stuart said, on Symbian & Windows, it is defined to unsigned short.

By now, I fixed the problem by replace all wchar_t with a customized type such as uint16(unsigned short),
and removed all refers to wchar.h in android, which works for me.

2009/10/16 Sean Hodges <seanho...@googlemail.com>

David Turner

unread,
Oct 16, 2009, 2:58:40 PM10/16/09
to andro...@googlegroups.com
Because wchar_t is broken, you should really really not use it. Really, anything is better than wchar_t these days.

allstars

unread,
Oct 18, 2009, 10:47:00 PM10/18/09
to android-ndk
can we add -fshort-wchar in gcc args ??

On Oct 17, 2:58 am, David Turner <di...@android.com> wrote:
> Because wchar_t is broken, you should really really not use it. Really,
> anything is better than wchar_t these days.
>
> On Fri, Oct 16, 2009 at 1:49 AM, Eric Wang <eric.wan...@gmail.com> wrote:
> > It made me confused for hours!!!Why the wchar_t is 4 bytes? not unsigned

David Turner

unread,
Oct 19, 2009, 2:40:42 PM10/19/09
to andro...@googlegroups.com
that will only influence your code, not the one in the platform. If it works, it will probably by pure luck.
Reply all
Reply to author
Forward
0 new messages