portability issues

27
1 Portability Issues CIS*2450 Advanced Programming Concepts

Upload: inez-leblanc

Post on 30-Dec-2015

33 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Portability Issues

1

Portability Issues

CIS*2450

Advanced Programming Concepts

Page 2: Portability Issues

2

Reference

• The Practice of Programming by Brian W. Kernighan and Rob Pike,

Addison-Wesley, 1999.

Page 3: Portability Issues

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.

Page 4: Portability Issues

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

Page 5: Portability Issues

5

Troubles on the Road to Portability

Page 6: Portability Issues

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 *));}

Page 7: Portability Issues

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

Page 8: Portability Issues

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

Page 9: Portability Issues

9

Sizes of Data Types

• Always use sizeof.

Used sizeof

Didn’t use sizeof!

Page 10: Portability Issues

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?

Page 11: Portability Issues

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));

}

Page 12: Portability Issues

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

Page 13: Portability Issues

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.

Page 14: Portability Issues

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?

Page 15: Portability Issues

15

Order of Evaluation

ptr[count] = name[++count];

printf("%c %c\n",getchar(),getchar());

printf("%f %s\n",log(-1.23),strerror(errno));

Page 16: Portability Issues

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;

}

Page 17: Portability Issues

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++]; }

Page 18: Portability Issues

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());}

Page 19: Portability Issues

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());

Page 20: Portability Issues

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());}

Page 21: Portability Issues

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());

Page 22: Portability Issues

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]);}

Page 23: Portability Issues

23

Unambiguous Code – II

> good2order

0 1

1 2

2 3

3 4

4 5

Page 24: Portability Issues

24

Hints

• Always use sizeof.

• Say exactly what you mean -- make sure that you have not introduced ambiguities because of sloppy coding.

Page 25: Portability Issues

25

Hints

• Study the features and problems of the language that you are working in.

• Never assume that a structure is organized contiguously.

Page 26: Portability Issues

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.

Page 27: Portability Issues

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.