Pointer is a derived data type. A variable of type of pointer is called pointer variable. A pointer variable hold address of memory location
Dynamic memory allocation
Allows to share data between functions
Increment the effiecient of the program
Not secured
Syntax of Pointers in C++ <data type> * variable – name ; int * * - - > indirection operator Dereferencing operator Value of given address What is address ?
int *a
16 bit compiler - - > 2 bytes 32 bit compiler - - > 4 bytes Example C++ program using Pointers # include <iostream.h> void main() { int *p; char *q; float *r; double *d; cout << sizeof(p) << endl; cout<<sizeof(q)<<endl; cout<<sizeof(r)<<endl; cout<<sizeof(d)<<endl; o/p : 4 4 BCZ turbo c++ 32 bit compiler 4 4
# include <iostream.h> void main() { int n1,n2; cout<< “\n input any two numbers”; cin>> n1 >> n2; int *p,*q; p=&n1; Q=&n2; int n3; n3=*p; // *p - - > value of 500 *p=*q; // *q - - > value of 502 *q=n3; cout<<”\n n1=”<<*p; cout<<”\n n2=”<<*q; } o/p : input - - > 10 20 After swapping value -> 20 10 Example # include <iostream.h> void main() { int n1,n2; cout<<”\n input any two numbers”; cin>>n1>>n2; int *p,*q; p=&n1; q=&n1; *p=*p+*q; // value of 500=value of 500 + value of 500 *q=*p-*q; = 10+20= 30 *p=*p-*q; cout<<”\n after swapint \n”; cout<<”\n n1=”<<n1; cout<<”\n n2=” <<n2; } o/p : input two values => 10 20 After swapping => 20 10
Allocating memory during the execution if runtime program is called as dynamic memory allocation
Advantage :
It avoiding wastage of memory
Increase efficiency of program
Size of memory allocated by program, known at compile time on before executing program. is called static memory allocation.
Size of memory allocated by program is not known at compile time but known at runtime is called dynamic memory allocation
new
delete c++ achieves dynamic memory allocation using 2 keywords
It allocates memory of n bytes in heap area and return address.
Heap area is dynamic memory area, which is manage by c++ program.
syntax : <pointer - variable – name >=new datatype[size];
size can be constant or variable
size is optional Texa code area Data area global variable Heap area Dynamic stack area local variable are use.
# include <iostream.h> void main() { int *a,*b,*c; a=new int; b=new int; c=new int; cout<< “\n input any two numbers”; cin>> *a>> *b; *c=*a-*b; cout<<”\n sum is”<<*c; } o/p : input any two numbers.
Delete unreserved memory which is reserved using new operator. delete<pointer-variabel>;
Malloc | new |
It is a function | It is a (operator) keyword |
It is reserved memory in one block. | It is allocates one or more than one block of memory. |
Malloc does required type casting | It doesn’t required type casting. |
It does not understand constructor | It understand constructor. |
Delete | Free |
It is a key word | It is a function |
understand destruction | It doesn’t understand destructor. |
# include <iostream.h> void main() { int *a, *b; a=new int; b=new int; cout<<” \n input any two numbers\ n”; cin>> *a>>*b; *a =*a+*b; *b=*a-*b; *a=*a-*b; cout<< “\n After swapping two numbers\n”; cout<< *a << “\t”<<*b; delete a; delete b; cout<< *a; cout<< *b; Pointer variable which holdsgf an address of memory location whose lifetime is over is called as Dangling pointer. Drawback:
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.