1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22.

Slides:



Advertisements
Similar presentations
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Advertisements

Programming in C Chapter 10 Structures and Unions
Dynamic memory allocation
Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
1/18 Strukture. 2/ Osnove struktura struct point { int x; int y; }; y x0, 0 (4, 3) struct { … } x, y,
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Introduction to Data Structures Systems Programming.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
Introduction to Data Structures Systems Programming Concepts.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
CSCE Systems Programming Lecture 06 - C++/Java  C, Memory CSCE 510 Jan 23, 2013.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Advanced Pointers and Structures Pointers in structures Memory allocation Linked Lists –Stacks and queues Trees –Binary tree example.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 5, Pointers and Arrays
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
12 C Data Structures.
Week 7 Part 2 Kyle Dewey.
Student Book An Introduction
Introduction to Data Structures
Programming Languages and Paradigms
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Circular Buffers, Linked Lists
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Discussions on hw7 Memory Allocation Exercise 3 functions:
Chapter 16 Pointers and Arrays
Your questions from last session
C Programming Basic – week 9
Homework Starting K&R Chapter 5 Good tutorial on pointers
Homework Continuing K&R Chapter 6.
Homework Continue with K&R Chapter 5 Skipping sections for now
Structures EECS July 2019.
Presentation transcript:

1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22

2 Self Referential structs, K&R 6.5 Look at this binary tree structure (Pg. 139) now isthe for all aidcome menoftime partytheirtogood

3 Self Referential structs struct tnode { /* the tree node struct */ char *word; /* points to the word at this node */ int count; /* has a count of occurances */ struct tnode *left; /* a word < one at this node */ struct tnode *right; /* a word > one at this node */ }; OK to have a pointer to a struct of same type in its own definition NOT OK to have a struct itself in its own definition!

4 Tree of struct tnodes char *word int count tnode *left tnode *right keyword \0 char *word int count tnode *left tnode *right char *word int count tnode *left tnode *right … … … … … … tnode *root

5 Self Referential structs Each node is a struct tnode with a string value and a count of occurrences Each node also contains a pointer to a left child and a right child Each child is another struct tnode

6 Declarations / Prototypes #include #include (see functions in App. B2, pp , K&R) #include (see functions in App. B3, pp , K&R) #include “getch.h” #define MAXWORD 100 int getword(char *word, int lim); struct tnode *addtree(struct tnode *, char *); void treeprint(struct tnode *);

7 Main program int main( ) { struct tnode *root; /* ptr to root for binary tree*/ char word[MAXWORD]; /* word to get from stdin */ root = NULL; /* initially, no tnodes in tree */ /* OK that root pointer points to NULL initially*/ while (getword(word, MAXWORD) != EOF) /* getword from stdin*/ if (isalpha(word[0])) /* from ctype.h library */ root = addtree(root, word); /* expect not NULL anymore*/ treeprint(root); /* print it out in order */ return 0; }

8 treeprint ( ) To print out tree contents in order: void treeprint (struct tnode *p) { if (p != NULL) { treeprint (p->left); printf (%4d %s\n”, p->count, p->word); treeprint (p->right); }

9 getword ( ) int getword(char *word, int lim) { int c; char *w = word; while isspace(c = getch())/* skip spaces; c first non-space*/ ; if (c != EOF) *w++=c;/* c might be EOF here (empty word) */ if(!isalpha(c)) {/* c (identifier) starts with alpha*/ *w = '\0'; return c;/* if not, word empty*/ } for( ; --lim > 0; w++) if (!isalnum(*w = getch())) {/* identfier alpha or digits*/ ungetch(*w); break; /* e.g., + might be needed later */ } *w = '\0'; return word[0]; }

10 addtree ( ) struct tnode *talloc(void); /* allocate. return ptr to tnode */ char *strdup(char *); /* allocate space, copy word there */ struct tnode *addtree(struct tnode *p, char *w) { int cond; /*for empty tree, root is NULL*/ if (p == NULL) { /* nothing at this node yet */ p = talloc(); /* allocate a new node */ p->word = strdup(w); /* allocate space, copy word there */ p->count = 1; /* count is 1 */ p->left = p->right = NULL; /* this works; see precedence */ } else if ((cond = strcmp(w, p->word)) == 0) /* string.h */ p->count++; /* repeated word, increment count */ else if (cond < 0) /* note cond remembers strcmp above */ p->left = addtree(p->left, w); /* less: go left subtree */ else /* more, go to right subtree */ p->right = addtree(p->right, w); return p; }

11 addtree( ) Note, that addtree is recursive. If it adds a node, it might need to add root or any left or right child anywhere down the tree We pass struct tnode *p as first argument, and also pass back the same type - always a pointer to the node at the current recursive nesting level Any time a new node is created: root, p->left or p->right in node above will be set for the FIRST time based on return value from addtree

12 addtree ( ) When main calls addtree, the return sets root value each time even if the root already pointed to tnode Allows for possible addition of a pointer to a node Why do we need to return a struct tnode *p when we have struct tnode *p as an argument? Can't we just set it via argument, return void from addtree, and write (in main)? addtree(root, word); instead of root = addtree(root, word);

13 addtree ( ) No, root is already a pointer, struct tnode *, but it is the pointer root itself that must be modified To modify root, we would need to declare addtree with a pointer to a struct tnode * as the first arg: void addtree(struct tnode **pptr, char *w) { struct tnode *p = *pptr; Call it from main recursively by invocation: addtree(&root, word); Then, addtree could set the value for root directly *pptr = p; Same is true when adding a left or right child

14 talloc ( ) How to write talloc to allocate a tnode and return a pointer. struct tnode *talloc(void) { return (struct tnode *) malloc (sizeof(struct tnode)); }

15 strdup ( ) strdup ( ) creates space for a character string, copies a word into it (up to null terminator) and returns the char pointer char *strdup(char *s) { p = (char *) malloc (strlen(s)+1); /* +1 for '\0' */ if (p != NULL) /* if malloc didn’t fail */ strcpy(p, s); /* library function in string.h */ return p; }

16 Intro to HW6 Memory model for malloc( ) and free( ) Before any calls to alloc( ): allocbuf: After two calls to alloc( ) and before call to freef( ): allocbuf: After call to freef( ) on first block alloc gave out: allocbuf: Free In Use Free In Use

17 Intro to HW6 Fragmentation with malloc( ) and free( ) Before call to alloc( ) for a large memory block: allocbuf: alloc can NOT provide a large contiguous block of memory - even though there is theoretically sufficient free memory! Memory is fragmented!! In useFreeIn useFreeIn useFreeIn use

18 Intro to HW6 Not possible to “defragment” allocbuf memory after the fact as you would do for a disk On a disk, the pointers to memory are in the disk file allocation table and can be changed With malloc, programs are holding pointers to memory they own - so can’t be changed!! Can only defragment as blocks are freed by using program

19 Intro to HW6 Initial structures in allocbuf[] Structures after two calls to alloc() blockl blockr Inuse 1Inuse 2Free Memory blocklblockr Free Memory

20 Intro to HW6 Structures after free call on first block only Structures after free call on second block only blockl blockr Inuse 1Free Memory blockl blockr Free MemoryInuse 2Free Memory Note effect of coalesce!

21 Intro to HW6 Four possible states for block being freed –Between two blocks that are still in-use –Above a free block and below an in-use block –Above an in-use block and below a free block –Above a free block and below a free block When freeing a block, coalesce with adjacent free blocks to avoid unnecessary fragmentation of allocbuf (allows alloc to serve later requests for large amounts of memory)