EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Advertisements

COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 13 Static vs Dynamic Memory Allocation
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
ECE Application Programming
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Introduction to Programming
ECE Application Programming
Programmazione I a.a. 2017/2018.
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Programming Languages and Paradigms
Some examples.
Programming and Data Structures
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EECE.2160 ECE Application Programming
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2018 Lecture 33: Dynamic memory allocation

ECE Application Programming: Lecture 31 Lecture outline Announcements/reminders Program 9 due Wednesday, 5/2 Deals with basic file I/O Program 10 (extra credit) to be posted; due Wednesday, 5/9 Deals with file I/O and bitwise operators Up to 4 points of extra credit on your final average Only one chance to turn in—no resubmissions Grading will be tougher than typical assignment All outstanding regrades due Thursday, 5/3 Course evaluations to be posted Today’s class Review: bitwise operators, hex output Dynamic memory allocation 4/4/2019 ECE Application Programming: Lecture 31

Review: bit manipulation Bitwise operators: | & ^ ~ Used for desired logical operations Used to set/clear bits Bit shifts: << >> Used to shift bits into position Used for multiplication/division by powers of 2 Common operations Setting/clearing/flipping individual bit Setting/clearing/flipping multiple bits Extracting bits i.e. x = (x & 0x00FFFF00) >> 8; 4/4/2019 ECE Application Programming: Exam 3 Preview

Review: hexadecimal output To print a number in hex, use %x or %X %x prints characters a-f in lowercase %X prints characters A-F in uppercase To show leading 0x, use the # flag To show leading 0s, use precision with total # chars Field width + 0 flag also works unless value = 0 Examples (assume var1 = 0x1A2B) printf(“%x”, var1)  1a2b printf(“%X”, var1)  1A2B printf(“%#x”, var1)  0x1a2b printf(“%.6x”, var1)  001a2b printf(“%#.6x”, var1)  0x001a2b 4/4/2019 ECE Application Programming: Exam 3 Preview

Justifying dynamic memory allocation Data structures (i.e., arrays) usually fixed size Array length set at compile time Can often lead to wasted space May want ability to: Choose amount of space needed at run time Allows program to determine amount Modify size as program runs Data structures can grow or shrink as needed Dynamic memory allocation allows above characteristics 4/4/2019 ECE Application Programming: Lecture 31

Allocation functions (in <stdlib.h>) All return pointer to allocated data of type void * (no base type—just an address) Must cast to appropriate type Arguments of type size_t: unsigned integer Basic block allocation: void *malloc(size_t size); Allocate block and clear it: void *calloc(size_t nmemb, size_t size); Resize previously allocated block: void *realloc(void *ptr, 4/4/2019 ECE Application Programming: Lecture 31

ECE Application Programming: Lecture 31 4/4/2019 ECE Application Programming: Lecture 31

Basic allocation with malloc() void *malloc(size_t size); Allocates size bytes; returns pointer Returns NULL if unsuccessful Example: int *p; p = malloc(10000); if (p == NULL) { /* Allocation failed */ } 4/4/2019 ECE Application Programming: Lecture 31

ECE Application Programming: Lecture 31 Type casting All allocation functions return void * Automatically type cast to appropriate type Can explicitly perform type cast: int *p; p = (int *)malloc(10000); Some IDEs (including Visual Studio) strictly require type cast 4/4/2019 ECE Application Programming: Lecture 31

ECE Application Programming: Lecture 31 Application: arrays One common use of dynamic allocation: arrays Can determine array size, then create space Use sizeof() to get # bytes per element Array notation can be used with pointers int i, n; int *arr; printf("Enter n: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; i++) arr[i] = i; 4/4/2019 ECE Application Programming: Lecture 31

Allocating/clearing memory: calloc() void *calloc(size_t nmemb, size_t size); Allocates (nmemb * size) bytes Sets all bits in range to 0 Returns pointer (NULL if unsuccessful) Example: integer array with n values int *p; p = (int *)calloc(n, sizeof(int)); 4/4/2019 ECE Application Programming: Lecture 31

Resizing allocated space: realloc() void *realloc(void *ptr, size_t size); ptr must point to previously allocated space Will allocate size bytes and return pointer size = new block size Rules: If block expanded, new bytes aren’t initialized If block can’t be expanded, returns NULL; original block unchanged If ptr == NULL, behaves like malloc() If size == 0, will free (deallocate) space Example: expanding array from previous slide p = (int *)realloc(p, (n+1)*sizeof(int)); 4/4/2019 ECE Application Programming: Lecture 31

Deallocating memory: free() All dynamically allocated memory should be deallocated when you are done using it Returns memory to list of free storage Once freed, program should not use location Deallocation function: void free(void *ptr); Example: int *p; p = (int *)malloc(10000); ... free(p); 4/4/2019 ECE Application Programming: Lecture 31

Example: what does program print? void main() { int *arr; int n, i; n = 7; arr = (int *)calloc(n, sizeof(int)); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); n = 3; arr = (int *)realloc(arr, n * sizeof(int)); for (i = 0; i < n; i++) { arr[i] = i * i; } n = 6; arr = (int *)realloc(arr, n * sizeof(int)); for (i = 0; i < n; i++) { arr[i] = 10 - i; printf("%d ", arr[i]); } free(arr); 4/4/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Solution Output: 0 0 0 0 0 0 0 0 1 4 10 9 8 7 6 5 4/4/2019 ECE Application Programming: Lecture 30

Pitfalls: memory leaks Changing pointers leaves inaccessible blocks Example: p = malloc(1000); q = malloc(1000); p = q; Block originally accessed by p is “garbage” Won’t be deallocated—wasted space Solution: free memory before changing pointer free(p); 4/4/2019 ECE Application Programming: Lecture 30

Pitfalls: dangling pointers free() doesn’t change pointer Only returns space to free list Pointer is left “dangling” Holds address that shouldn’t be accessed Solution: assign new value to pointer Could reassign immediately (as in previous slide) Otherwise, set to NULL free(p); p = NULL; 4/4/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 31 Next time Dynamic memory allocation (continued) Remaining topics TBD Reminders: Program 9 due Wednesday, 5/2 Deals with basic file I/O Program 10 (extra credit) to be posted; due Wednesday, 5/9 Deals with file I/O and bitwise operators Up to 4 points of extra credit on your final average Only one chance to turn in—no resubmissions Grading will be tougher than typical assignment All outstanding regrades due Thursday, 5/3 Course evaluations to be posted 4/4/2019 ECE Application Programming: Lecture 31