CIS*2450 Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Advertisements

Introduction to Pointers These Slides NOT From Text.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
1 Portable Programming CS Portability We live in a heterogeneous computing environment  Multiple kinds of HW: IA32, IA64, PowerPC, Sparc, MIPS,
1 Review of Chapter 6: The Fundamental Data Types.
Arrays and Pointers in C Alan L. Cox
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
CSE 425: Target Machine Architecture Target Machine Details Many architectures can be similar in overall structure –E.g., Von Neumann with CISC instruction.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C is a high level language (HLL)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Basic Data Types & Memory & Representation
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
The Machine Model Memory
Data in Memory variables have multiple attributes symbolic name
Chapter 7 - Pointers Outline 7.1 Introduction
A bit of C programming Lecture 3 Uli Raich.
Tokens in C Keywords Identifiers Constants
Pointers in C.
An Introduction to C Programming
CPSC 315 – Programming Studio Spring 2012
Programmazione I a.a. 2017/2018.
Fundamental Data Types
CprE 185: Intro to Problem Solving (using C)
Introduction to the C Language
Introduction to the C Language
Machine-Level Programming 6 Structured Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Lecture 8.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Portability CPSC 315 – Programming Studio
Pointers.
Lectures on Numerical Methods
Outline Defining and using Pointers Operations on pointers
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Lecture 2 SCOPE – Local and Global variables
7. Pointers, Dynamic Memory
Overloading functions
Pointers Pointers point to memory locations
Fundamental Data Types
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
What Actions Do We Have Part 1
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Building Blocks of C Programming Language
C Language B. DHIVYA 17PCA140 II MCA.
Lecture 8.
Pointer Arithmetic By Anand George.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

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.