Pointer is a variable that stores address. Every storage location of memory has an associated address. The address is a number that grows sequentially. For every program placed in memory, each variable or function in the program has an associated address.
The address of operator:
The address of operator or Reference operator is denoted by the notation &. When the user wants to get the address of a variable, then the reference operator & can be used. The operator & is used to find the address associated with a variable.
The syntax of the reference operator is as follows:
&variablename
This means that the address of the variablename is achieved.
Defining Pointer Variables or Pointer:
In order to define pointer variables, the programmer must use the operator denoted as * in C++.The symbol * when placed before a pointer, variable means that it as a pointer to.
While defining variables, the data type is placed before it. When the programmer wants to define the integer variable i it is written:
int i;
A programmer may think that to define pointer variable there is a separate data type. But this is not the case. There is no separate data type for pointer available. When a programmer defines a pointer variable, he or she can point to integer, float, char. The compiler must know the type of data the pointer is pointing to.
To define pointer variable is as follows:
datatype_of_ variable_pointedto* pointer_varaible;
For example:
char* ch;
This defines that ch is a pointer variable which points to char data type.
int* i;
This defines that i is a pointer variable which points to int data type.
float* f;
Void PointerPointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).
NULL Pointer:
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.
For example:
int* num;
num=0;
The above statement denotes num as an integer pointer type that does not point to a valid memory address. This shows that exforsys has a NULL pointer value.
The difference between void pointers and NULL pointers:
A Void pointer is a special type of pointer of void and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.
Need for Memory Management operators
The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.
In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language.
What are memory management operators?
There are two types of memory management operators in C++:
* new
* delete
These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
General syntax of new operator in C++:
The general syntax of new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.
Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;
In the above example, delete is a keyword and the pointer variable is the pointer that points to the objects already created in the new operator. Some of the important points the programmer must note while using memory management operators are described below:
The programmer must take care not to free or delete a pointer variable that has already been deleted.
The address of operator:
The address of operator or Reference operator is denoted by the notation &. When the user wants to get the address of a variable, then the reference operator & can be used. The operator & is used to find the address associated with a variable.
The syntax of the reference operator is as follows:
&variablename
This means that the address of the variablename is achieved.
Defining Pointer Variables or Pointer:
In order to define pointer variables, the programmer must use the operator denoted as * in C++.The symbol * when placed before a pointer, variable means that it as a pointer to.
While defining variables, the data type is placed before it. When the programmer wants to define the integer variable i it is written:
int i;
A programmer may think that to define pointer variable there is a separate data type. But this is not the case. There is no separate data type for pointer available. When a programmer defines a pointer variable, he or she can point to integer, float, char. The compiler must know the type of data the pointer is pointing to.
To define pointer variable is as follows:
datatype_of_ variable_pointedto* pointer_varaible;
For example:
char* ch;
This defines that ch is a pointer variable which points to char data type.
int* i;
This defines that i is a pointer variable which points to int data type.
float* f;
Void PointerPointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).
NULL Pointer:
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.
For example:
int* num;
num=0;
The above statement denotes num as an integer pointer type that does not point to a valid memory address. This shows that exforsys has a NULL pointer value.
The difference between void pointers and NULL pointers:
A Void pointer is a special type of pointer of void and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.
Need for Memory Management operators
The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.
In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language.
What are memory management operators?
There are two types of memory management operators in C++:
* new
* delete
These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
General syntax of new operator in C++:
The general syntax of new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.
Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;
In the above example, delete is a keyword and the pointer variable is the pointer that points to the objects already created in the new operator. Some of the important points the programmer must note while using memory management operators are described below:
The programmer must take care not to free or delete a pointer variable that has already been deleted.