Download presentation
Presentation is loading. Please wait.
Published byTheresa McCarthy Modified over 9 years ago
1
20070110 chap13 Chapter 13 Programming in the Large
2
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
3
20070110 chap13 3 Using Abstraction to Manage Complexity (1/2) what 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).
4
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)
5
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.
6
20070110 chap13 6
7
7 Header files header file To create a personal library, we must first make a header 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.
8
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.
9
20070110 chap13 9 Case Study: planet.h
10
20070110 chap13 10
11
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.
12
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.
13
20070110 chap13 13 Implementation File planet.c
14
20070110 chap13 14 Use Functions from a Personal Library Angular brackets : 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.
15
20070110 chap13 15 Storage Classes C has five storage classes auto extern global variable static register
16
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
17
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; void afun(int n) … /* eg2.c */ extern int global_var_x; void bfun(int n) …
18
20070110 chap13 18
19
20070110 chap13 19 Static Variables static: storage class of variables allocated only once, prior to program execution. int fun_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.
20
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.
21
20070110 chap13 21 Modifying Function for Inclusion in a Library Error: return an error code or display an error message.
22
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.
23
20070110 chap13 23 #define TRACE
24
20070110 chap13 24
25
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.
26
20070110 chap13 26
27
20070110 chap13 27 Arguments to Function main int main(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 opt3 argc 4 argv[0] prog argv[1] opt1 argv[2] opt2 argv[3] opt3
28
20070110 chap13 28 backup old.txt new.txt See Fig 13.12
29
20070110 chap13 29 Defining Macros with Parameters
30
20070110 chap13 30 Importance of Parentheses in Macro Body
31
20070110 chap13 31
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.