Weeks 6-7 Memory allocation Pointers functions Complex structures with pointers, structures, etc Dynamic structures.

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

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
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.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
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:
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’;
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
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.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Memory allocation & parameter passing
Day 03 Introduction to C.
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
Checking Memory Management
Dynamic Memory Allocation
14th September IIT Kanpur
Pointers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
Pointers.
EECE.2160 ECE Application Programming
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Pointers.
Weeks 7-8 Memory allocation Pointers functions
Presentation transcript:

Weeks 6-7 Memory allocation Pointers functions Complex structures with pointers, structures, etc Dynamic structures

Memory Allocation Where we have dealt with memory issues: Address arithmetic: we had to use array to get memory Return characters strings: It is incorrect to return a local char array. cp char [] encode(int x) { char temp[4]; temp[0]=‘A’ + x; return temp; /* wrong */ } buff[256] malloc(256)

Memory Interface Interfaces: from malloc: void *malloc(int size); calloc: void *calloc(int num, int size); realloc: void *realloc(void *ptr, int size); free: void free(void *ptr); Miscellaneous memset: void *memset(void *ptr, int c, int size); bzero: void bzero(void *ptr, int size);

Usages To get a dynamic memory area, which exists until free To avoid a really large array To avoid static arrays (array with fixed sizes) malloc/free: struct student { int id; int num; /* wrong */ int bookids[num]; int *bookids; }; int main() { int i; struct student s; scanf(“%d %d”, &s.id, &s.num); s.bookids = (int *)malloc(sizeof(int)*num); while (i=0; i< s.num; i++){ scanf(“%d”, &s.bookids[i]); } free(s.bookids); return 0; }

Memory issues Memory Leak Pointer aliases Dangling pointers Garbage collection

Rules Do NOT dereference a pointer before its assigned a memory area Dereference only pointers with type than void* Always free the memory you allocated Be cautious of your pointer aliases and dangling pointers. Initialize your pointers with NULL and do sanity checks.

Pointer functions Syntax: type (* name) (argument list); Compare function prototypes: type name (argument list); Usage for ( i=0; i<20; i++) max = max > a[i] ? max: a[i]; return max; } int main() { int job, result, score[20]; int (*func)(int []); scanf(“%d”, &job); func = job == 1 ? find_average : find_max; result = func(score); return 0; } int find_average(int a[]) { int i, sum; for ( i=0; i<20; i++) sum +=a[i]; return sum/20; } int find_max(int a[]) { int max=a[0];

Passing Function pointers int main() { int job, result, score[20]; scanf(“%d”, &job); result = process (job, score, find_average, find_max); return 0; } int process(int job, int score[], int (*)a(int []), int(*)b(int [])); { int (*func)(int []); func = job ==1? a:b; return func(score); }

Pointer functions in Structures struct course { int course_num; int scores[20]; int (*find_min)(int []); int (*average)(int []); }; int main() { int result; struct course c459; … result=c459.find_min(c459.scores); … result=c459.average(c459.scores); … return 0; }