Lec1 2
Lec1 2
Lec1 2
• Pointers
• Pointers with Arrays
• Dynamic Memory Allocation
1
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. 2
Cont.
3
Pointer
• 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.
4
Pointer
5
Pointer
In C++, pointer arithmetic is scaled.
Consider the following program:
int a,*ptr;
ptr = &a;
Therefore
&a[i] is the same as a+i
a[i] is the same as *(a+i )
7
Pointers and Arrays
Example
#include <iostream.h>
void main()
{
int a[5],i;
for (i=0;i<5;i++) cin >>a+i ;
for (i=0;i<5;i++) cout <<*(a+i);
}
8
Pointers and Arrays
9
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.
10
Pointers and Arrays
#include<iostream>
using namespace std;
void main() {
char a[4][3]={{'m','a','t'},{'s','a','t'},{'h','a','t'},{'c','a','t'}};
int i, j; char c;
for (i=0;i<4;i++) {
for (j=0;j<3;j++) { cout<<*((char*) a+i*3+j)); }
cout<<endl; }
}
11
DYNAMIC MEMORY ALLOCATION
12
C-DATA TYPES AND DATA STRUCTURE CLASSIFICATION
13
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. 14
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. 15
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 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.
16
DMA
Example :
#include<iostream.h>
void main()
{
int *p;
p=new int;
*p=56;
cout<<“Memory allocated at ”<<p<<endl;
cout<<“Integer in memory="<<*p<<endl;
}
17
DMA
Another Example :
#include<iostream.h>
void main() struct pair *p;
{ p= new pair;
struct pair (*p).x=56;
(*p).y=47;
{
cout<<p-
int x; >x<<endl<<p->y;
int y; }
};
(*p).x is equivalent to p->x (*p).y is equivalent to p->y
18
DMA
19
DMA
#include <iostream.h> pt2 = new int;
#include <string.h> *pt2 = 173;
int main() cout<<"The values are "<<index<<" "
{ <<*pt1<<" "<<*pt2<<endl;
struct date pt1 = new int;
{ pt2 = pt1;
int month; *pt1 = 999;
int day; cout<<"The values are "<<index<<"
int year; "<<*pt1<<" "<<*pt2<<endl;
}; delete pt1;
int index, *pt1,*pt2; date *date_pt;
date_pt = new date;
pt1 = &index;
date_pt->month = 10;
*pt1 = 77;
date_pt->day = 18;
date_pt->year = 1938; 20
DMA
cout<<date_pt->day<<"/"<<date_pt-
>month<<"/"<<date_pt->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;
}
21