Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Programming and Data Structure
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Computer Architecture CSCE 350
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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’;
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.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
Runtime Environments Compiler Construction Chapter 7.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
C++ Memory Overview 4 major memory segments Key differences from Java
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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 and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
System Programming Practical Session 7 C++ Memory Handling.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Overview 4 major memory segments Key differences from Java stack
Day 03 Introduction to C.
Chapter 12 Variables and Operators
Pointers and Memory Overview
Day 03 Introduction to C.
Overview 4 major memory segments Key differences from Java stack
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Dynamic Memory A whole heap of fun….
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Dynamic Memory A whole heap of fun….
C (and C++) Pointers April 4, 2019.
Destructor CSCE 121.
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Run-time environments
Presentation transcript:

Dynamic Memory Allocation in C++

Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds global data – Stack Segment Used to contain local variable and other temporary information In addition to these, the operating system provides and extra amount of memory called Heap

C++ Variable Name – An identifier to recognize the variable Type – The set of values it can take Scope – The area of code it is visible to Address – The address at which the variable is located Size – The amount of memory to store its content

An Example Void cookie() { int integer_x; ……. } Name: integer_x Type: int Scope: local, only visible within the cookie function Size: 32 bits (can vary)

Dynamic Variable Stored in heap, outside the stack and data segments May change size during runtime or may disappear for good If no explicit allocation of memory is done, it never exists double *dp; – Tells the compiler to have a pointer dp that will hold the starting address of a variable of type double in heap memory

Dynamic Variable (contd…) To create the dynamic variable new is used. – dp = new double; Will create a dynamic variable of type double and makes dp pointer point to its starting address It is always a good practice to initialize dynamic variables. – *dp = a or dp = new double(a);

Visual Demonstration dp 0045:02FE ….. …… 0045:02FE ….. …. ….. …… …… ….. ……. *dp Pointer variable (data segment) Dynamic Variable (Heap)

Deleting Dynamic Variable delete operator is used to delete the memory allocated for the dynamic variable in heap delete dp; – Will delete 8 bytes of the memory allocated for the double variable NOTE: the dp pointer will not be erased as it is created in code/data segment and can be point some other dynamically allocated memory location

Dynamic Arrays C++ also provides the possibility to allocate an entire array in the heap – int *table; – table = new int[100]; Assigns 100 * sizeof(int) = 100 * 4 = 400 bytes of memory and assign the starting address to table pointer Arrays defined in heap are similar to the ones defined in data and code segments and can be accessed using [] For (int i = 0; i < 100; i++) – table[i] = 0; will initialize all the array items to zero

Deleting Dynamic Arrays Again delete operator is used – [] is used will delete operator to tell the compiler that you are deleting arrays not variables delete [] table; will delete not only the memory location pointed by table but all the items in the array

Common mistakes made in dynamic allocation Freeing an already freed variable char *str = new char [100]; delete [] str; delete [] str; // error Freeing a dynamic variable not assigned yet char *str; delete str; // error Accessing or assigning value to the freed variable int *str = new int[100]; delete [] str; str[10] = 90; // error