« Set a breakpoint at GNOME warning information | Main | Google Notifier dies on Mac OS X 10.5 (Leopard) »
August 15, 2007
Porting code from GCC to Sun Studio
Some problems you may not know.
1) Avoid using multi-character character constant
Code sample:
#include
int main()
{
const int a = 'ABCD';
printf("%x\n", a);
return 0;
}
You will get different result on Solaris x86 and SPARC if it is compiled with Sun Studio.
It's an endian issue.
On x86, result is "44434241"
On SPARC, result is "41424344"
If it is compiled with gcc, result is "41424344" for both platform.
gcc gives you a warning for this code, but Sun Studio doesn't.
Interesting.
You can use 'A' << 24 | 'B' << 16 | 'C' << 8 | 'D'
2) Avoid doing *x++ = foo(*x);
Code sample:
#include
int main()
{
char x[11] = "ABCDEFGHIJ";
char *ptr = x;
int i;
for (i = 0; i < 9; i++) {
*(ptr++) = *ptr + 1;
}
printf("%s\n", x);
return 0;
}
If it is compiled with gcc, g++, or cc (Sun Studio C compiler), the result is
BCDEFGHIJJ
But if it is compiled with CC (Sun Studio compiler), the result is
CDEFGHIJKJ
All code tested with Sun Studio 11 on OpenSolaris.
Posted by ginn at August 15, 2007 6:09 PM