by shivender kumar bhardwaj pgt-computer sc the air … · second: pointer supports c++ dynamic...

47
BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR FORCE SCHOOL

Upload: others

Post on 04-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

BY SHIVENDER KUMAR BHARDWAJ

PGT-COMPUTER SC

THE AIR FORCE SCHOOL

Page 2: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

2 SHIVENDER KUMAR, TAFS

Page 3: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

First : Pointer provide the means

through which the memory location of variable can be directly accessed and hence it can be manipulated in the way as required.

3 SHIVENDER KUMAR, TAFS

Page 4: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Second: Pointer supports C++

dynamic allocation routines. Third: Pointer can improve the

efficiency of certain routines.

4 SHIVENDER KUMAR, TAFS

Page 5: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

STACK

HEAP

GLOBAL

VARIABLES

PROGRAM

CODE

STACK - This area is used for

function calls return address, argument and local variables

HEAP – This area is used for Dynamic Memory Allocation

GLOBAL VARIABLES - This area is used to store global variables where they exists as long as the program continues

5 SHIVENDER KUMAR, TAFS

Page 6: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

The Golden Rule of computer states that

anything or everything that needs to be

processed must be loaded into internal

memory before its processing takes place.

Therefore the main memory is allocated in two

ways,

STATIC MEMORY ALLOCATION

DYNAMIC MEMORY ALLOCATION

6 SHIVENDER KUMAR, TAFS

Page 7: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

In this technique the demanded memory is

allocated in advance that is at the compilation

time is called static memory allocation.

For example,

int s;

The compiler allocates the 2 bytes of memory

before its execution.

7 SHIVENDER KUMAR, TAFS

Page 8: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

In this technique the memory is allocated as

and when required during run time (program

execution) is called Dynamic memory

allocation.

C++ offers two types of operators called new

and delete to allocate and de-allocate the

memory at runtime.

8 SHIVENDER KUMAR, TAFS

Page 9: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

FREE STORE is a pool of unallocated heap

memory given to a program that is used by the

program for dynamic allocation during

execution.

9 SHIVENDER KUMAR, TAFS

Page 10: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

• Pointer variables are declared like normal

variables except for the addition of unary

operator * (character)

• The General Format of Pointer variable declaration is ,

where , type refers to any C++ valid data type and var_name is any valid C++ variable

10 SHIVENDER KUMAR, TAFS

Page 11: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

For example,

// integer pointer variable points to //another integer variable’s location.

// float type pointer variable points to //another float type variable’s location.

// character type pointer variable //points to another char type variable’s location.

11 SHIVENDER KUMAR, TAFS

Page 12: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Two special operators are used and are

with pointers.

operator is called as address of operator

which provides the address of operand.

Operator is called as at address and also

called as differencing which provides the value

being pointed by pointer.

12 SHIVENDER KUMAR, TAFS

Page 13: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

For example,

25 i

1050

1050 iptr

13 SHIVENDER KUMAR, TAFS

Page 14: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

A pointer variable must not remain uninitilized

since uninitialized pointer cause system crash.

Even if you do not have legal pointer value to

initialize a pointer, you can initialize it with

NULL pointer (ZERO POINTER).

14 SHIVENDER KUMAR, TAFS

Page 15: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Only two arithmetic operators, addition and

subtraction may be performed on pointers.

When you add 1 to a pointer, you are actually

adding the size of what ever the pointer

pointing at.

For example,

iptr++;

iptr--;

15 SHIVENDER KUMAR, TAFS

Page 16: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Only two arithmetic operators, and

may be performed on pointers.

When you add 1 to a pointer, you are actually

adding the size of what ever the pointer pointing

at.

For example,

Note: In pointer arithmetic all pointers increase

and decrease by the length of the data type they

point to.

16 SHIVENDER KUMAR, TAFS

Page 17: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

C++ offers two types of operators called new and

delete to allocate and de-allocate the memory at

runtime. Since these two operators operate upon free

store memory, they are also called as free store

operators

Syntax :

where pointer_variable pointer variable and datatype

is valid C++ datatype and new is operator which

allocates the memory (size of data type) at run time.

17 SHIVENDER KUMAR, TAFS

Page 18: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

For example,

Once a pointer points to newly allocated memory,

data values can be stored there using at address

operator

18 SHIVENDER KUMAR, TAFS

Page 19: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Initializing values at the time of declaration one can

rewrite like,

19 SHIVENDER KUMAR, TAFS

Page 20: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

CREATING DYNMIC ARRAYS 1D ARRAYS:

Syntax :

It will create memory space from the free store to

store 10 integers. Initilization while declaring dynamic

array is not possible but it is included in C++ compiler

ver 11.

20 SHIVENDER KUMAR, TAFS

Page 21: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

CREATING DYNMIC ARRAYS (2D ARRAYS):

One needs to be tricky while defining 2D array as,

int *val,r,c,i,j;

cout<<“Enter dimensions”;

cin>>r>>c;

val=new int [ r * c ] ;

To read the elements of the 2D array

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

cin>> val [ i *c + j ] ;

} 21

