CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail.

Slides:



Advertisements
Similar presentations
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CS 192 Lecture 12 Winter 2003 December 31, January 1, 2004 Dr. Shafay Shamail.
Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
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.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
Thursday, January 18, 2007 The question of whether computers can think is just like the question of whether submarines can swim. -Edsger W. Dijkstra (1930.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
Dynamic memory allocation and Pointers Lecture 4.
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.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
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.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Pointers and Arrays Dynamic Variables and Arrays.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
The C++ Data Types Fundamental Data Types
Day 03 Introduction to C.
Introduction to Programming
Dynamic Array Multidimensional Array Matric Operation with Array
Day 03 Introduction to C.
Introduction to Programming
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Pointers and dynamic objects
הקצאות דינאמיות קרן כליף.
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Dynamic Memory A whole heap of fun….
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Dynamic Memory A whole heap of fun….
CS111 Computer Programming
Dynamic Memory A whole heap of fun….
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
Dynamic Objects.
Presentation transcript:

CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail

Void Pointer int age = 20; int *ageptr = &age; void *vptr = (void *)ageptr; int *newptr = (int *)vptr; Can’t do: vptr++ vptr— vptr + 2 As the base type of vptr is not known

Pointers and Multidimensional Arrays int ar[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; ar[1][2]= *(ar[1] + 2) = *(*(ar+1) + 2) = *(int *)((char *)ar + (1*3*4) + (2*4)) = *(int *)((char *)ar + 20) Where 3 = # of elements in each row And each int is of 4 byte long

Initializing Multi-Dimentional Arrays static int example[5][3] = { { 1, 2, 3 }, { 4 }, { 5, 6, 7 } }; static int example[5][3] = { 1, 2, 3, 4, 5, 6, 7 };

Initializing Multi-Dimensional Arrays static int example[][3][2] = { { {1, 1}, {2, 2}, {3,3} }, { {4, 4}, {5, 5}, {6,6} }, { {1, 1}, {2, 2}, {3,3} }, { {4, 4}, {5, 5}, {6,6} } }; 4-by-3-by-2 array

Arrays of Pointers char *fortunes[] = { "Soon, you will come into some money.\n", "A new love will enter your life.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; cout << fortunes[1] << endl << fortunes<<endl << *(fortunes) << endl << *(fortunes[2]) << endl; Output: A new love will enter your life 0012FFC6 Soon, you will come into some money Y

Arrays of Pointers … Two dimensional string pointers e.g. C++ dictionary char *keyword[][2] = { "for", "for(initialization; condition; increment)", "if", "if(condition)... else...", "switch", "switch(value) { case-list }", "while", "while(condition)...", // add the rest of the C++ keywords here "", "" // terminate the list with nulls }; int main() { char str[80]; int i; cout << "Enter keyword: "; cin >> str; for(i=0; *keyword[i][0]; i++) if(!strcmp(keyword[i][0], str)) cout << keyword[i][1]; return 0; }

Array of Strings vs. Array of Pointers  char movies[5][20] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”};  char *movies[5] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”}; each column is 20 characters wide due to the longest movie name wasted space the strings are placed contiguously in memory without wasting space pointers in the array movie point to them

The new operator int *x_ptr = new int; OR int *xptr; xptr = new int; //heap Dynamic Allocation

int *xptr=new int; *xptr = 73; int *x2ptr = new int; *x2ptr = 65; Dynamic Allocation

What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr; *x2ptr=65; Dynamic Allocation

int *xptr = new int; *xptr = 73; int *x2ptr; x2ptr = xptr; *x2ptr = 65; Dynamic Allocation

//What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr = new int; x2ptr = xptr; *x2ptr = 65; //memory leak Dynamic Allocation

int *myptr = new int(73); cout << *myptr; delete myptr; Dynamic Allocation

const int SIZE = 10; double *ptr = new double[SIZE]; /*10 element array*/ int i; for (i=0; i<SIZE; i++) { ptr[i] = 2.0*i; } for (i=0; i<SIZE; i++) { cout << ptr[i] << endl; } delete []ptr; Dynamic Allocation

Array of Pointers const int RSIZE = 4; const int CSIZE = 6; int * iarray[RSIZE]; for(int k=0; k<RSIZE; k++) { iarray[k] = new int [CSIZE]; if (iarray[k] == NULL) { cerr << “Memory Allocation Error…\n”; cerr << “Exiting …\n”; exit(-1); } A two dimensional array of RSIZE*CSIZE iarray

Double and Tripple Pointers int a = 20; int *ip = &a; int **ipp = &ip; int ***ippp = &ipp; cout << a << endl; cout << ip << *ip << endl; cout << ipp << *ipp << **ipp << endl; cout << ippp << *ippp << **ippp << ***ippp << endl; 20 a 100 ip 200 ipp 300 ippp

Dynamic Array of Pointers const int RSIZE = 4; const int CSIZE = 6; int ** matrix; matrix = new int * [RSIZE]; for(int k=0; k<RSIZE; k++) { matrix[k] = new int [CSIZE]; if (matrix[k] == NULL) { cerr << “Memory Allocation Error…\n”; cerr << “Exiting …\n”; exit(-1); } A two dimensional array of RSIZE*CSIZE matrix