Areas of heap memory are accessed via variables of pointer type […]. This kind of variable manages values of memory address type.
Arithmetic of pointers […] involves the operators use to access the areas of heap memory. Allocating of heap memory areas is done by functions and/or operators implemented in programming language.
In the below source program, there are exemplified specific mechanisms to work with pointers on heap memory areas in C++ programming language. The application was developed as a solution in Visual Studio, Win32 Console Application.
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; void main() { char sir[1000], *psir; cout << "Give the string: "; gets(sir); int dim; dim = sizeof(sir); cout << "Length of the memory area associated to the variable sir:" << dim << endl; dim = strlen(sir); cout << "Length of the given string:" << dim << endl; psir = new char[dim + 1]; dim = sizeof(psir); cout << "Length of the memory area associated to the pointer variable psir:" << dim << endl; dim = strlen(psir); cout << "Length of the string allocated in heap memory:" << dim << endl; strcpy(psir, sir); cout << "The string copied in heap memory:" << psir << endl; dim = strlen(psir); cout << "Length of the string copied in heap memory:" << dim << endl; char *pc; pc = psir; while (*pc != 0) { cout << *pc; pc++; } cout << endl; int i; do { cout << "Give the rank of the symbols will be modified: "; cin >> i; } while (i < 1 || i > dim); pc = psir + i - 1; cout << "Give the symbol: "; cin >> *pc; cout << "The string after the modification of the symbol on rank " << i << " is:" << psir << endl; delete[] psir; }
In the above example, the string variable sir acts as a buffer to store the characters entered from the keyboard. Retrieving string to string buffer sir is done via standard function gets. Size of string variable sir in number of bytes is given by the maximum number of elements specified in the declaration of the variable, 1000 bytes respectively. Size of string variable sir in number of ASCII symbols is given by the number of symbols entered from the keyboard, without the terminator of string, the digit 0.
Allocation of heap memory area that will store the string temporarily saved in the buffer sir is achieved through operator new. Number of elements of type char necessary to store the string of ASCII symbols is dim+1, where dim is the number of characters of the string from the buffer. Because the standard terminating digit is not considered as part of the string by function strlen, it is necessary as the allocated space for pointer variable psir to have an additional element of type char. If the length of the heap memory area is specified as dim, then when the de-allocating with delete operator is made, the compiler will generate a runtime error. Heap memory address obtained by the use of operator new is stored in the pointer variable psir. Size of pointer variable psir is 4 bytes (32 bits).
After allocating the heap memory area, its content has residual values. Length of the heap memory in number of ASCII symbols and managed by pointer variable psir is greater than dim+1 for security reasons. After copying the string from the buffer, memory area with the start address stored in psir has the same length (number of characters) as the variable sir. Also, on the position dim+1 it is placed the standard terminator for strings.
Reading the area of heap memory is done by pointer variable pc. pc is initialized with the start address stored in psir. The content of an ASCII symbol having the address stored in pc is accessed by the operator *, the expression being *pc. The above application displays the string symbol by symbol, passing to the next symbol using the post-incrementing operator applied on variable pc, pc++ respectively.
Changing a symbol of the string stored in the heap memory requires:
- Specifying the position of the symbol will be modified;
- Accessing the byte of the specified position by storing its address in the variable pc;
- Taking over from the keyboard the symbol and direct overwriting of the heap memory area to heading to change.
Checking correctness replacement of a symbol on a given position is achieved by displaying the modified string. The application is completed by de-allocating the area of heap memory associated to the character vector, using the operator delete.