Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Pointers by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
System Programming Practical Session 7 C++ Memory Handling.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
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.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Dynamic Allocation in C
Object Lifetime and Pointers
Pointers and Dynamic Arrays
8 Pointers.
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation Reference Variables
Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures

Acknowledgement Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Ms. Krishani Abeysekera

Memory Address & Pointers Each byte in the primary memory has an address. A pointer is a variable that stores a memory address of another variable. The address (reference) operator & is used to return the address of a variable. char ch = ‘q’; char* p = & ch;//&returns & of ch cout << *p; //outputs q by dereferencing

Pointer Type A pointer variable has a type associated with it, and the memory address it points to should hold data of that type. int myInt = 0;//Allocates memory stores 0 int *pMyInt;// Declares pointer variable without initialization int *x, y, z; //int *x, int y, int z

Pointer Type Checking A pointer variable of a particular type cannot store address of an incompatible type. int x = 10; float* fp_x = &x; MS VS: “Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast”

An Example #include using namespace std; int main() { int x = 10; float y = ; string z = "I am a college professor."; bool b = true; cout << "x: value: " << x << " addr: " << &x << endl; cout << "y: value: " << y << " addr: " << &y << endl; cout << "z: value: " << z << " addr: " << &z << endl; cout << "b: value: " << b << " addr: " << &b << endl; }

Output x: value: 10 addr: 0012FF50 y: value: addr: 0012FF44 z: value: I am a college professor. addr: 0012FF1C b: value: 1 addr: 0012FF13

Pointer Type Checking A pointer variable of a particular type cannot store address of an incompatible type. int x = 10; float* fp_x = &x; MS VS: “Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast”

Dereferencing Dereferencing (indirection) operation in C++: *. *p: access the value of the memory address stored in p. * is heavy overloaded in C++:  Number multiplication  Pointer variable declaration  Dereferencing.

Example … int main() { int x = 10; cout << "value of x = " << x << endl; int* p_x = &x; cout << "value of p_x = " << p_x << endl; int** p_p_x = &p_x; cout << "value of p_p_x = " << p_p_x << endl; int*** p_p_p_x = &p_p_x; cout << "value of p_p_p_x = " << p_p_p_x << endl; *p_x = 15; cout << "value of x = " << x << endl; }

Output of Example value of x = 10 value of p_x = 0012FF60 value of p_p_x = 0012FF54 value of p_p_p_x = 0012FF48 value of x = 15

Example // Dereferencing: cout << "value of *p_x = " << *p_x << endl; cout << "value of *p_p_x = " << *p_p_x << endl; cout << "value of **p_p_x = " << **p_p_x << endl; cout << "value of *p_p_p_x = " << *p_p_p_x << endl; cout << "value of **p_p_p_x = " << **p_p_p_x << endl; cout << "value of ***p_p_p_x = " << ***p_p_p_x << endl;

Output of Example value of *p_x = 10 value of *p_p_x = 0012FF60 value of **p_p_x = 10 value of *p_p_p_x = 0012FF54 value of **p_p_p_x = 0012FF60 value of ***p_p_p_x = 10

Example: Same Effect … int main() { int x = 10; cout << "value of x = " << x << endl; int* p_x = &x; cout << "value of p_x = " << p_x << endl; int** p_p_x = &p_x; cout << "value of p_p_x = " << p_p_x << endl; int*** p_p_p_x = &p_p_x; cout << "value of p_p_p_x = " << p_p_p_x << endl; ***p_p_p_x = 15; // Change from *p_x = 15; cout << "value of x = " << x << endl; }

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; }

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5;

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5; ERROR! Missing operator* //Correction *pn = 5;

Error messages For pn = 5; MS VS: “error C2440: '=' : cannot convert from 'int' to 'int *‘. Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function- style cast”

Error messages For int *pm; *pm = 5; MSVS: warning C4101: ‘pm' : unreferenced local variable; warning C4700: uninitialized local variable 'pm' used.

Pointer and Array A name of an array holds the address of the first element in the array. Thus, an array is just a pointer const (cannot be changed). Pointers and arrays point to elements of the same type.

Example int a[5] = {1,2,3,4,5}; cout << "a[2] = " << a[2] << endl; cout << "a = " << a << endl; cout << "a+2 = " << (a+2) << endl; cout << "a+4 = " << (a+4) << endl; cout << "a+6 = " << (a+6) << endl; Output: a[2] = 3 a = 0012FF50 a+2 = 0012FF58 a+4 = 0012FF66 a+6 = 0012FF74

