I you are Finding best computer coaching in RDC, Raj nagar, Ghaziabad or you are Finding best computer programming institute in RDC, Raj nagar, Ghaziabad or Finding best computer teacher in RDC, Raj nagar, Ghaziabad.
We will provide you the experienced computer programming faculty in RDC RAJ NAGAR, and we have also best and latest programming courses in our Institute, such as:
C++ programming language with object oriented programming language
C programming language with Mouse programming too.
Data Structure with C and C++ programming
JAVA programming language with object oriented programming concepts (OOP)
C#.NET programming language with object oriented programming language
ASP.NET
ADO.NET
Website development using PHP, CSS, HTML, SQL Server.
C++ coaching in Rajnagar very soon
JAVA coaching in Rajnagar very soon
C#.NET coaching in Rajnagar very soon
PYTHON coaching in Rajnagar very soon
C language coaching in Rajnagar very soon
CCC coaching in Rajnagar very soon
O-Level coaching in Rajnagar very soon
Data structure coaching in Rajnagar very soon
C++ coaching in Raj nagar very soon
JAVA coaching in Raj nagar very soon
C#.NET coaching in Raj nagar very soon
PYTHON coaching in Raj nagar very soon
C language coaching in Raj nagar very soon
CCC coaching in Raj nagar very soon
O-Level coaching in Raj nagar very soon
Data structure coaching in Raj nagar very soon
20 + years of teaching in Computer Education
By Dr. Lalit Kishore Arora
In
the C++ programming language, there are two ways that memory gets allocated for
data storage:
1.Static
Memory Allocation
The memory for
defined variables is allocated by the compiler itself. The exact size and data type
of variable to store must be known at the time of compilation. Therefore, standard
array declarations need the constant size.
2.Dynamic
Memory Allocation
The memory allocated for the variables
during the run time. The dynamically allocated space usually placed in a
program segment known as the heap or the free store. In this allocation exact
amount of space or number of items does not have to be known by the compiler in
advance. Therefore, for dynamic memory allocation, pointers are crucial.
We
can dynamically allocate storage space while the program is running, but we
cannot create new variable names during the execution.
Therefore
to implement the concept of dynamic allocation, it requires two steps:
1.Creating
the dynamic space.
2.Storing
its address in a pointer (so that the space can be accessed)
In
the C++, new
operator is used to create the dynamic memory space.
De-allocation is
the process to clean-up the space being used for variables or other data
storage. The compile time variables are automatically de-allocated based on
their known extent (this is the same as scope for "automatic"
variables). It is the programmer's job to de-allocate dynamically created space,
to de-allocate dynamic memory space the delete
operator is available in C++ programming language.
6.5.1
new and delete operators
In
the C++, for allocation and de-allocation of memory dynamically,new and delete operators
are used.
To
allocate space dynamically, use the unary operator new, followed by the type
being allocated.
new int;// dynamically allocates an int
new double;// dynamically allocates a double
These
above statements are not very useful by themselves, because the allocated
spaces have no names, and the new operator returns the starting address
of the allocated space, and this address can be stored in a pointer:
int * p;// declare a pointer p
p = new int;// dynamically allocate an int and load address into p
double * d;// declare a pointer d
d = new double; // dynamically allocate a
double and load address into d
Consider
the following example for dynamically allocation and de-allocation of the
variables.
Example
6.4 : Program to allocate and de-allocate the dynamic space using pointers.
#include
< iostream.h >
void
main()
{
int *p1;
p1 = new int;//
dynamic memory allocation
*p1 = 300;
cout << “\n Value inside the p1 = ” << *p1;
delete p1;//free
the dynamically allocated memorys
}
OUTPUT
Value
inside the p1 = 300
Using
the new and delete operators, we can create arrays at runtime by dynamic memory
allocation and de-allocation. The general form for doing this is:
p_var = new array_type[size];
size
specifies the number of elements in the array.
To
free the memory of an array we use:
delete[] p_var;
Let
see the given example for the above explanation.
Example
6.5 : Program to create dynamic array using pointers.
#include
< iostream.h >
void
main()
{
int *p;
p = new int[10];//
dynamic allocation for array of 10 elements