Introduction to Programming

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

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Dynamic memory allocation
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
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.
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
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.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different.
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:
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’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Pointers. 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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
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.
Dynamic memory allocation and Pointers Lecture 4.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
ENEE150 Discussion 07 Section 0101 Adam Wang.
2016.
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
By Hector M Lugo-Cordero September 17, 2008
Dynamic Memory A whole heap of fun….
CS111 Computer Programming
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Presentation transcript:

Introduction to Programming Lecture 24

Today’s Agenda Memory Allocation Dynamic memory allocation Advantages/disadvantages of Dynamic and static memory allocation Common programming errors while using Dynamic memory allocation

Static Memory Allocation int i , j , k ; char s [ 20 ] ;

Compile Time Allocation

Dynamic Memory Allocation

Heap

Pointers

void Pointer

int *i ; char *s ; i is a pointer to an integer

void *ptr ;

Cast

void *ptr ;

( int * ) ptr ;

NULL

calloc ( n , m ) ; Space in terms of numbers of elements Space in terms of size each of elements

calloc ( 1000 , sizeof ( int ) ) ;

( int * ) calloc ( 1000 , sizeof ( int ) ) ;

void * calloc ( size_t n , size_t el-size ) ;

Example 1 int *iPtr ; iPtr = ( int * ) calloc ( 1000 , sizeof ( int ) ) ; if ( iPtr == NULL ) exit ( ) ;

Number of bytes required void * malloc ( n ) ; Number of bytes required

malloc (1000 *sizeof ( int ) ) ) ;

malloc ( n ( sizeof ( float ) ) ) ;

Static Memory Allocation #define MAXSTUDENT 100 int Student [ MAXSTUDENT ] ;

Problem statement Find the average age of the students in your class

Example 2 int numstd ; int *iPtr , *sPtr ; cout << "Enter the number of students " << endl ; cin >> numstd ; iPtr = malloc ( numstd * ( sizeof ( int ) ) ) ; if ( iPtr == NULL ) {         cout << "Error on malloc " ;         return 1 ;         /* Use a nonzero return to indicate an error has occurred */ } // a while loop to read the ages of the student and place them in the memory sPtr = iPtr ; sPtr++ ;

free ( iPtr ) ;

realloc(void *iPtr, size_t size);

Unreferenced Memory

Memory Leaks

Example main ( ) { funct ( ) ; } funct ( ) int *iPtr ; iPtr = malloc ( 1000 * ( sizeof ( int ) ) ) ; // used the memory

Dangling Pointers

Example int *ptr1 , *ptr2 ; ptr1 = malloc (1000 * ( sizeof ( int ) ) ) ; ptr2 = ptr1 ; - - - free ( ptr1 ) ;

Multi-tasking

Review Dynamic Memory Allocation Efficient usage of computers resources Have to do memory management