1 / 31

Chapter 13

Chapter 13. Programming in the Large. 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.

nevaf
Télécharger la présentation

Chapter 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13 Programming in the Large

  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. Using Abstraction to Manage Complexity (1/2) • Procedural Abstraction: separation of whata 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. Using Abstraction to Manage Complexity (2/2) • 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

  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. Header files • 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.

  7. 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.

  8. Case Study: planet.h

  9. 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.

  10. 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.

  11. Implementation File planet.c

  12. 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.

  13. Storage Classes • C has five storage classes • auto • extern • global variable • static • register

  14. 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

  15. 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) …

  16. Static Variables • static: storage class of variables allocated only once, prior to program execution. int fun_frag(int n) { staticint 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.

  17. 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.

  18. Modifying Function for Inclusion in a Library • Error: return an error code or display an error message.

  19. 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.

  20. #define TRACE

  21. 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.

  22. 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

  23. See Fig 13.12 backup old.txt new.txt

  24. Defining Macros with Parameters

  25. Importance of Parentheses in Macro Body

More Related