POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 18 Vectors and Arrays
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
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 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
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.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
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.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: 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. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
Pointers & Dynamic Arrays Shinta P.
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
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++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Dynamic Memory CSCE 121 J. Michael Moore.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Destructor CSCE 121 J. Michael Moore.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Destructor CSCE 121.
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Run-time environments
SPL – PS2 C++ Memory Handling.
Presentation transcript:

POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1

POINTERS  Pointers are variables storing the memory address of another data/variable/object. 0x000x040x080x0C0x100x140x180x1C0x20 2

POINTERS  Pointers are variables storing the memory address of another data/variable/object.  int x=5; 5 x 0x000x040x080x0C0x100x140x180x1C0x20 3

POINTERS AND MEMORY  Pointers are variables storing the memory address of another data/variable/object.  int x=5;  int *y = &x;  Operator & gives the memory address of a variable/object 5 xy 0x04 0x000x040x080x0C0x100x140x18 0x1C0x20 4

POINTERS AND MEMORY  Pointers are variables storing the memory address of another data/variable/object.  int x=5;  int *y = &x;  Operator & gives the memory address of a variable/object  int* z = y; 5 xy 0x04 0x000x040x080x0C0x100x140x18 0x1C0x20 5 z 0x04

POINTERS AND MEMORY  int *y = &x;  int* z = y;  What will happen if I write: *z = 0; ? 5 xy 0x04 0x000x040x080x0C0x100x140x18 0x1C0x20 6 z 0x04

POINTERS AND MEMORY  int *y = &x;  int* z = y;  What will happen if I write: *z = 0; ?  * is a dereferencing operator – gives the content of the memory address pointed by (or stored in) the pointer 0 xy 0x04 0x000x040x080x0C0x100x140x18 0x1C0x20 7 z 0x04

MEMORY ALLOCATION – NEW OPERATOR  new allocates space to hold the object.  new calls the object’s constructor.  new returns a pointer to that object.  Point * A = new Point (10, 20); 8

MEMORY DEALLOCATION – DELETE OPERATOR  For every call to new, there must be exactly one call to delete.  Point * A = new Point (10, 20); //allocates memory  …  delete A; //deallocates or frees the memory 9

POINTERS AND ARRAY  An array can act as a pointer  Array name is a pointer to first element in the array  Pointer can be indexed like an array  int arr[5] = {1,2,3,4,5};  cout << arr[2] << "," << *(arr+2); //displays 3,3 10

DYNAMIC ALLOCATION & DEALLOCATION OF ARRAY  Static allocation:  You must know the size of the array before hand  int arr[10];  Dynamic Allocation  Size of the array can be passed as a variable  size_t sz = 10;  int* arr = new int[sz];  Deallocate : delete[] arr; 11

MEMORY 12 MEMORY HEAPSTACK

STACK VS. HEAP  Heap – Dynamic Allocation  Point *p = new Point(5,10);  double *amount = new double[5];  Stack – static allocation  Point p(5,10);  double amount[5]; 13 What happens when p goes out of scope?

PAIR PROGRAMMING  Driver – One at the keyboard  Navigator – Helps direct driver  Teamwork and communication are key here  Switch roles frequently! 14