Dynamic memory allocation and Pointers Lecture 4.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
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.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
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:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
Dale/Weems/Headington
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
February 11, 2005 More Pointers Dynamic Memory Allocation.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Writing a Good Program 8. Elementary Data Structure.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
System Programming Practical Session 7 C++ Memory Handling.
Variables and memory addresses
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Introduction to Programming
Pointers and Memory Overview
Pointers Psst… over there.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
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 MANAGEMENT
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Dynamic memory allocation and Pointers Lecture 4

Pointers Special type of variable declaration Indicate we are pointing to an address in memory Allow us to use memory outside of the stack Numerically equivalent to a positive integer (A memory address)

Pointers Have their own rules Operations Operations Treated differently to static data declarations Treated differently to static data declarations

Memory used by a pointer int myInt; //assign 4 bytes char myChar; //assign 1 byte char *cptr; //4 bytes int *iptr; //4 bytes; You can check this by using the sizeof( ); function These are static declarations

Assigning a pointer variable A pointer variable can only be assigned an address in memory We use the address of operator : & int *iptr = 5; //invalid int a = 3; Int *iptr = &a; //valid

Declaring pointer variables int* iptr = NULL; These are all the same int& b = *iptr;

Dereferencing float f = 8.3; float* fptr =&f; cout << fptr; // prints the address in memory cout << *fptr //prints the value held in fptr cout << &fptr //prints the address of the address in memory

number++;(*iptr)++; cin >> letter; cin >> *cptr; if(letter == 'A') if(*cptr == 'A') cout << 10 - number; cout << 10 - *iptr; for (number=O; number<5; number++) for(*iptr=0; *iptr<5; (*iptr)++) int number = 12; Is the same as *iptr = 12;

DMA Processes OS is first process loaded OS is first process loaded OS manages the loading of subsequent processes multiple processes can be loaded at the same time multiple processes can be loaded at the same time processes are loaded in lowest RAM address space available processes are loaded in lowest RAM address space available all memory above the highest process is called the heap all memory above the highest process is called the heap

CODE compiler executable code compiler executable codeDATA globally declared variables globally declared variablesSTACK local variables & parameters local variables & parameters return address return address usage varies during program execution usage varies during program execution DATA + STACK maximum 1Mb maximum 1Mb

Process can ask OS to: Reserve memory on the heap as and when required Reserve memory on the heap as and when required Release memory previously reserved on the heap Release memory previously reserved on the heap Operating system Knows memory has been reserved Knows memory has been reserved ONLY releases memory when asked by a process ONLY releases memory when asked by a process

Everything up to now has been static The compiler determines: Memory required for the data type Memory required for the data type Reservation for the process stack Reservation for the process stack Usually only 1mb reserved (more these days) Usually only 1mb reserved (more these days)

It’s a brave new world The new keyword asks the OS for the required memory In C this is done using the malloc function void* malloc (size_t size); void* malloc (size_t size); void* operator new(size_t size); void* operator new(size_t size);new To be safe always use assignment to a pointer variable To be safe always use assignment to a pointer variable int *iptr = new int; int *iptr = new int;

For everything you new

You must

delete The delete keyword Requests that the values stored in memory are removed from the memory address Requests that the values stored in memory are removed from the memory address For safety anything you delete should be assigned a NULL value; For safety anything you delete should be assigned a NULL value;

Simple example #include using namespace std; void main() { int *iptr = NULL; iptr = new int; *iptr = 12; cout << *iptr << endl; delete iptr; iptr = NULL; } // IF you were to display the value of the pointer variable it would be a negative number

So back to functions void myfunc(const int* v1); Will not allow us to change v1 Will not allow us to change v1

What is this doing? void myfunc (int* & val);

Static wizards Wizard w; Wizard w(“Zedd”); w.setName(“Zedd”);

Pointers to a class Instantiation Wizard* w1 = new Wizard(); Wizard* w1 = new Wizard(); Wizard* w1 = new Wizard(“Richard”); Wizard* w1 = new Wizard(“Richard”);Use w1->setName(“Richard”); w1->setName(“Richard”); cout getName(); cout getName(); Memory release delete w1; delete w1;

Arrays and Pointers A pointer can be used to point to a block of memory such as an array A string is a character array A string is therefore also a character pointer

What about this? int* ptrarray = myarr; cout << *(ptrarray - 100) << endl; myarr[-100] would surely crash! myarr[-100] would surely crash!

Dynamic arrays Wizard * w = NULL; Can hold a pointer to a wizard object Can hold a pointer to a wizard object Or an array of Wizard objects Or an array of Wizard objects w = new Wizard[6]; w[4].setName(“Nathan”); What about: w->setName(“Bob”); w->setName(“Bob”);

double* distance = new double[1000]; float* wage = new float[67]; int* age = new int[44]; Wizard* w = new Wizard[19]; delete[] distance; delete[] wage; delete[] age; delete[] w; delete[] distance, wage, age, w; syntactically correct will get past compiler syntactically correct will get past compiler only first array released properly only first array released properly causes memory leak causes memory leak delete[] distance, []wage, []age, []w; invalid syntax invalid syntax generates syntax error generates syntax error