Example int a[5] = {1,2,3,4,5}; *(a+3) = 20; cout << " a[3] = " << a[3] << endl; Output: a[3] = 20

Example int a[5] = {1,2,3,4,5}; *(a+5) = 40; cout << "*(a+5) = " << *(a+5) << endl; Output: *(a+5) = 40

Pointer Arithmetic C/C++ allow pointer arithmetic: + and – for pointer and array, ++ and -- for pointer. If a and p are array and pointer respectively, the following operations are allowed:  p++, p--  a-1, a+20, p+i*j, p-3*j

Example int a[5] = {1,2,3,4,5}; int *p; p = &a[1]; cout << a[0] << ", " << p[-1]<< ", " << *(p - 1) << "," << *a << endl; cout << a[1] << ", " <<p[0]<< ", " << *(p) << "," << *(a + 1) << endl; cout << a[2] << ", " << p[1]<< ", " << *(p + 1) << "," << * (a + 2) << endl; Output: 1, 1, 1, 1 2, 2, 2, 2 3, 3, 3, 3

Example int b[5] = {12, 4, 16, 98, 50}; p = b; cout << (*p)++ << endl;// prints 12 cout << *p << endl;// prints 13 cout << *++p << endl;// prints 4 cout << *p << endl; // prints 4 cout << (*p)-- << endl; // prints 4 cout << *p << endl; // prints 3 cout << *--p << endl; // prints 13 cout << *p << endl; // prints 13

Pointer Operations Operations are adjusted with respect to the location of the element. Thus, p++ update p to point to the next element, no matter what the type of the element is.

Example double d[5] = {1.0, 3.0, 5.0, 7.0}; cout << "d[2] = " << d[2] << endl; cout << "d = " << (d) << endl; cout << "d+1 = " << (d+1) << endl; cout << "d+2 = " << (d+2) << endl; cout << "d+3 = " << (d+3) << endl; Output: d[2] = 5 d = 0012FEE4 d+1 = 0012FEEC d+2 = 0012FEF4 d+3 = 0012FEFC

Why Pointer Arithmetic Efficiency!

Example: strcpy char *strcpy (char *dst, const char *src) { int i = 0; for (i = 0; src[i] != '\0'; i++) { dst[i] = src[i]; } dst[i] = '\0'; return dst; }

Example: strcpy using pointers char *strcpy (char *dst, const char *src) { char *retval = dst; while (*dst++ = *src++) ; return retval; }

Pointer Arithmetic Cryptic (especially using together with ++ and --). Error-prone. Avoid using it.

Dynamic Memory Allocation The proper use of pointer is for dynamic memory allocation. Data structures of variable sizes can be constructed.

Sizes of data structures Many data structures require known sizes during their creations. Examples: arrays  int a[5];  double d[MAX];  Student s[20];

Dynamic Sizes However, many applications require data structures of dynamic sizes. Examples:  Tasks in an operating system  Number of bidders in an auction  People supporting first women vice president in Clear Lake

Memory Allocation The new and new [] operators allocate the necessary memory and return the address.

Example p = new int; *p = 8; cout << "p = " << p << endl; cout << "*p = " << *p << endl; p = new int; // memory leakage. *p = 20; cout << "p = " << p << endl; cout << "*p = " << *p << endl; Output: p = D8 *p = 8 p = *p = 20

Example 2: Where is the error? int main(){ int* p, q; p = new int; *p = 8; q = p; cout << "p = " << p << endl; cout << "q = " << q << endl; p = new int; *p = 20; cout << "p = " << p << endl; cout << "q = " << q << endl; cout << "*p = " << *p << endl; cout << "*q= " << *q << endl; }

Example 2 int main(){ int *p, *q; p = new int; *p = 8; q = p; cout << "p = " << p << endl; cout << "q = " << q << endl; p = new int; *p = 20; cout << "p = " << p << endl; cout << "q = " << q << endl; cout << "*p = " << *p << endl; cout << "*q= " << *q << endl; }

Output of Example 2 p = E0 q = E0 p = q = E0 *p = 20 *q= 8

