Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
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.
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-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
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 OVERVIEW.
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.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
POINTERS. // Chapter 3 - Program 1 - POINTERS.CPP #include int main() { int *pt_int; float *pt_float; int pig = 7, dog = 27; float x = , y = 32.14;
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Pointers and Arrays Dynamic Variables and Arrays.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers 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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
Pointer Data Type and Pointer Variables II
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
Dynamic Memory Allocation
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation

Recollection of the Previous Lecture Why Data Structure Needed? –Programming and Data Processing requires efficient algorithms for accessing data in main memory and on secondary storage devices. –This efficiency is directly linked to the structure of the data being processed. What is Data Structure? –It is a way of organizing data that considers not only the items stored but also their relationship to each other.

Cont. Basic Data Types –Integer –Real –Boolean –Character Arrays –and some operations on arrays

What is a POINTER? A pointer variable contains an address. A pointer variable can be declared as follows: int *ptr; int a = 23, b; This declares ptr to be a (pointer) variable that can contain the address of an integer variable. Pointer

ptr = &b; /* Assigns ptr the address of b */ *ptr = a; /* The contents of the variable pointed by ptr becomes the value of a*/

Pointer c = *ptr; /* Assigns the value of the variable pointed by ptr (i.e. contents of b) to the variable c, (i.e. c becomes 23). */ cout << c << *ptr; /* prints c and contents of address ptr */ /*prints 23,23 */

Pointer  In C++, pointer arithmetic is scaled. Consider the following program: int a,*ptr; ptr = &a; if &a is equal to then ptr+1 will contain sizeof(int)

Pointers and Arrays Consider the following program: #include void main() { int a[10]; cout<<&a[0]<<endl; cout<<a; } The two cout will have the same output because the name of an array is actually representing the address of element 0 of the array. Therefore &a[i] is the same as a+i a[i] is the same as *(a+i )

Pointers and Arrays Example #include void main() { int a[5],i; for (i=0;i >a+i ; for (i=0;i<5;i++) cout <<*(a+i); }

Pointers and Arrays Two dimensional Arrays:  The case of two dimensional array is interesting. Every two dimensional array is stored in the memory row by row. From the row and column index (i and j), we can calculate exactly where an element a[i][j] is stored in the memory.  In a 2-D array, the name of the array represent the address of the first row.

Pointers and Arrays  If we know the address of the first element, then given the index i and j of any element, we can find its address as i*maximum number of columns +j.  Example - Next Slide

Pointers and Arrays #include void main() { char a[4][3]={{'m','a','t'},{'s','a','t'},{'h','a','t'},{'c',' a','t'}}; int i,j; char c;

Pointers and Arrays for (i=0;i<4;i++) { for (j=0;j<3;j++) { cout<<*((char*) a+i*3+j)); } cout<<endl; }

DYNAMIC MEMORY ALLOCATION  Memory is allocated for variables in two ways:  Static (or at compile-time)  Dynamic (or at run-time)

 In a program which declares an integer variable x, at compile time itself, memory locations of size enough for an integer ( 2 locations since size of an integer is 2 bytes) is reserved for x. Arrays are also known as static data structures since they also get their required memory allocated during compile time.. C-DATA TYPES AND DATA STRUCTURE CLASSIFICATION

Disadvantage : The problem with static memory allocation is that the memory usage may not be efficient Example : Consider the case of array marks to store the marks of a class of a maximum size 100. It is likely that in each semester the number of students may vary. In a given semester even if 25 students are there, 100 locations will still be reserved and 75 of them wasted. We can re-write the program each time with the array declaration exactly matching the number of students, but then the program is no longer a general one.

DMA  Dynamic memory allocation is in contrast with this. Memory gets allocated at the time of running the program and hence we can use memory to exactly meet our needs.  Allocated memory can be many types: Contiguous memory allocation: Allocation of adjacent memory locations Non-Contiguous memory allocation: Allocation of non adjacent memory locations Heap: Collection of all free memory locations available for allocation De-allocation: Releasing of allocated memory after use, to be added back to the heap.

DMA The new and delete operators do dynamic allocation and deallocation in much the same manner that the malloc() and free() functions do in C.  The new operator requires one modifier which must be a type.  The delete operator can only be used to delete data allocated by a new operator. If the delete is used with any other kind of data, the operation is undefined and anything can happen.

DMA Example : #include void main() { int *p; p=new int; *p=56; cout<<“Memory allocated at ”<<p<<endl; cout<<“Integer in memory="<<*p<<endl; }

DMA Another Example : #include void main() { struct pair { int x; int y; }; (*p).x is equivalent to p->x (*p).y is equivalent to p->y struct pair *p; p= new pair; (*p).x=56; (*p).y=47; cout x y; }

DMA Proper programming practice requires that all memory that is allocated to be freed after use In the previous programs, when the program finishes running the operating system frees all memory allocated.

DMA #include int main() { struct date { int month; int day; int year; }; int index, *pt1,*pt2; pt1 = &index; *pt1 = 77; pt2 = new int; *pt2 = 173; cout<<"The values are "<<index<<" " <<*pt1<<" "<<*pt2<<endl; pt1 = new int; pt2 = pt1; *pt1 = 999; cout<<"The values are "<<index<<" "<<*pt1<<" "<<*pt2<<endl; delete pt1; date *date_pt; date_pt = new date; date_pt->month = 10; date_pt->day = 18; date_pt->year = 1938;

DMA cout day month year<<endl; delete date_pt; char *c_pt; c_pt = new char[37]; strcpy(c_pt,"John"); cout<<c_pt<<endl; delete c_pt; return 0; }