SPL – PS2 C++ Memory Handling.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
Basic Semantics Associating meaning with language entities.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
System Programming Practical Session 7 C++ Memory Handling.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Dynamic Allocation in C
Object Lifetime and Pointers
Dynamic Storage Allocation
EGR 2261 Unit 11 Pointers and Dynamic Variables
Data Types In Text: Chapter 6.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 374 Programming Concepts & Tools
CS 215 Final Review Ismail abumuhfouz Fall 2014.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Pointers and Memory Overview
Pointers and references
Java Review: Reference Types
This pointer, Dynamic memory allocation, Constructors and Destructor
Advanced Programming Behnam Hatami Fall 2017.
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers and Arrays Beyond Chapter 16
Passing Arguments and The Big 5
Dynamic Memory.
Exercise Arrays.
Course Overview PART I: overview material PART II: inside a compiler
Chapter 9: Pointers and String
Pointers and References
Pointers, Dynamic Data, and Reference Types
Pointers and references
SPL – PS3 C++ Classes.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

SPL – PS2 C++ Memory Handling

Memory model Each process running on the system has it’s own memory space. This memory space is the range of addresses that can be described using a single word. In this memory space the process puts any information it needs. This includes compiled code, constants, and variable it needs. In this course we will focus on two “kinds” of memory available to the process. The stack and the heap.

Memory model - cont Stack: Used for static memory allocation. Every variable you declare inside a function lives on the stack. Once you leave the scope of the function, the stack variables are discarded. Heap: Used for dynamic memory allocation. Variables allocated on the heap have their memory allocated at run time. You can allocate a block any time, and free it any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.

Java references The actual value of the string s is on the heap, the value of the variable s is the location of this data on the heap. Here, the variable s can hold the address of a String object on the heap, but it currently holds a null value. In Java, primitive types are stored in the stack.

C++ pointers In C++ you have access to actual memory locations. Declaring a pointer is done with using the operator ‘*’ before the name of the variable. Accessing the address of a variable is done using the operator ‘&’

C++ pointers (cont) A C++ pointer is simply a number (between 0-2^32 in a 32bit system). The null value in C++ is the number 0. (Memory address 0) You can (but usually shouldn’t) assign an integer value to a pointer. When appearing before a pointer, the ‘*’ operator means – access the value in this address.

C++ pointers (cont) You can also compare the value of pointers

Pointers to pointers

Pointers to pointers (cont)

Pointers arithmetic A pointer is simply a number, so why do we need different kinds of pointers? (int*,char*) Reason 1: Type safety Reason 2: Pointer arithmetic The operators ‘+’, ‘+=‘, ‘++’, ‘-’, ‘-=‘, ‘--’ are all defined to pointers.

C++ Arrays Arrays in C++ are const pointers. Declaring arrays is done with the following syntax: In this line a memory block big enough for holding 10 integers was allocated on the stack. A holds the address of the start of that memory block. Accessing an array value is done like in Java using the operator []

C++ arrays (cont) In the first line, we declare an array of size 10 In the second line, we put ‘9’ in the fourth place in the array. The second line could also be written as *(A+3)=9 The third line copies the fourth element in the array to the variable j. It could also be written as int j = *(A+3) In C++ arrays don’t have length properties, C++ arrays are simply pointers. For this reason, there is no “array out of bounds” exception in C++.

Using regular pointers like arrays

Using regular pointers like arrays (cont)

String vs char* In the previous practical session we’ve seen the std::string object. C++ has another way of representing a string: an array of characters, where the last character has the value 0.

String vs char* (cont) You can convert between std::string and char* and back. The command line arguments are passed as c-strings (char*) to the main function.

C++ references C++ supports a concept called references. There are two types of references, lvalue and rvalue references. lvalue refers to an object that persists beyond a single expression. rvalue refers to a temporary value that does not persist beyond the expression that uses it. ravlue cannot appear on the left side of an expression.

C++ references (cont)

lvalue references. Behave like const pointers. Point to a specific location which cannot be changed afterwards. Must be initialized with a real value. (Not null) Declared using the operator & after the type.

C++ memory handling Up until now we discussed allocating memory on the stack. Allocating memory on the heap is done using the “new” operator. “new” allocates a memory on the heap, initializes it, and returns a pointer to it. All memory allocated with “new” must be freed, or you will get a memory leak. Freeing memory is done using the operator “delete”

C++ memory handling (cont)

Arrays on the heap Use the new[] operator to allocate a block of memory on the heap, and delete[] to free that block.

Arrays on the heap(cont)

2-D array on the heap. A 2-D array is simply an array of pointers to arrays. You can initialize it using a loop.

2-D arrays on the heap (cont)

Pointers dangers Uninitialized pointers Dereferencing null pointers Dereferencing a deleted pointer

Pointer dangers(cont) Dereferencing a dangling pointer Beware of memory leaks Safe delete