Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CIS*2450 (W05) - Seminar 2 Commenting Style Pointers and Arrays ISO C99 Preprocessor Directives Handling Unknown Quantities of Data."— Presentation transcript:

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

2 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 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 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 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 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 7 Preprocessor Directives Instructions for the preprocessor to execute before compilation occurs #include #define #if(n)def #warning

8 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 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 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 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 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 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 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 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 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 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 18 The End Questions? Comments? Reminder: A1 due on Jan 28 That’s just a week away!


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

Similar presentations


Ads by Google