1 CIS*2450 (W05) - Seminar 2 Commenting Style Pointers and Arrays ISO C99 Preprocessor Directives Handling Unknown Quantities of Data.

Slides:



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

Files in C Rohit Khokher.
Dr. Kalpakis CMSC 661, Principles of Database Systems Representing Data Elements [12]
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Hash-based Indexes CS 186, Spring 2006 Lecture 7 R &G Chapter 11 HASH, x. There is no definition for this word -- nobody knows what hash is. Ambrose Bierce,
Hash-Based Indexes The slides for this text are organized into chapters. This lecture covers Chapter 10. Chapter 1: Introduction to Database Systems Chapter.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Data Structures: A Pseudocode Approach with C
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
File Organizations Sept. 2012Yangjun Chen ACS-3902/31 Outline: File Organization Hardware Description of Disk Devices Buffering of Blocks File Records.
1 Hash-Based Indexes Yanlei Diao UMass Amherst Feb 22, 2006 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 Friday, July 07, 2006 “Vision without action is a daydream, Action without a vision is a nightmare.” - Japanese Proverb.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, and Hashing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 Disk Storage, Basic File Structures, and Hashing.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
Stacks - 2 Nour El-Kadri ITI Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
CS4432: Database Systems II Record Representation 1.
Hashing and Hash-Based Index. Selection Queries Yes! Hashing  static hashing  dynamic hashing B+-tree is perfect, but.... to answer a selection query.
File Storage Organization The majority of space on a device is reserved for the storage of files. When files are created and modified physical blocks are.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
File Structures. 2 Chapter - Objectives Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, and Hashing.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Copyright 2014 – Noah Mendelsohn Code Tuning Noah Mendelsohn Tufts University Web:
THE PREPROCESSOR
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 5 Record Storage and Primary File Organizations
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CS4432: Database Systems II
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
13 C Preprocessor.
Dynamic Allocation in C
CSCI-255 LinkedList.
Chapter 13 - The Preprocessor
C Basics.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Programming
Disk Storage, Basic File Structures, and Hashing
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
What Is? function predefined, programmer-defined
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

1 CIS*2450 (W05) - Seminar 2 Commenting Style Pointers and Arrays ISO C99 Preprocessor Directives Handling Unknown Quantities of Data

2 Administrative Details Newsgroup Coursework forums CIS 2450 Official W05, CIS 2450 Unofficial Office Hours Dr. Gardner: Mon 1-2pm, Tues 9:30-10:30am Reyn 105 Tom: Tues 5:30-6:30pm, Wed 10-11am Alex: Tues TBD, Fri 1-2pm Paul: Wed 2pm-3pm, Thurs 2pm- 3pm POSSIBLY by appointment. TA Office: Thorn 1308

3 Commenting File Filename and description Author contact info (name/ /website) Creation/last modification dates Copyright/ License Function Precondition/postcondition/description Like A1 specifications Line/Block Anything non-obvious

4 Review of Pointers and Arrays A pointer is a variable that holds a location in memory Can hold the address of another variable Eg “int *ptr=&intvar;” Or it can get its own memory Eg int *ptr=(int*)calloc(5,sizeof(int)); An array name is just a pointer to the first element in the array Array indexing is shorthand for the value at n data positions from the base address

5 Pointers and Arrays (2) Memory address dataArray FormPointer Form Pointer Form (expanded) 10001a[0]*(a+0)*(1000+(0*4)) 10041a[1]*(a+1)*(1000+(1*4)) 10082a[2]*(a+2)*(1000+(2*4)) 10123a[3]*(a+3)*(1000+(3*4)) 10165a[4]*(a+4)*(1000+(4*4))

6 ISO 9899:1999 C99 Standard Some Differences between C89 and C99 Variable length arrays Available in GCC as an extension to C89, C++ int k; k= ; int array[k]; Mix declarations and code Use sparingly otherwise debugging complications can occur Line (//) comments Can nest within traditional /* */ comments snprintf family of functions in stdio.h Prevent buffer overflows Flexible array members rec=malloc( sizeof(MARCREC)+ (n*3*sizeof(int)))

7 Preprocessor Directives Instructions for the preprocessor to execute before compilation occurs #include #define #if(n)def #warning

8 #include Inserts the content of one file into another Traditionally used for header files System includes #include Checks all default include directories and -I Local includes #include “myheader.h” Checks current directory

9 #define Have preprocessor do a “search and replace” Three main ways to use Constants #define PI Existence #define DEBUG Add to makefile -D Macros #define SQUARE(X) ((X)*(X)) Some useful ones built into compiler __FILE__, __LINE__

10 #if(n)def Used to control which code gets compiled #if(n)def Allow block if is (not) #define’d Close block with #endif A common example #ifdef DEBUG fprintf(stderr,“x=%d\n”,x); #endif

11 #if(n)def (cont’d) Used in header files to prevent duplicate code being #include’d An example, util.h #ifndef UTIL_H #define UTIL_H //structs,enums,etc #endif

12 #warning Causes a warning message to be displayed at compile time Useful for adding reminders of stuff to fix #warning fix this test.c:7:2: warning: #warning fix this Warning is from test.c, line 7

13 Managing Unknown Quantities of Data Large, fixed-size array Bad. Should never assume maximum size Expandable data structures Resizing arrays Variable length arrays Store raw data then parse it

14 Expandable Data Structures Many types/variations Linked-lists, trees, etc Many don’t allocate memory for unused elements Can use excessive memory for data structure if data elements are small High functional overhead depending on data structure used Data Next Data Next NULL head

15 Resizing Arrays Allocate a fixed-sized array Pick a “reasonable” starting size As elements are added/removed, keep track of quantity If array becomes full, allocate a larger array and transfer array contents Manual method or realloc Pick “reasonable” new size Some wasted memory is possible Not too much functional overhead Like a Vector in Java

16 Variable-Length Arrays New to ISO C99 Implemented by GCC for C89 and C++ Can determine array size at runtime Eg int x=rand()%256; int arrary=int[x]; Once array size is set, it cannot be changed Same job can be done with dynamic memory Dynamic memory requires cleanup

17 Store Raw Data Then Parse It Sometimes the quantity of raw data is known but the amount of information encoded is not. E.g. Record size Vs # of fields in a marc record It may be easier to gather the raw data and parse it from memory than to try and parse it on-the-fly E.g. If the original data can only be accessed sequentially and not randomly Requires some knowledge about the raw data so it’s not completely “unknown” Can be used in conjunction with aforementioned techniques

18 The End Questions? Comments? Reminder: A1 due on Jan 28 That’s just a week away!