Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

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
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
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.
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.
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.
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.
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.
Pointers & Dynamic Arrays Shinta P.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
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.
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 MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
CSC 253 Lecture 8.
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
By Hector M Lugo-Cordero September 17, 2008
Indirection.
Dynamic Memory A whole heap of fun….
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.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Standard Version of Starting Out with C++, 4th Edition
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

Pointers Pointers are variables storing the memory address of another data/variable/object. 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20

Pointers Pointers are variables storing the memory address of another data/variable/object. int x=5; 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 x

5 x y Pointers and Memory 0x04 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 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 x y

5 x y z Pointers and Memory 0x04 0x04 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; 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 0x04 x y z

5 x y z Pointers and Memory 0x04 0x04 int *y = &x; int* z = y; What will happen if I write: *z = 0; ? 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 0x04 x y z

x y z Pointers and Memory 0x04 0x04 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 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x04 0x04 x y z

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);

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

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

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;

Memory HEAP MEMORY STACK

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]; 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!