1 Homework / Exam 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 Continue HW5.

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

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.
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.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Kernighan/Ritchie: Kelley/Pohl:
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{
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
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.
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.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
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.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Lab 2 Intermediate Pointers & Basic Structures.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
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.
C Structures and Memory Allocation
Chapter 5, Pointers and Arrays
Memory allocation & parameter passing
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 374 Programming Concepts & Tools
Lesson One – Creating a thread
Programming Languages and Paradigms
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Miscellaneous functions
EECE.2160 ECE Application Programming
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
Homework Starting K&R Chapter 5 Good tutorial on pointers
7. Pointers, Dynamic Memory
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
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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 – Due at start of class 17 Exam Class 18 –Through end of K&R 6.4 plus MAKE

2 Using 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!

3 Using malloc( ) and free( ) malloc returns a pointer to void (void *) that is the address of 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)); Contents of memory block are NOT initialized!

4 Using 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

5 Using 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( )

6 Using 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!!

7 Pointers to Functions What is &foobar in the following code fragment? int main () { … &foobar … } int foobar (void) { /* some code here */ }

8 Pointers to Functions &foobar is the address of the entry point to the function foobar - in code space, not in data space int main () { … &foobar … /* pointer to foobar code’s entry point */ } int foobar (void) { /* some code here */ }

9 Pointers to Functions Why would we need the address of the entry point to foobar and how would we use it? We can pass the address of foobar as an argument to another function that needs to call foobar: … result = function (i, j, &foobar); … int foobar (void) { /* some code here */ }

10 Pointers to Functions Define, initialize, and use a pointer to a function /* Define the function pointer fooptr */ int (*) (void ) fooptr; /* type is “function pointer” */ /* set function pointer fooptr = address of foobar */ fooptr = &foobar; /* Call the function foobar via the pointer fooptr */ result = (*fooptr) (void); /* Why first parens? */

11 Pointers to Functions, K&R 5.11 Function prototype with a pointer to a function void qsort ( …, int (*comp) (void *, void *)); Function call passing a pointer to a function and casting the data types of the argument variables qsort( …, (int (*) (void *, void *)) strcmp); /* strcmp is defined with char * argument variables*/ /* but qsort needs a function with void * as arguments */ qsort calling the function via passed the pointer if ((*comp) (v[i], v[left]) < 0) … Note: qsort here is NOT same as standard library qsort, but it uses the same type of function pointer argument!

12 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 */

13 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)*/

14 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 */

15 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 xbox.pt1.xbox.pt2.x box.pt1.y box.pt2.y

16 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); }

17 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