Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Dipti Rana
Software implementation and maintenance is difficult Clean conceptual framework allows for more efficient and more correct code
Efficient
Efficient
data structures
algorithms
Example
A collection of 3,000 texts with avg. of 20 lines each, with avg. 10 words / line
600,000 words
Find all occurrences of the word happy Suppose it takes 1 sec. to check a word for correct matching What to do?
Example (contd)
What to do?
Sol. 1 Sequential matching: 1 sec. x 600,000 words = 166 hours Sol. 2 Binary searching: - order the words - search only half at a time Ex. Search 25 in 5 8 12 15 15 17 23 25 27 25 ? 15 15 17 23 25 27 25 ? 23 23 25 27 25 ? 25 How many steps?
Set
Stack
Tree
Data Structure
Primitive
Data structures that are directly operated upon by machine level instructions
int, real, character, pointer, logical data item (true/false)
Non Primitive
No direct deletion or insertion operation are performed
Arrays [using predefined data types], lists and files
Data Structure
Linear
A list which displays the relationship of adjacency between elements is said to be linear
Stack, queue, link list
Non Linear
Tree, graphs
Data Structure
Sequential vs. Non Sequential Static vs. Dynamic
Topics
Arrays Stacks Queues Link lists Trees Graphs Hashing / Dictionaries Sorting
Textbook
Introduction to Data Structures with Applications
Tremblay and Sorenson
C compiler
Array
Arrays
Array: a set of pairs (index and value) data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.
Arrays in C
int list[5], *plist[5]; list[5]: five integers list[0], list[1], list[2], list[3], list[4] *plist[5]: five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] implementation of 1-D array list[0] base address = list[1] + sizeof(int) list[2] + 2*sizeof(int) list[3] + 3*sizeof(int)
Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: ? Difference: ?
Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 (list2 + i) *(list2 + i) -
Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 - a pointer to list2[0] (list2 + i) - a pointer to list2[i] (&list2[i]) *(list2 + i) - list2[i]
Contents 0 1 2 3 4
Contents 0 1 2 3 4
Memory Organization
During run time, variables can be stored in one of three pools
Stack Static heap Dynamic heap
Stack
Maintains memory during function calls
Argument of the function Local variables Call Frame
Stack - Example
int foo( int a, double f ) { int b; }
<call> a f b
Stack - Example
int foo( int a, double f ) { int b; { int c; } }
<call> a f b
Stack - Example
int foo( int a, double f ) { int b; { int c; } }
<call> a f b c
Stack - Example
int foo( int a, double f ) { int b; { int c; } }
<call> a f b c
Stack - Example
int foo( int a, double f ) { int b; { int c; } }
<call> a f b c
Stack errors?
void foo( int depth ) { int a; if( depth > 1 ) foo( depth ); }
Static heap
Memory for global variables
#include <stdio.h> const int ListOfNumbersSize = 1000; int ListOfNumbers[ListOfNumbersSize]; int main() {
Static heap
Variables on the static heap are defined throughout the execution of the program Memory on the static heap must be defined at compile time
Dynamic Heap
Memory that can be allocated and freed by the program during run time The program controls how much is allocated and when Limitations based on run-time situation
Available memory on the computer
Returns a pointer to a new memory block of size Size Returns NULL if it cannot allocate memory of this size
Example: strdup
Function to duplicate a string:
char * strdup( char const *p ) { int n = strlen(p); char* q =(char*)malloc(sizeof(char)*(n+1)); if( q != NULL ) strcpy( q, p ); return q; }
Memory Management
void foo( char const* p ) { char *q = strdup( p ); // do something with q }
<call> p q
Heap
a b \0
De-allocating memory
void free( void *p );
Example of free
void foo( char const* p ) { char *q = strdup( p ); // do something with q free(q); }
Further Knowledge
Read manual page of malloc calloc realloc free