

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Pointer variable: A variable whose content is an address (that is, a memory address).
Program #1: Let us consider the following statements: int *ptr; int x; In these statements, ptr is a pointer variable of type int and x is a variable of a type int. Let us assume that memory location 001DFACC is allocated for ptr and memory location 001DFAD8 is allocated for x. Consider the following statements:
#include
//delete ptr; }
There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be a constant expression , and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new allows to assign memory during runtime using any variable value as size.
Every program has its own three memory segments, ie, code segment in which the compiled (object/machine) code resides, stack segment in which the local/temporary variables are stored and heap segment which is used for dynamic memory allocation.
Program #2: Implementation of Dynamic Array
#include
cout<<"Enter Number of Elements \n"; cin>>size; arr=new int [size]; cout<<"Enter "<<size<<" Elements \n"; for (int i=0;i<size;i++) { cin>>arr[i]; } cout<<"You Entered \n"; for (int i=0;i<size;i++) { cout<<arr[i]<<endl; } delete[] arr;