Memory Allocation The memory structure for allocation through new operator is usually called heap. The C++ Runtime may not have enough memory. Example: p = new int[ ]; MSVS: total size of array must not exceed 0x7fffffff bytes

Handling insufficient error int * p = new int [n]; // if it fails an exception bad_alloc is thrown. p = new (nothrow) int [5]; if (p == NULL) { // error allocating memory. };

Memory De-allocation delete and delete [] are used to de-allocate memory for a single element and an array of elements respectively. new and delete replaces malloc and free in C. Example: int * p; … delete p;

Memory Corruption Unintended pointer operations may cause memory corruption.

Example char *s1; delete s1; cause warning (“uninitialized local variable 's1' used”) and runtime exception.

Example char *s2 = new char[5]; delete s2; cause runtime exception.

Example char *s3; s3[2] = 10; cause runtime exception.

Example char *s4 = new char[10]; s4[10] = 'c'; cout << "s4[10] = " << s4[10] << endl; Output: s4[10] = c No error! But memory is corrupted!

Example char *t1 = new char('a'); char *t2 = t1; cout << "*t2 = " << *t2 << endl; delete t1; cout << "*t2 = " << *t2 << endl; Output: (no error!) *t2 = a *t2 = ▌ No other pointer referring to memory before de- allocation.

Memory Leakage If the programmer forgets to de-allocate memory allocated by the new operator, memory leakage occurs. Memory leakage is a notorious problem in C/C++.

Example char *s = new char; *s = 'c'; cout << "s[0] = " << s[0] << endl; s = new char('d'); cout << "*s = " << *s << endl; *s = 'e'; cout << "*s = " << *s << endl; Output: (with leakage) s[0] = c *s = d *s = e

Delete Before Re-Allocation char *s = new char; *s = 'c'; cout << "s[0] = " << s[0] << endl; delete s; s = new char('d'); cout << "*s = " << *s << endl; *s = 'e'; cout << "*s = " << *s << endl;

Example: Leakage int *i = new int(10); int *j = new int(15); i = j; cout << "*i = " << *i << endl; *j = 20; cout << "*i = " << *i << endl; Output: *i = 15 *i = 20

Example: What is the problem? char *s1 = new char[15]; char *s2 = new char[15]; strcpy(s1, "clinton"); s2 = s1; strcpy(s2, s1); delete [] s1;

Example: No memory corruption char *s1 = new char[15]; char *s2 = new char[15]; strcpy(s1, "clinton"); s2 = s1; strcpy(s2, s1); delete [] s1; delete [] s2;

The Culprit s2 = s1; strcpy(s2, s1); The two statements have the same effect. char * is unique as an indirection is automatically done.

C/C++ Dynamic Memory Allocation Allow custom designed memory management. Possibly more effective! Error-prone. Programming intensive

Java’s Approach Java has no pointer. Thus, there is no pointer arithmetic. Java does have reference. Programmers usually do not handle memory management. “delete” is not a keyword in Java.

Automatic Garbage Collection Java performs automatic garbage collection.  Greatly simplify programmer’s work.  Much less error-prone.  Programmers lose control of memory management.  Performance may be an issue, especially for real-time applications.

Java Reference Java’s variable to objects are reference to the objects. Student s = new Student(“Yue”); A Student object (Yue) is created. s refers to the object.

C++ Objects and Pointers Pointers to objects are similar to other pointers.

Example: C++ Rect r1 (2,3); Rect r2 (10,20); cout << "r1 area: " << r1.area() << endl; cout << "r2 area: " << r2.area() << endl; r1 = r2; cout << "r1 area: " << r1.area() << endl; r2.setWidth(7); cout << "r1 area: " << r1.area() << endl; cout << "r2 area: " << r2.area() << endl; Output: r1 area: 6 r2 area: 200 r1 area: 200 r2 area: 140

Example: Comparing to Java Rect r1 = new Rect(2,3); Rect r1 = new Rect(10,20); r1 = r2; r2.setWidth(7);

Similar Effect in C++ Rect *pr1 = & Rect(2,3); Rect *pr2 = & Rect(10,20); cout area() << endl; cout << "*pr2 area: " << (*pr2).area() << endl; pr1 = pr2; cout << "*pr1 area: " << (*pr1).area() << endl; pr2.setWidth(7); cout << "*pr1 area: " << (*pr1).area() << endl; cout << "*pr2 area: " << (*pr2).area() << endl;

Questions and Comments?