portability issues
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
1
Portability Issues
CIS*2450
Advanced Programming Concepts
2
Reference
• The Practice of Programming by Brian W. Kernighan and Rob Pike,
Addison-Wesley, 1999.
3
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.
4
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
5
Troubles on the Road to Portability
6
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 *));}
7
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
8
Sizes of Data Types
• It is not even required that a pointer value fit into an int!
• The only rules that you can follow aresizeof(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
9
Sizes of Data Types
• Always use sizeof.
Used sizeof
Didn’t use sizeof!
10
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?
11
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));
}
12
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
13
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.
14
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?
15
Order of Evaluation
ptr[count] = name[++count];
printf("%c %c\n",getchar(),getchar());
printf("%f %s\n",log(-1.23),strerror(errno));
16
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;
}
17
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++]; }
18
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());}
19
Order of Evaluation
> Testorder0 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());
20
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());}
21
Unambiguous Code – I
> goodorder0 01 1 for ( i=0; i < 5; i++ ) {
2 2 ptr[count] = name[count];
3 3 count++;
4 4 }aba b printf("%c ",getchar());
printf("%c\n",getchar());
22
Unambiguous Code – IIint 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]);}
23
Unambiguous Code – II
> good2order
0 1
1 2
2 3
3 4
4 5
24
Hints
• Always use sizeof.
• Say exactly what you mean -- make sure that you have not introduced ambiguities because of sloppy coding.
25
Hints
• Study the features and problems of the language that you are working in.
• Never assume that a structure is organized contiguously.
26
Hints
• Do not write code that relies on a certain compiler interpretation for correctness.
• Use the simplest data and control structures that you can.
27
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.