C Module System C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Data Structure & Abstract Data Type
Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
C Intro.
Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Extensible Array C and Data Structures Baojian Hua
C Module System C and Data Structures Baojian Hua
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Abstract Data Type C and Data Structures Baojian Hua
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Functions C and Data Structures Baojian Hua
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
C and Data Structures Baojian Hua
C Module System C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
Standard ML- Part II Compiler Baojian Hua
CS 201 Functions Debzani Deb.
C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
Guide To UNIX Using Linux Third Edition
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Programming Fundamentals Enumerations and Functions.
Precalculus Section 1.5 Perform basic operations with complex numbers The basic imaginary unit is i. i = i 2 = -1 A complex number is any number that can.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Computer Science 210 Computer Organization
CS1010 Programming Methodology
Computer Science 210 Computer Organization
CS1010 Programming Methodology
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Imaginary Numbers.
Classes and Data Abstraction
Computer Science 210 Computer Organization
C++ Compilation Model C++ is a compiled language
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
(4 – 2) Introduction to Classes in C++
Presentation transcript:

C Module System C and Data Structures Baojian Hua

Software System is Large Practical software systems tend to be large and complex: Linux kernel consists of ~1000K LOC So the general principals in designing large (even small) software systems are: dividing into manageable smaller ones separating specification (interface) from code (implementation)

Module System Different styles of module systems in languages: ML signature and structure Java interface and class C header files (.h) and C files (.c) The next slides explain the details of the C module system via one example: Complex number

Complex Number // Recall the definition of complex number c: // c = x + yi, where x,y \in R, and i=sqrt(-1); // And some typical operations: // complex newComplex (double x, double y); // complex add (complex c1, complex c2); // complex sub (complex c1, complex c2); // complex mult (complex c1, complex c2); // complex distance (complex c1, complex c2); // complex modus (complex c1, complex c2); // complex divide (complex c1, complex c2);

Complex Number: Interface — Types // In file “complex.h”: #ifndef COMPLEX_H #define COMPLEX_H // note that “struct complex” is not given typedef struct complex *complex; complex newComplex (double x, double y); // other function prototypes are similar … #endif

Client Code // With this interface, we can write client code // that manipulate complex numbers. File “main.c”: #include “complex.h” int main () { complex c1, c2, c3; c1 = newComplex (3.0, 4.0); c2 = newComplex (7.0, 6.0); c3 = add (c1, c2); printComplex (c3); return 0; }

Client Code // The client code now compiles: $gcc –c –std=c99 –pedantic –Wall complex.h main.c // which produces main.o, which roughly looks // like: newComplex( … ) add( … ) printComplex( … ) main.o Require an implementation of complex.h

Complex Number: Implementation#1 — Types // In file “complex.c”: #include “complex.h” // We may choose to define complex type as: struct complex { double x; double y; }; // which stores the real and imaginary parts of // a complex number in fields x and y in a // structure.

Complex Number: Implementation#1 — Operations // With this type definition, the “newComplex” // function could be written as: complex newComplex (double x, double y) { complex c = malloc (sizeof(*c)); c->x = x; c->y = y; return c; } // To call function “malloc”, we must include the // header file “stdio.h” #include

Complex Number: Implementation#1 — Operations // function “add ()”: complex add (complex c1, complex c2) { complex c = malloc (sizeof(*c)); c->x = c1->x + c2->x; c->y = c1->y + c2->y; return c; } // Leave other functions as programming // assignments. See the course page.

Linking // Now compiles file “complex.c”: $gcc –c –std=c99 –pedantic –Wall complex.h complex.c // to produce file “complex.o”. newComplex( … ) add( … ) printComplex( … ) newComplex( … ) {} add( … ){} printComplex( … ) {} main.ocomplex.o Linking (simplified) a.out

Complex Number: Implementation#2 — Types // In file “comp2.c”: // Another definition of complex type: #include #include “complex.h” struct complex { double a[2]; }; // which stores the real and imaginary parts of // a complex number in array element a[0] and // a[1] separately.

Complex Number: Implementation#2 — Operations // With this type definition, the “newComplex” // function could be written as: complex newComplex (double x, double y) { complex c = malloc (sizeof(*c)); (c->a)[0] = x; (c->a)[1] = y; return c; }

Complex Number: Implementation#2 — Operations // function “add ()”: complex add (complex c1, complex c2) { complex c = malloc (sizeof(*c)); (c->a)[0] = (c1->a)[0] + (c2->a)[0]; (c->a)[1] = (c1->a)[1] + (c2->a)[1]; return c; } // Leave other functions as programming // assignments. See the course page.

Linking // Now compiles file “complex.c”: $gcc –c –std=c99 –pedantic –Wall complex.h comp2.c // to produce file “comp2.o”. newComplex( … ) add( … ) printComplex( … ) newComplex( … ) {} add( … ){} printComplex( … ) {} main.ocomp2.o Linking (simplified) a.out

Summary The many benefits of modularity: Reading the code: in small, separable pieces Testing the code: test each function separately Speeding up the code: focus only on the slow parts Extending the code: change only the relevant parts Compiling the code: compile each part separately