Cross platform coding: data type issues to remember.

1. long data type.
Always remember on Windows long is 4 bytes in size. On Linux it is 8 bytes!

2. Reading unsigned short variable.

void foo(char *shortString)
{
    unsigned short val;
    sscanf(shortString, "%u", &val);
}

This code works properly on Ubuntu. But gives core dump on CentOS / RHEL.
Never read a short variable using “%d” or “%u”, always use “%h” or “%hu”

    // this is the right thing to do.
    sscanf(shortString, "%hu", &val);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s