Chapter 13 Programming in the Large 20070110 chap

  • Slides: 31
Download presentation
Chapter 13 Programming in the Large 20070110 chap 13

Chapter 13 Programming in the Large 20070110 chap 13

Objectives n n Difficulties of Developing Large Software Systems Reduce the complexity of development

Objectives n n Difficulties of Developing Large Software Systems Reduce the complexity of development and maintenance. n n Flexible Macros Storage classes of variables and functions Library of reusable code Additional preprocessor directives 20070110 chap 13 2

Using Abstraction to Manage Complexity (1/2) n n Procedural Abstraction: separation of what a

Using Abstraction to Manage Complexity (1/2) n n Procedural Abstraction: separation of what 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). 20070110 chap 13 3

Using Abstraction to Manage Complexity (2/2) n n n Both enable the designer to

Using Abstraction to Manage Complexity (2/2) n n n Both enable the designer to make implementation decision in a piecemeal fashion Information Hiding: protecting the implementation details of a low-level module from direct access by a high-level module. Reusable code n 20070110 Encapsulate a data object and its operators in a personal library. #include chap 13 4

Personal Libraries: Header Files n n Copying the code of the functions into other

Personal Libraries: Header Files n n 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. 20070110 chap 13 5

20070110 chap 13 6

20070110 chap 13 6

Header files n n To create a personal library, we must first make a

Header files n n To create a personal library, we must first make a header file Header file: text file containing the interface information about a library needed n n 20070110 by a complier to translate a program system that used the library or by a person to understand use the library. chap 13 7

Typical Contents of a Header File n n A block comment summarizing the library’s

Typical Contents of a Header File n n 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. 20070110 chap 13 8

Case Study: planet. h 20070110 chap 13 9

Case Study: planet. h 20070110 chap 13 9

20070110 chap 13 10

20070110 chap 13 10

Notes for Header File Design n The common boundary between two separate parts of

Notes for Header File Design n 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. 20070110 chap 13 11

Personal Libraries: Implementation Files n n Implementation File: file containing the C source code

Personal Libraries: Implementation Files n n 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: n n n 20070110 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. chap 13 12

Implementation File planet. c 20070110 chap 13 13

Implementation File planet. c 20070110 chap 13 13

Use Functions from a Personal Library Angular brackets <stdio. h>: indicate to the preprocessor

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. n Quotes “planet. h”: a library belonging to the programmer. 14 20070110 chap 13 n

Storage Classes n C has five storage classes n n n 20070110 auto extern

Storage Classes n C has five storage classes n n n 20070110 auto extern global variable static register chap 13 15

Auto and Extern Variables n auto: default storage class of function parameters and local

Auto and Extern Variables n auto: default storage class of function parameters and local variables n n 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. n the name of the functions themselves 20070110 chap 13 16

Global Variables A variable that may be accessed by many functions in a program.

Global Variables A variable that may be accessed by many functions in a program. /* eg 1. c */ /* eg 2. c */ int global_var_x; extern int global_var_x; void afun(int n) bfun(int n) … … n n Only the defining declaration, the one in eg 1. c, allocates spaces for global_var_x. A declaration beginning with the keyword extern allocated no memory; it simply provides information for the computer. 17 20070110 chap 13

20070110 chap 13 18

20070110 chap 13 18

Static Variables n static: storage class of variables allocated only once, prior to program

Static Variables n static: storage class of variables allocated only once, prior to program execution. int fun_frag(int n) { static int once = 0; int many = 0; } n n 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. 20070110 chap 13 19

Register variables n n n Storage class of automatic variable that the programmer would

Register variables n n n 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. 20070110 chap 13 20

Modifying Function for Inclusion in a Library n Error: return an error code or

Modifying Function for Inclusion in a Library n Error: return an error code or display an error message. 20070110 chap 13 21

Conditional Compilation n C allows the user to select parts of a program to

Conditional Compilation n 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. 20070110 chap 13 22

n #define TRACE 20070110 chap 13 23

n #define TRACE 20070110 chap 13 23

20070110 chap 13 24

20070110 chap 13 24

Conditional Compilation n Example 2 Library sp_one (uses library sp) Library sp_two (uses library

Conditional Compilation n 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. n Example 3: Design a system for use on a variety of computers n Allows one to compile only the code appropriate for the current computer. 20070110 chap 13 25

20070110 chap 13 26

20070110 chap 13 26

Arguments to Function main int main(int argc, /*input - argument count */ char *argv[])/*input

Arguments to Function main int main(int argc, /*input - argument count */ char *argv[])/*input - argument vector*/ n n Command line arguments: options specified in the statement that activates a program. Example: prog opt 1 opt 2 opt 3 argc 4 argv[0] prog argv[1] opt 1 argv[2] opt 2 argv[3] opt 3 20070110 chap 13 27

backup old. txt new. txt 20070110 chap 13 See Fig 13. 12 28

backup old. txt new. txt 20070110 chap 13 See Fig 13. 12 28

Defining Macros with Parameters 20070110 chap 13 29

Defining Macros with Parameters 20070110 chap 13 29

Importance of Parentheses in Macro Body 20070110 chap 13 30

Importance of Parentheses in Macro Body 20070110 chap 13 30

20070110 chap 13 31

20070110 chap 13 31