Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic Memory Management
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
ECE Application Programming
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
C Structures and Memory Allocation
Chapter 5, Pointers and Arrays
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CSE 374 Programming Concepts & Tools
Lesson One – Creating a thread
Dynamic Memory Allocation
Programming Languages and Paradigms
Dynamic Memory Allocation
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Pointers, Dynamic Data, and Reference Types
Miscellaneous functions
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
C Structures and Memory Allocation
Dynamic Memory – A Review
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc( ) and free( ) To get a pointer p to a block of memory that is n characters in length, program calls p = malloc(n); When it is finished with that memory, the program returns it by calling free(p); Sounds simple, huh? It is NOT so simple!

malloc( ) and free( ) malloc returns a pointer (void *) that points to a memory block of n bytes If you need a pointer to n of a specific type, you must request a memory block in size of the type and cast pointer returned by malloc int *p; p = (int *) malloc(n * sizeof(int));

malloc and free If it can not provide the requested memory, malloc returns a NULL pointer value If you dereference a NULL pointer to access memory  System Crash!! Always check to be sure that the pointer returned by malloc is NOT equal to NULL If pointer is NULL, code must take appropriate recovery action to handle lack of memory

malloc and free Call to free does not clear the program’s pointer to the memory block, so it is now a “stale” pointer If program uses pointer after free( ) by accessing or setting memory via pointer, it could overwrite data owned by another program  System Crash! If program calls free again with the same pointer, it releases memory possibly owned by a different program now  System Crash! SHOULD set pointer to NULL after calling free( )

malloc and free However, if you set the pointer to a memory block to NULL before calling free, you have caused the system to lose the memory forever This is called a memory leak!! If it happens enough times  System Crash!! MUST not clear or overwrite a pointer to a memory block before calling free!!

malloc( ) and free( ) Memory model for malloc( ) and free( ) Before call to malloc( ) and after call to free( ): allocbuf: After call to malloc( ) and before call to free( ): allocbuf: In use Free In useFree In use Free

malloc( ) and free( ) Fragmentation with malloc( ) and free( ) Before call to malloc( ) for a large memory block: allocbuf: malloc 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

malloc and free The malloc memory fragmentation problem is not easy to solve!! Not possible to “defragment” malloc memory 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!!

Pointers to Functions, K&R 5.11 Function prototype with pointer to function void qsort ( …, int (*comp) (void *, void *)); Function call passing a pointer to function qsort( …, (int (*) (void *, void *)) strcmp); This is a cast to function pointer of strcmp Within qsort(), function is called via a pointer if ((*comp) (v[i], v[left]) < 0) …

Pointers to Functions Initialize a pointer to a function /* function pointer *fooptr = cast of foo to func ptr */ int (*fooptr) (int *, int*) = (int (*) (int *, int *)) foo; Call the function foo via the pointer to it (*fooptr) (to, from);

structs, K&R 6 A struct is a collection of variables, possibly of different types, grouped under a single name for common reference as a unit. struct point {/* with optional tag */ int x; /* member x */ int y;/* member y */ } pt, q; /* variable names */ or struct {/* w/o optional tag */ int x, y;/* two members */ } pt, q;/* variable names */

structs The defined struct point is like a new “type” With the tag, point, can declare other variables: struct point pt1, maxpt = {320, 200}; Reference to struct members: pt1.x = 320; pt1.y = 200; /* alternate init */ printf("(%d, %d)\n", pt1.x, pt1.y); /* prints out as (320, 200)*/

structs Defining a struct inside a struct (nesting). struct rect { struct point pt1; /* lower left */ struct point pt2; /* upper right */ }; struct rect box;/* declare box as a rect */

structs pt1 is lower left hand corner of box pt2 is upper right hand corner of box: box.pt1.x < box.pt2.x box.pt1.y < box.pt2.y y x box.pt1.xbox.pt2.x box.pt1.y box.pt2.y

structs /* Find area of a rectangle */ int area = rectarea (box); … int rectarea (struct rect x) { return (x.pt2.x - x.pt1.x) * (x.pt2.y - x.pt1.y); }

structs Memory allocation for structs –Two point structs pt1 and pt2 –One rect struct box containing two point structs pt1 pt1.xpt1.ypt2.xpt2.y pt2 pt1 pt1.xpt1.ypt2.xpt2.y pt2 box