CIS*2450 Advanced Programming Concepts Portability Issues CIS*2450 Advanced Programming Concepts
Reference The Practice of Programming by Brian W. Kernighan and Rob Pike, Addison-Wesley, 1999.
Portability Issues Ideally you should be able to move your program to a different compiler, processor or operating system. In practice, one strives for code that takes only a few revisions to make it work on another system.
Portability Issues Why worry about portability? successful programs are expected to do more than what was originally planned environments change portable programs are better: better design, better construction and better testing
Troubles on the Road to Portability
Sizes of Data Types In C, data type sizes are not defined. #include < stdio.h > int main ( int argc, char *argv[] ) { printf("char = %d, short = %d, int = %d, long = %d\n", sizeof(char),sizeof(short),sizeof(int),sizeof(long)); printf("float = %d, double = %d, pointer = %d\n", sizeof(float),sizeof(double),sizeof(void *)); }
Sizes of Data Types In C, data type sizes are not defined. > testsize char = 1, short = 2, int = 4, long = 4 float = 4, double = 8, pointer = 4
Sizes of Data Types It is not even required that a pointer value fit into an int! The only rules that you can follow are sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(float) <= sizeof(double) char must have at least 8 bits, short and int at least 16 and long at least 32
Sizes of Data Types Always use sizeof. Used sizeof Didn’t use sizeof!
Alignment of Structure Members The alignment of items within a structure is not defined except that the order of items in the declaration is observed. What will the following program print out?
Alignment of Structure Members int main ( int argc, char *argv[] ) { struct X { char c; int i; }; printf("Sizeof c = %d and sizeof i = %d and sizeof X = %d\n", sizeof(char), sizeof(int), sizeof(struct X)); }
Alignment of Structure Members int main ( int argc, char *argv[] ) { struct X { char c; int i; }; printf(“sizeof c = %d and sizeof i = %d and sizeof X = %d\n", sizeof(char), sizeof(int), sizeof(struct X)); } sizeof c = 1 and sizeof i = 4 and sizeof X = 8
Alignment of Structure Members Never assume that the elements of a structure occupy contiguous memory. Most machines require that n-byte primitive data types be stored at an n-byte boundary. The compiler may force different alignments for performance reasons. Optimization options can affect packing, too.
Order of Evaluation In C, the order of evaluation of operands, side effects, and function arguments is not defined. What is evaluated first in the following statements and does the order matter?
Order of Evaluation ptr[count] = name[++count]; printf("%c %c\n",getchar(),getchar()); printf("%f %s\n",log(-1.23),strerror(errno));
Order of Evaluation int main ( int argc, char *argv[] ) { int i; int count = 0; int count2 = 0; int name[10], ptr[10], ptr2[10]; for ( i=0; i < 10; i++ ) { name[i] = i; ptr[i] = ptr2[i] = 0; }
Order of Evaluation int main ( int argc, char *argv[] ) { int i; int count = 0; int count2 = 0; int name[10], ptr[10], ptr2[10]; for ( i=0; i < 10; i++ ) { name[i] = i; ptr[i] = ptr2[i] = 0; } for ( i=0; i < 5; i++ ) { ptr[count] = name[++count]; ptr2[count2] = name[count2++];
Order of Evaluation int main ( int argc, char *argv[] ) { int i; int count = 0; int count2 = 0; int name[10], ptr[10], ptr2[10]; for ( i=0; i < 10; i++ ) { name[i] = i; ptr[i] = ptr2[i] = 0; } for ( i=0; i < 5; i++ ) { ptr[count] = name[++count]; ptr2[count2] = name[count2++]; for ( i=0; i < 5; i++ ) printf("%d %d %d\n",name[i],ptr[i],ptr2[i]); printf("%c %c\n",getchar(),getchar());
Order of Evaluation > Testorder 0 1 0 ptr[count] = name[++count]; ptr2[count2] = name[count2++]; 1 2 1 2 3 2 3 4 3 4 5 4 ab input b a printf("%c %c\n",getchar(),getchar());
Unambiguous Code – I int main ( int argc, char *argv[] ) { int i, count = 0; int name[10], ptr[10]; for ( i=0; i < 10; i++ ) name[i] = i; for ( i=0; i < 5; i++ ) { ptr[count] = name[count]; count++; } for ( i=0; i < 5; i++ ) printf("%d %d\n",name[i],ptr[i]); printf("%c ",getchar()); printf("%c\n",getchar());
Unambiguous Code – I > goodorder 0 0 1 1 for ( i=0; i < 5; i++ ) { 2 2 ptr[count] = name[count]; 3 3 count++; 4 4 } ab a b printf("%c ",getchar()); printf("%c\n",getchar());
Unambiguous Code – II int main ( int argc, char *argv[] ) { int i, count = 0; int name[10],ptr[10]; for ( i=0; i < 10; i++ ) name[i] = i; for ( i=0; i < 5; i++ ) { ptr[count] = name[count+1]; count++; } for ( i=0; i < 5; i++ ) printf("%d %d\n",name[i],ptr[i]);
Unambiguous Code – II > good2order 0 1 1 2 2 3 3 4 4 5
Hints Always use sizeof. Say exactly what you mean -- make sure that you have not introduced ambiguities because of sloppy coding.
Hints Study the features and problems of the language that you are working in. Never assume that a structure is organized contiguously.
Hints Do not write code that relies on a certain compiler interpretation for correctness. Use the simplest data and control structures that you can.
Hints Listen to all compiler warnings! (-Wall) Study the function libraries that you are using. Make sure that you understand what every function is returning and what it expects as parameters.