Pointers and dynamic memory

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Informática II Prof. Dr. Gustavo Patiño MJ
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 Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Pointers and Dynamic Memory Allocation. Dynamic Data Suppose we write a program to keep track of a list of students How many student records should we.
Run-Time Storage Organization
C and Data Structures Baojian Hua
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
Outline Midterm results Static variables Memory model
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C Programming Chapters 11, . . .
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
CSE 220 – C Programming malloc, calloc, realloc.
Day 03 Introduction to C.
Linked list.
Day 03 Introduction to C.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
CSCI206 - Computer Organization & Programming
The Hardware/Software Interface CSE351 Winter 2013
CSC 253 Lecture 8.
Lecture 4: Process Memory Layout
Storage.
Programming and Data Structures
CSC 253 Lecture 8.
Pointers and dynamic memory
Dynamic Memory A whole heap of fun….
Memory Allocation CS 217.
Understanding Program Address Space
Pointers and dynamic memory
Dynamic Memory A whole heap of fun….
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
C (and C++) Pointers April 4, 2019.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory And Objects
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
Pointers, Dynamic Data, and Reference Types
Run-time environments
Presentation transcript:

Pointers and dynamic memory

What if we don’t know how much memory will be required to store our data?

It’s possible to determine the amount of memory needed at the execution time and then request this from the operating system. This is called dynamic memory allocation.

Pointers and dynamic memory #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Play sq() x Pause sos() a,b,c main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Play sos() a,b,c Pause main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Play main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Play printf() Pause main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Play main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Stack frame Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Stack frame Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack (1 MB)  D()  Stack overflow C() B() Stack frame A() Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Stack frame Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total);   Application's memory  Free store Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Stack Stack frame Global Heap -- Not heap date structure   To access heap – malloc() calloac() realloc() free()   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

Pointers and dynamic memory   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; *p = 20; } Stack Heap   20 800 main() p = 400 800 10 400 x

Pointers and dynamic memory   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; free(p); *p = 20; } Stack Heap   20 800 main() p = 400 800 400 x

Pointers and dynamic memory   Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; free(p); p = (int*)malloc(10*sizeof(int)); *p = 20; } Stack Heap   200 main() p = 200 240 x p[0], p[1], p[2] *p, *(p+1), *(p+2)

Dynamic memory allocation - Pros No need to decide how much memory to allocate when writing the program, giving us more flexibility. Free up memory for other programs after it is no longer used by the program which allocates it.

Dynamic memory allocation - Cons Have to use pointers. The programmer has the responsibility of tracking the locations of allocated memory and freeing it up. It’s possible to create a memory leak, i.e., memory that is no longer used is not freed. If the process is repeated, the amount of unfreed memory grows in size.

Example 1 int i, *d; d = (int *) malloc( 5 * sizeof(int) ); for(i = 0; i < 5; i++) d[i] = i + 100;