SHIVENDER KUMAR, TAFS

Page 22: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

The life time of the variable or object created by new is not

restricted to the scope in which it is created. It lives in the

memory until explicitly deleted using the delete operator.

Syntax:

FOR ARRAYS :

Syntax :

22 SHIVENDER KUMAR, TAFS

Page 23: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Improper use of new and delete may lead to memory

leaks. Make sure that the memory allocated through new

must be deleted through delete.

Otherwise this may lead to adverse effect on the system

23 SHIVENDER KUMAR, TAFS

Page 24: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

It is faster to use an element pointer than an index when

scanning arrays,

For 1D Array,

S[0]= *( S + 0 ) or *S or *(S)

S[1] = *( S + 1 )

S[2] = *( S + 2 )

24 SHIVENDER KUMAR, TAFS

Page 25: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

A string is a one dimensional array of characters

terminated by a null ‘\0’ . You have learnt in 11 std a string

can be defined and processed as,

char name [ ] = “POINTER”;

for ( I = 0 ; name [ i ] ! = ‘ \ 0 ’ ; i + + )

cout<<name[i];

Alternatively the same can be achieved by writing,

char name [ ] = “POINTER”;

char *cp;

for ( cp = name ; * cp ! = ‘ \ 0 ’ ; cp + + )

cout<< *cp ;

25 SHIVENDER KUMAR, TAFS

Page 26: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Another Example,

char *names [ ] = { “Sachin”, “Kapil”, “Ajay”, “Sunil”, “Anil” };

char *t;

t=name[1] ; //pointing to string “Kapil”

cout<<*t; // will produce out put “Kapil”

Similarly

T=name[3]; will point to?

26 SHIVENDER KUMAR, TAFS

Page 27: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

Constant pointer mean that the pointer in consideration will

always point to the same address . Its address ( to which it is

pointing to ) can not be modified.

For example,

int n=44;

int *const ptr = &n;

++(*ptr); // allowed since it modifies the content.

++ptr; // illegal because pointer ptr is constant pointer

27 SHIVENDER KUMAR, TAFS

Page 28: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

28 SHIVENDER KUMAR, TAFS

Page 29: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

29 SHIVENDER KUMAR, TAFS

Page 30: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

30 SHIVENDER KUMAR, TAFS

Page 31: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

//Implicit use of this pointer void Rectangle::input() { cin>>L>>B; } //Explicit use of this pointer void Rectangle::output() { cout<<this->L<<endl; cout<<this->B; }

31 SHIVENDER KUMAR, TAFS

Page 32: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

R1

R2

Input()

output() 2050

1025

1025 this

32 SHIVENDER KUMAR, TAFS

Page 33: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

R1

R2

Input()

output() 2050

1025

2050 this

33 SHIVENDER KUMAR, TAFS

Page 34: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

PREVIOUS YEAR BOARD QUESTIONS

34 SHIVENDER KUMAR, TAFS

Page 35: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

SHIVENDER KUMAR, TAFS 35

Page 36: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

2. Find the output of the following program:

# include <iostream.h>

void main ( )

{

int Track [] = {10,20,30,40,}, *Striker ;

Striker=Track ;

Track [1] + = 30 ;

cout<<“Striker”<<*Striker<<endl ;

* Striker - =10 ;

Striker++;

cout<<“Next @”<<*Striker <<endl ;

Striker+=2 ;

cout<<“last @”<<*Striker<<endl ;

cout<<“Reset To” <<Track [0] <<endl ;

} SHIVENDER KUMAR, TAFS

36

Page 37: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

37 SHIVENDER KUMAR, TAFS

Page 38: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

38 SHIVENDER KUMAR, TAFS

Page 39: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

39 SHIVENDER KUMAR, TAFS

Page 40: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

void main()

{ char *c="Romeo Joliet";

strcon(c);

cout<<"Text= "<<c<<endl;

c=c+3;

cout<<"New Text= "<<c<<endl;

c=c+5-2;

cout<<"last Text= "<<c;

}

40 SHIVENDER KUMAR, TAFS

6. Find the output of the following program:

Page 41: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

SHIVENDER KUMAR, TAFS 41

Page 42: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

42 SHIVENDER KUMAR, TAFS

Q.8

Page 43: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

43 SHIVENDER KUMAR, TAFS

Q.9

Page 44: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

44 SHIVENDER KUMAR, TAFS

Q.10

Page 45: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

SHIVENDER KUMAR, TAFS 45

Q.No. Answer

1 PoiNteRs Fun @ 10

2 Striker10

Next @50

last @40 Reset To0

3 110*56*

32*26*33

4 2@4@8

4#8#16#20#

5 Ur2GReat

r2GREat

2GrEat

grEat

6. Text= tMKCM@lMJGCR

New Text= CM@lMJGCR

last Text= lMJGCR

Page 46: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

SHIVENDER KUMAR, TAFS 46

Page 47: BY SHIVENDER KUMAR BHARDWAJ PGT-COMPUTER SC THE AIR … · Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines

47 SHIVENDER KUMAR, TAFS