Void - The Very Exceptional Type Pointers To This Type As in The Following Example Void PTR
Void - The Very Exceptional Type Pointers To This Type As in The Following Example Void PTR
Void - The Very Exceptional Type Pointers To This Type As in The Following Example Void PTR
C++ - Memory
We are going to present a complete, although not very useful, program that demonstrates the use of
both keywords.
We declare a variable called ptr which will point to the data of type int (the pointer's type is int
*); no value is assigned to this variable initially
We use the new keyword to allocate a block of memory sufficient to store a float array
consisting of 5 elements;
We make use of the newly allocated array (to be precise, a vector) and next we release it
using the delete keyword
We want you to pay attention to the fact that the pointer returned by new is treated as if it is an array.
Surprising?
The handling of the dynamic arrays (created during the run of the program) is no different than using
regular arrays declared in the usual way.
We owe it to the [] operator. Regardless of the nature of the array we can access its elements in the
same way.
#include <iostream>
using namespace std;
int main(void) {
float *arr;
arr = new float[5];
for(int i = 0; i < 5; i++)
arr[i] = i * i;
for(int i = 0; i < 5; i++)
cout << arr[i] << endl;
delete [] arr;
return 0;
}
The possibility of allocating the amount of memory which is really needed lets us write programs that
can adapt themselves to the size of the currently processed data. Let's go back to the bubble sort
algorithm that we presented previously. That program assumed that there were exactly 5 numbers to
sort. This is obviously a serious inconvenience. It may happen one day that we want to sort 10,000
numbers and sometimes hundreds of them. You can of course, declare an array of the maximum
predictable size but it would be unreasonable. A much better way is to ask the user how many
numbers will be sorted and then allocate the array of the appropriate size.
Let's try to start with a simpler example. In the following program we allocate an array containing 5
elements of type int, set their values, sum them up and, finally, release the previously allocated
memory.
int *tabptr, sum = 0;
tabptr = new int[5];
for(int i = 0; i < 5; i++)
tabptr[i] = i;
sum = 0;
for(int i = 0; i < 5; i++)
sum += tabptr[i];
delete [] tabptr;
The improved bubble sort program goes here We encourage you to compile and run the program
yourself.
#include <iostream>
using namespace std;
C++ - Memory
int main(void) {
int *numbers, how_many_numbers;
int aux;
bool swapped;
cout << "How many numbers are you going to sort? ";
cin >> how_many_numbers;
if( how_many_numbers <= 0 || how_many_numbers > 1000000) {
cout << "Are you kidding?" << endl;
return 1;
}
numbers = new int[how_many_numbers];
for(int i = 0; i < how_many_numbers; i++) {
cout << "\nEnter the number #" << i + 1 << ": ";
cin >> numbers[i];
}
do {
swapped = false;
for(int i = 0; i < how_many_numbers - 1; i++)
if(numbers[i] > numbers[i + 1]) {
swapped = true;
aux = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = aux;
}
} while(swapped);
cout << endl << "The sorted array:" << endl;
for(int i = 0; i < how_many_numbers; i++)
cout << numbers[i] << " ";
cout << endl;
delete [] numbers;
return 0;
}