Dynamic Memory Allocation Reference Variables

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Writing a Good Program 8. Elementary Data Structure.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Learners Support Publications Operators.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers and Dynamic Arrays
Operators in c++.
Introduction to Programming
C/C++: type sizes in memory pointers
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointer Data Type and Pointer Variables II
Pointers Psst… over there.
this Pointer Scope Resolution Operator Friend Function
group work #hifiTeam
Static Data Member and Functions
Lecture 8 – 9 Arrays with in a class
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 2 Elementary Programming
Pointers and dynamic objects
Contents Introduction to Constructor Characteristics of Constructor
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Operators.
Dynamic Memory A whole heap of fun….
CS150 Introduction to Computer Science 1
Arrays an array of 5 ints is filled with 3,2,4,1,7
Pointers and Pointer-Based Strings
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Pointers and dynamic objects
Using string type variables
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Dynamic Objects.
Video: The Sound of Raining
Presentation transcript:

Dynamic Memory Allocation Reference Variables Lecture 11 Dynamic Memory Allocation Reference Variables 11/12/2018 UTA009

Dynamic Allocation Operators new Allocates memory and returns a pointer to the start of it. Let p_var is a pointer variable of any data type type, then p_var = new type; delete Frees memory previously allocated using new. delete p_var; 11/12/2018 UTA009

Example – 1 #include<iostream> using namespace std; int main() { int *p; p = new int(87); cout << "Value is: " << *p; delete p; return 0; } 11/12/2018 UTA009

Example – 2 (Dynamic Arrays) #include <iostream> using namespace std; int main () { int i, n, *p; cout << "How many numbers would you like to type? "; cin >> n; p = new int[n]; for (i = 0; i < n; i++) { cout << "Enter number: "; cin >> p[i]; } cout << "You have entered: "; for (i = 0; i < n; i++) cout << p[i] << " "; delete[] p; return 0; } 11/12/2018 UTA009

Some points… In case of insufficient memory, new returns null pointer. So check for the pointer produced by new before using it. ….. p = new int; If(!p) count << "Allocation failed\n"; 11/12/2018 UTA009

Advantages Automatically computes size of the data object, no need to use sizeof operator. Automatically returns correct pointer type, no need to use type cast. Possible to initialize the object during the memory space creation. New and delete can be overloaded. 11/12/2018 UTA009

data-type &reference-name = variable-name Reference variable It provides an alias, an alternative name, for a previously defined variable. Syntax: data-type &reference-name = variable-name Must be initialized at the time of declaration. Example: int n[10]; int &x = n[10]; char &a = '\n'; int x; int *p = &x; int &m = *p; 11/12/2018 UTA009

UTA007 - Computer Programming I Example #include <iostream> using namespace std; int main() { int a = 10; int &ref = a; cout << a << " " << ref << endl; ref += 5; return 0; } Thapar University UTA007 - Computer Programming I

Example – Call-by-reference #include<iostream> using namespace std; void add(int &n); int main() { int number; number = 34; cout << " The initial value of number : " << number << endl; add(number); cout << " The final value of number : " << number << endl; return(0); } void add(int &n) { n = n + 6; } Thapar University UTA007 - Computer Programming I

Restrictions to References Reference to another reference is not possible. Address of a reference cannot be obtained. Arrays of references cannot be created. Pointer to a reference cannot be created. Reference for a bit-field is not possible. A reference variable must be initialized when it is declared unless it is a member of a class, a function parameter, or a return value. Null references are prohibited. 11/12/2018 UTA009