comp 2103 programming 3 part 4
play

COMP 2103 Programming 3 Part 4 Jim Diamond CAR 409 Jodrey School - PowerPoint PPT Presentation

COMP 2103 Programming 3 Part 4 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University 171 Modules: Introduction Modules: a technique for organizing your functions Idea 1: if you have a large program, you


  1. COMP 2103 — Programming 3 — Part 4 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

  2. 171 Modules: Introduction • Modules: a technique for organizing your functions – Idea 1: if you have a large program, you don’t want all your functions in • one file imagine 1,000,000 lines of code in one file – changing one line means you would have to re-compile everything – editing the file might become unwieldy – – re-using code is difficult Idea 2: suppose you develop your own library of (related) functions, • – – you might want people to be able to use your library functions without giving them your source code Jim Diamond, Jodrey School of Computer Science, Acadia University

  3. 172 Modules: Organization • A module consists of two files: – a “ .c ” file, which contains the implementation of the module functions, and a “ .h ” file, which contains declarations of the “public” module – functions, and other things (see next slide) – The programmer creating the module prepares these two files • (e.g., some_package.c and some_package.h ) The programmer using the module uses • #include "some_package.h" in any of his source files which need declarations from that module The programmer using the module includes the implementation file † in • his/her gcc line: $ gcc -Wall ... myprog.c some_package.c -o myprog Jim Diamond, Jodrey School of Computer Science, Acadia University

  4. 173 Modules: Content of The “ ✳❤ ” File The .h file contains all interface information which should be exposed • to the module user – the documentation for the user any #define constants or macros needed by the user – any data types created with typedef or struct needed by the user – – any function declarations that the user should see – there may be functions in the implementation of the module which are not exposed to the user (“private” functions) Example: math.h • – # define M_PI 3.14159265358979323846 /* pi */ – has macros; e.g., # define isless(x, y) __builtin_isless(x, y) – provides function prototypes (for sin() , cos() , . . . ) Some .h files also define typedef s and/or struct s • Jim Diamond, Jodrey School of Computer Science, Acadia University

  5. 174 Modules: Structure of The “ ✳❤ ” File Suppose your module is named myfuncs • the myfuncs.h file MUST (by convention, but still MUST ) look – like this #ifndef MYFUNCS_H #define MYFUNCS_H ... all the macros, function prototypes, ... #endif This #ifndef ... #define ... #endif construct tells the compiler to • skip the contents of the file on second (and third, fourth, . . . ) readings • Why do we care? – because in some (simple or complex) situations you might end up #include ing a .h file twice; • So what? – the compiler doesn’t allow you to do some things twice (like #define ing the same token twice) – and just because you have a fast computer doesn’t mean you should waste CPU time C++ (bah!) Jim Diamond, Jodrey School of Computer Science, Acadia University

  6. 175 Modules: The “ ✳❝ ” File First, the .c file #include s the corresponding .h file • – this ensures that the declarations (of externally-visible functions) are in agreement with the function definitions – (if they disagree the compiler will bleed over you when you try to compile the .c file!) this also provides the .c file with any constant or macro definitions – found in the .h file Following the #include is the rest of the implementation of the module • functions (including the documentation for the implementation) For example (docs are missing and code is compressed to fit on this slide!) : • #include "myfuncs.h" #include <math.h> ONLY if *THIS* .h file needs math.h int isquare(int i) { return i * i; } float fsquare(float f) { return f * f; } ... Jim Diamond, Jodrey School of Computer Science, Acadia University

  7. 176 The “ st❛t✐❝ ” Keyword: 1 The keyword “ static ” has a number of (somewhat) dissimilar • meanings in C A variable declared inside a function to be static , e.g., • static int abc; is not stored on the stack; rather it is stored in another area of memory so that its value is preserved between calls to that function • Here is a function that returns how many times it has been called: int times_called(void) { static int counter = 0; return ++counter; } Jim Diamond, Jodrey School of Computer Science, Acadia University

  8. 177 The “ st❛t✐❝ ” Keyword: 2 A function declared to be static , e.g., • static int icube(int i) { ... } is visible only to other functions in the same source file If you use helper functions in a module (that you don’t want the • module user to access) declare them static – – these static functions must not be declared in the .h file There is one other use of static ; we will discuss this later • Jim Diamond, Jodrey School of Computer Science, Acadia University

  9. 178 Modules: Separate Compilation • There are three ways to use module functions in your program (in all of these examples, a9p5.c has “ #include myfuncs.h ”) 1: compile the module .c file with your file: • gcc -Wall -Wextra -std=gnu11 a9p5.c myfuncs.c -o a9p5 2a: pre-compile the module file: • gcc -Wall -Wextra -std=gnu11 -c myfuncs.c → this creates a “ .o ” file ( myfuncs.o in this case) 2b: then use this pre-compiled file: gcc -Wall -Wextra -std=gnu11 a9p5.c myfuncs.o -o a9p5 3: create a “library” (“ .a ” or “ .so ”) file and name the library file when • making the program – Note: the second and third methods allow a module writer to share his • functions without sharing his source code Jim Diamond, Jodrey School of Computer Science, Acadia University

  10. 179 Modules: Various Categories • The previous slides show how to create a package module – One type of package module is known as a type abstraction module • – this defines a new data type (e.g., a stack) and the operations for that data type (e.g., push() , pop() , . . . ) You can also create a layer module • – • You can also create a module which – – replaces (over-rides) functions from another module – read about this in “C for Java Programmers” Jim Diamond, Jodrey School of Computer Science, Acadia University

  11. 180 Modules: Sample Layer Module • Suppose you only have trig functions for radians, but want to use degrees; you could create a “trig in degrees” module: Your trig_degrees.h file would have • #ifndef TRIG_DEGREES_H #define TRIG_DEGREES_H ... user documentation for sin_degrees() ... double sin_degrees(double x); ... #endif Your trig_degrees.c file would have • #include "trig_degrees.h" #include <math.h> ... programmer documentation for sin_degrees() ... double sin_degrees(double x) { return sin(x * M_PI / 180.); } ... Jim Diamond, Jodrey School of Computer Science, Acadia University

  12. 181 Modules: Yet Another Categorization • Suppose you write a module to implement the stack data structure; you might write it in one of these two ways: – any program using this module can use at most one stack, or – any program using this most can use many stacks • The former type of module is sometimes referred to as a singleton module The latter type of module is sometimes referred to as a reentrant • module Typically, a module will be singleton if it uses static variables to store • state (information) between calls to the module’s functions – if it doesn’t use static variables to store state between calls, it will (almost certainly) be reentrant Jim Diamond, Jodrey School of Computer Science, Acadia University

  13. 182 Modules: Sharing Variables • You can define and declare variables outside of functions – these are called global variables – they can be used to share information among different functions – these functions can be in the same file (e.g., a module) – these functions can also be in different files (e.g., a module and your main program) In the other files, you declare the global variable as follows: • extern int some_variable; • You can restrict the usage of a global variable to one source file by defining it to be static static double running_total; – • In both cases, the “scope” of the variable in its file begins at its declaration; it is not known to the code above its declaration Jim Diamond, Jodrey School of Computer Science, Acadia University

  14. 183 Global Variables: Use with Caution • Note: the use of global variables to share information between functions in different source files should generally be avoided – indeed, maybe I should say almost always be avoided The use of global variables can decrease understandability and • maintainability of programs – if a global variable is modified in multiple places, understanding the behaviour of a program can become difficult – in particular, understanding the interfaces between functions gets more complex with global variables Inside a module, a static global variable is more acceptable • – On some computer architectures global variables are slower to access • than automatic (local) variables GEQ for COMP 2213: Why? Jim Diamond, Jodrey School of Computer Science, Acadia University

Recommend


More recommend