20070110 chap13 chapter 13 programming in the large

31
20070110 chap 13 Chapter 13 Programming in the Large

Upload: theresa-mccarthy

Post on 12-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13

Chapter 13

Programming in the Large

Page 2: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 2

Objectives Difficulties of Developing Large

Software Systems Reduce the complexity of

development and maintenance. Flexible Macros Storage classes of variables and

functions Library of reusable code Additional preprocessor directives

Page 3: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 3

Using Abstraction to Manage

Complexity (1/2) Procedural Abstraction:

separation of whatwhat a function does from how the function accomplishes its purpose.

Data Abstraction: separation of the logical view of a data object (what is stored) from the physical view (how the information is stored).

Page 4: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 4

Both enable the designer to make implementation decision in a piecemeal fashion Information Hiding

Information Hiding: protecting the implementation details of a low-level module from direct access by a high-level module.

Reusable code Encapsulate a data object and its

operators in a personal library. #include

Using Abstraction to Manage Complexity (2/2)

Page 5: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 5

Personal Libraries: Header Files

Copying the code of the functions into other programs to allow reuse is possible but cumbersome.

C permits source code files to be complied separately and then linked prior to loading and execution.

Page 6: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 6

Page 7: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 7

Header files To create a personal library, we

must first make a header fileheader file. Header file: text file containing

the interface information about a library needed by a complier to translate a

program system that used the library or

by a person to understand and use the library.

Page 8: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 8

Typical Contents of a Header File

A block comment summarizing the library’s purpose

#define directives naming constant macros

Type definitions Block comments stating the

purpose of each library function and declarations of the form.

Page 9: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 9

Case Study: planet.h

Page 10: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 10

Page 11: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 11

Notes for Header File Design The common boundary between two

separate parts of a solution is called the interface.

The header file’s purpose is to define the interface between a library and any program that uses the library.

Cautionary Notes: the constant macro defined (PLANET_STRSIZ) has a long name that begins with the library name. This reduces the conflict likelihood.

Page 12: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 12

Personal Libraries: Implementation Files

Implementation File: file containing the C source code of a library’s functions and any other information needed for compilation of these functions.

The elements of an implementation file: A block comment summarizing the library’s

purpose. #include directives for this library’s header file

and for other libraries used by the functions in this library.

#define directives naming constant macros used only inside this library.

Type definitions used only inside this library. Function definitions including the usual comments.

Page 13: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 13

Implementation File planet.c

Page 14: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 14

Use Functions from a Personal Library

Angular brackets <stdio.h>: indicate to the preprocessor that a header file is to be found in a system directory.

Quotes “planet.h”: a library belonging to the programmer.

Page 15: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 15

Storage Classes C has five storage classes

auto extern global variable static register

Page 16: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 16

Auto and Extern Variables

auto: default storage class of function parameters and local variables Storage is automatically allocated on the

stack at the time of a function call and deallocated when the function returns.

extern: storage class of names known to linker. the name of the functions themselves

Page 17: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 17

Global Variables A variable that may be accessed by many

functions in a program.

Only the defining declaration, the one in eg1.c, allocates spaces for global_var_x. A declaration beginning with the keyword extern allocated no memory; it simply provides information for the computer.

/* eg1.c */int global_var_x;voidafun(int n)…

/* eg2.c */extern int global_var_x;voidbfun(int n)…

Page 18: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 18

Page 19: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 19

Static Variables static: storage class of variables allocated only

once, prior to program execution.intfun_frag(int n){

static int once = 0;int many = 0;

} many is allocated space on the stack each time

fun_frag is called. Every time fun_frag returns, many is deallocated.

Static variable once is allocated and initialized one time, prior to program execution. It remains allocated until the entire program terminates.

Page 20: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 20

Register variables Storage class of automatic variable

that the programmer would like to have stored in registers.

It is closely related to storage class auto and may be applied only to local variables and parameters.

By choosing storage class register, the programmer indicates an expectation that the program would run faster if a register, a special high-speed memory location inside the central processor, could be used for the variable.

Page 21: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 21

Modifying Function for Inclusion in a Library

Error: return an error code or display an error message.

Page 22: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 22

Conditional Compilation C allows the user to select parts of a

program to be complied and parts to be omitted.

This ability can be helpful. Example 1: one can build in debugging printf

calls when writing a function and then include these statements in the complied program only when they are needed.

Page 23: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 23

#define TRACE

Page 24: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 24

Page 25: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 25

Conditional Compilation Example 2

Library sp_one (uses library sp)Library sp_two (uses library sp also)A program uses the facilities of both sp_one &

sp_two. This would lead to inclusion of sp.h twice.

C prohibits such duplicate declarations. Example 3: Design a system for use on a variety of computers

Allows one to compile only the code appropriate for the current computer.

Page 26: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 26

Page 27: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 27

Arguments to Function mainintmain(int argc, /*input - argument count */

char *argv[])/*input - argument vector*/ Command line arguments:

options specified in the statement that activates a program.

Example: prog opt1 opt2 opt3argc 4 argv[0] prog

argv[1] opt1 argv[2] opt2

argv[3] opt3

Page 28: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 28

backup old.txt new.txt See Fig 13.12

Page 29: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 29

Defining Macros with Parameters

Page 30: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 30

Importance of Parentheses in Macro Body

Page 31: 20070110 chap13 Chapter 13 Programming in the Large

20070110 chap13 31