Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++11
Tip/Trick

C++: Size Matters in Platform Compatibility

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Aug 2019CPOL2 min read 4.6K   4   2
Data width must stay unchanged for cross-platform interoperability

Introduction

For file storage and data communication to work interoperably, the width of datatype must stay invariant across platforms. This tip discusses the pitfalls of platform-dependent data width and their solution. Endianess, deserving a tip of its own, is not covered here.

time_t

time_t stores the number of seconds since 1st January, 1970. It is a 32-bit integer, on 32-bit Linux, where it can run up to year 2038, a Y2K equivalent crisis for Linux and otherwise, it is 64-bit on 64-bit Linux. Whereas on modern Visual C++, time_t is 64-bit, no matter the x86 or x64 platform. time_t is not guaranteed to be interoperable between platforms, so it is best to store time as text and convert to time_t accordingly.

wchar_t

wchar_t type to hold the Unicode character is UTF-16 on Windows while UTF-32 on Linux/MacOS, therefore incompatible with each other. UTF-16 character can be 2 bytes or 4 bytes depending on its codepage while UTF-32 character is always 4 bytes which is a colossal waste of memory since most Unicode characters can be expressed in 2 bytes. UTF-8 is 1 byte for ASCII and multibyte for Unicode. For interoperability between Windows and other OSes, the solution is to store the text in UTF-8 and convert to wchar_t upon loading. Another solution is to use fixed-width character types such as char16_t or char32_t introduced in C++11.

Integer Types

size_t and its signed counterpart type, ptrdiff_t whose width varies on x86 or x64 platform, should always be avoided in storage and communication packet. Undetermined width type like long type should be avoided as well. Use the fixed width integer types introduced in C++11, such as uint32_t and int32_t.

Pointer Types

Pointer width varies according to x86 or x64 mode. Pointer sometimes are used as a opaque index/identity. Window SDK's DWORD_PTR is one such example. Pointer derived identity can be temporarily stored in database, file storage or network packets due to distinctness of memory address. It poses a problem where a 64-bit value is sliced off in a 32-bit, say database column type, when the code is recompiled in x64 mode from the original x86 mode. If it has to be done, then use the largest pointer width as the data width. If not, it is best to derive your identity through other means like GUID or truly random number generation.

History

  • 15th August, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
QuestionA few exceptions Pin
Member 1330167916-Aug-19 1:07
Member 1330167916-Aug-19 1:07 
NewsRe: A few exceptions Pin
Chad3F23-Aug-19 11:56
Chad3F23-Aug-19 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.