CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Introduction to C Programming
Pointers.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Hashing.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Files in C Rohit Khokher.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
1 CIS*2450 (W05) - Seminar 2 Commenting Style Pointers and Arrays ISO C99 Preprocessor Directives Handling Unknown Quantities of Data.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Data Management and File Organization
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
1 Data Structures. 2 Motivating Quotation “Every program depends on algorithms and data structures, but few programs depend on the invention of brand.
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
CS 261 – Data Structures Introduction to C Programming.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Copyright 2014 – Noah Mendelsohn Code Tuning Noah Mendelsohn Tufts University Web:
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
C++ Functions A bit of review (things we’ve covered so far)
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 13 - The Preprocessor
C Basics.
Introduction to C Programming Language
Circular Buffers, Linked Lists
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
Programming in C Pointers and Arrays.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const

Hash tables (1) Data structures weve seen so far: arrays struct s linked lists

Hash tables (2) Hash tables are a new data structure Like an array indexed with strings e.g. height[Jim] = 6; /* not C code */ Very fast lookup (O(1) i.e. constant time) Flexible: can add/delete elements easily

Hash tables (3) Want to associate a string (key) with a value Generate an integer hash value from the string key different keys should generate different hash values Use hash value as index into an array of linked lists array length is large (128 in lab 7) array values start off as NULL pointers (empty lists) no linked list should ever get larger than a few elements

Hash tables (4)

Hash tables (5) Generating the hash value from the string Many ways to do it We choose a particularly simple (and lame) way Treat the string as an array of chars Treat each char as a small integer ( ) C allows this Sum up the values of all the characters Take the sum mod 128 (the array length) Gives an integer in the range that's our index into the array

Hash tables (6) Three things we can do with a hash table: Look up the value corresponding to a particular key Change the value corresponding to an existing key in the table Add a new key/value pair to the table

Hash tables (7) How to find the value given the key compute hash value to get array index find array location if NULL, not there (return "not found" value) if not NULL, search for key in linked list if found, return node value if not found, not there (return "not found" value)

Hash tables (8) How to change the value corresponding to a given key (or add a new key/value pair): compute hash value to get array index find array location if NULL, add node with key/value pair if not NULL, search for key in linked list if found, change node value if not found, add new node to list (anywhere in list!)

Hash tables (9) Adding nodes to linked list nodes in linked list not in any order so can add to any place in list most people try to add to the end of the list actually easier to add to beginning of list either way, have to set some pointer values to different values

Hash tables (10) Hash table itself is not the array of linked lists It's a struct which contains that array Easy to make mistakes with this Think of it as a box containing the array Why use a struct if all it contains is one array? Practice in handling more complex data structures Real hash tables would have more fields e.g. length of array to permit resizing of the array

Lab 7 Pretty routine application of hash tables One likely problem involving a memory leak May be hard to figure out where to free memory

C preprocessor: #ifdef (1) Sometimes want to conditionally compile code If some condition met, compile this code else do nothing, or do something else Examples: debugging code compiling on different platforms

C preprocessor: #ifdef (2) Debugging code: #define DEBUG int value = 10; #ifdef DEBUG printf(value = %d\n, value); #endif

C preprocessor: #ifdef (3) Can leave out #define and choose at compile time: % gcc -DDEBUG foo.c -o foo -D option means to Define DEBUG This makes the debugging code compile Otherwise it wont compile Usually best to do it this way

C preprocessor: #else Also use #ifdef/#else for portability e.g.: #ifdef WINDOWS #include #else #include #endif

C preprocessor: #ifndef (1) #ifndef includes code if something is not defined assert is defined using #ifndef e.g. assert(i == 0); /* expands to: */ #ifndef NDEBUG if (!(i == 0)) { abort(); } #endif

C preprocessor: #ifndef (2) Recall: to switch off assertions, define NDEBUG : % gcc -DNDEBUG foo.c -o foo Then all assertions are removed from code during compilation Useful after code has been debugged

C preprocessor: #if (1) Can also test integer values with #if / #elif /... : #if REVISION == 1 /* revision 1 code */ #elif REVISION == 2 /* revision 2 code */ #else /* generic code */ #endif

C preprocessor: #if (2) Use #if 0 to comment out large blocks of code: #if 0 /* This doesnt get compiled. */ #endif Useful because cant nest /* */ comments

C preprocessor: include guards (1) Multiple inclusion of header files can cause problems e.g. multiple declarations of struct types Difficult to prevent one include file includes another, etc. Need mechanism to prevent this

C preprocessor: include guards (2) /* header file "foo.h": */ #ifndef FOO_H #define FOO_H /* contents of file */ #endif /* FOO_H */ contents of foo.h only included once

extern (1) Sometimes many files need to share some data e.g. global variable Can only define in one place Put extern declaration in header file Means: this is defined somewhere else

extern (2) /* In header file "foo.h": */ extern int max_value; /* In file "foo.c": */ /* global variable: */ int max_value = ;

const Weve seen this: #define SOME_CONSTANT 100 A better alternative is this: const int SOME_CONSTANT = 100; Why is this better? get type checking on SOME_CONSTANT

Next week Most of C language has been covered Virtual machines (!) More integer types: short, long, unsigned Wrapping up