C and C++ have something known as dynamic allocation of memory. In the following section we will describe how this techinique is used in C++ programs.
Dynamic allocation of memory allows us to create variables and arrays of whatever size we need when the program is being run as opposed to when we write it. In other words, we allocate the memory at run-time instead of at compile time. Let's start by modifying the example we gave in the previous section on pointers. Here is the new piece of code:
erp *erpBox; //Make a erp pointer called erpBox
erpBox = new erp; //Make a new erp variable in memory and have the
//pointer erpBox point at it.
erpBox->adc0u = 6; //Set adc0u = 6
erpBox->energy = 10.0; //Set the energy to 10 Mev.
int energy; //make an Integer variable called energy;
energy = erpBox->energy;//Set energy = 10 Mev(the value of erpBox.energy)
You probably noticed what was new in this example, namely the line:
erpBox = new erp
and the fact that we no longer needed to create an explicit variable (like the variable myErpBox in our previous example).
The line erpBox = new erp is the initialization for the pointer. When you first create a pointer it doesn't point at anything. We had to create an erp variable in memory. The above line does just this. It allocates a new piece of memory for an erp variable and makes erpBox point at it.
The important thing to realize is that this happens when the program is running (we say it happens at run-time). When you declare an explicit variable it is allocated before the program starts to run (at compile-time). On the other hand, using the new command lets us create as many erp variables as we need without having to know at the time we compile the program how many will be needed.
The syntax of the new command is as follows:
variable_type_pointer = new variable_type;
So some examples are:
int *p; float *f; p = new int; f = new float;
If we want to delete the variables which we created with the new command we can use the delete command. For example to free up the memory allocated by the erp variable we created earlier we would use:
delete erpBox; //Delete variable pointed to by erpBox
We can also use dynamic memory allocation to decide how big arrays and other data structures in memory are when we run the program.
In FORTRAN when we want to allocate an array we do something like this:
INTEGER MYARRAY(500)
Here is how we would do the same thing in C/C++ .
int myarray[500] //make an array
And here is how we would do it in C/C++ with a pointer using the new operator:
int *myarray //make a pointer called myarray pointing at an integer
myarray = new int[500]; //Allocate at an array of length 500 with
//first position being stored in *myarray
As you can see if we want to allocate an array using new we can just put an array subscript after the variable type. This looks similar to what we did with the erp pointer but now we have declared an array. In otherwords the pointer points to the first location of an array of 500. If you want the first element you could write
myarray[0]
and for the 3rd element myarray[2]. (Notice that C/C++ array subscripts start at 0 instead of 1).
This is all fine but what if we don't really know how big the array should be? If the array is holding streamer tube hits the length that it needs to be will be variable. In C++ , at the place where we used the number 500 we could have used a variable. This is how we can decide on the length of arrays at run time as opposed to compile time. Here is an example:
. . . Somewhere in program set variable sizeOfArray (from input or calculation) . . int *myarray; myarray = new int[sizeOfArray];
So depending on how big sizeOfArray winds up being when we run the program that is how big the array will be allocated to be.
The same thing could work for an array of erp variables:
erp *erpBox; int numErpHits = 6; erpBox = new erp[numErpHits]; //Make an array of erps of length 6 erp[1]->adc0u = 54; //set the adc of the second erp.
By now you have probably noticed the connections between pointers and arrays. Just to make it clear: a pointer points at a location in memory of some type. That location may be the first location of an array. In that case you can type pointer[i] where i is the member of the array you want. In fact, the compiler treats array names and pointers very similarly; you can use an array name as an argument to a function that expects a pointer.