When variables are created in a C++ program (when the variables are in scope) the memory required to hold the variable is allocated from the program stack, and when the variable goes out of scope, the memory which was taken on the stack is freed. When memory is allocated dynamically (by the programmer) memory is taken from the heap (which consists of all of the computers virutal memory, which includes onboard RAM and space available on the disk drive. as in Delphi, with C++ memory is allocated dynamically using the new operator.As an example of static (stack) variables, and dynamic variables (on the heap) consider a simple integer x. The static variable would be declared in Delphi and C++ as follows...
Delphi...
var x : integer;
In C++ variables do no need to be declared in the 'var' segment at the beginning of a function (or unit) but can be declared anywhere in code. (If a variable is declared within a for loop, then the variable would only be in scope (on the stack) while the for loop executed, and then would vanish and go out of scope (off the stack) when the for loop exits. This idea of the scope of a variable being limited to the section of code where it was declared is an important difference between C++ and Delphi...
The same integer declared somewhere in C++...
int x;
Let's assume that the address of x was 1000. X is an integer, which currently means it is a 32 bit value so memory is allocated on the stack for four bytes1000, 1001, 1002, and 1003. The address of x would be 1000 (so when we use the variable x we are actually referring to the address 1000). When we assign a value to x we are assigning this value to the four bytes beginning at the address to which x acts as a pointer (the variable name).
We can also create variables dynamically in which case they do not go onto the stack but rather onto the heap. Because the program stack is limited, and the heap consists of all virtual memory (RAM and disk space) pointers and heap variables are useful in particular for large records (structures in C++) but can also be used for smaller variables.
To begin with I will create a simple variable on the heap, dynamically, using pointers...The variable ' px ' will be a pointer to an integer...
In Delphi....
var px : ^integer;
begin
new(px); {allocates 4 bytes for the integer);
....use variable etc.....
dispose(px); {frees the 4 bytes);
px := nil; {the pointer was 'dangling' - set it to the null value}To access the value pointed to by ' px ' we deference the pointer. Assume that x is also an integer and we wish to assign it the value pointed to by ' px ' (or vice versa).
x := px^;
px^ := x;In this simple example the address of x is 1000, the address of 'px' (the pointer) could be 2000. The value stored in px is 1000 which is the address of x since px points to x. We could also assign the pointer the address of an integer using the 'address of' operator...Assume that we have an integer variable y...
px := @y;
The pointer px is now pointing to the address of y. If we dereference px we get the integer value stored in y. If we check the value of px itself (we do not dereference the pointer) we get the address of y in this case ...and not the integer value stored in y.
In C++ the same sort of things would be accomplished as follows...
int* px = &x; ...the pointer px points to x using the address of x operator...
y = *px; ...the pointer is deferenced and now y = the integer value stored in x....
int w = *px; ...w now is set equal to the integer value stored in x...
The dereference operator (' * ') and the address of operator (' & ') perform two functions, depending on the context in which they are used.
int* px;
Px is a derived type. It is a pointer to an integer, so its type 'pointer to an integer' is derived from the type integer. It is also a dangling pointer (it points to nothing at the moment). We could set it to null in two ways...
px = 0; ...a null pointer...
int* px = 0; ...set to null when it was declared...
In a different context the symbol becomes the dereference operator...
y = *px; ...assume px points to x, this dereferences the pointer so that y now is equal to x...
The ' * ' operator can also mean 'multiply' depending on the context.
y = x * 2;
Thus these operators can have multiple meanings in a C++ program dependant on context and this is something to keep in mind...
Pointers are frequently used for large data structures. For example let's assume that a structure (record) exists which has been defined with the label 'arecord'. To dynamically create an instance of a variable of this type the new statement is used in the following way...
First we will declare a pointer to a record (which will then be used to dynamically allocate memory from the heap for the record.) The pointer will be given the label MyRecord and the asterik indicates that we are defining a pointer to a structure of the type 'arecord'
arecord* MyRecord;
MyRecord = new arecord;
You will notice that the variable declaration in C++ is 'backwards' compared to what you are used to in Delphi. An equivalent definition in Delphi which mimics the above could be implemented as follows...
type
arecordPtr = ^arecord; {a variable arecordPtr which is a pointer to arecord}
arecord = record {define the structure of the record}
{some records fields are here}
aqe : integer;
cost : float; {etc etc}
end;
{....}
var
MyRecord : arecordPtr; {MyRecord is a variable of the type which points to arecord}
{...}
begin
New(MyRecord);
{create an actual instance of arecord structure - this 'new' function does not create the pointer, but rather the record}
Later we can free the memory occupied by the record using the Dispose statement (which disposes of the record, not the pointer) followed by setting the now invalid pointer to nil...Dispose(MyRecord);
MyRecord := nil;Using the new statement in either Delphi or C++ dynamically allocates memory from the heap (leaving room on the stack for local variables) and the function returns a pointer to the memory which has been allocated. Note that pointers themselves are allocated from the stack, just like local variables (a 32 bit pointer would use 4 bytes from the stack, and then the memory required for the record would be allocated from the heap).
Let's assume that the type 'arecord' has two integer fields, one named 'amount' and another 'paid'
To access the record fields using a pointer in Delphi one uses the following syntax...
MyRecord^.amount := 10;
MyRecord^.paid := 5;In C++ the syntax for accessing the field of a structure pointed to by a pointer is as follows...
MyRecord->amount = 10;
MyRecord->paid = 5;
This can be compared to accessing the same fields from a record declared as a static variable using the dot operator (which would take up memory on the stack, rather than the heap).arecord MyRecord;
MyRecord.amount = 10;
In Delphi the dot operator works exactly the same...Here once again the record is a static variable stored on the stack, thus not requiring a pointer...var MyRecord : arecord;
begin
MyRecord.amount := 10;
As an alterrnative to a linked list the records could be stored as an array of pointers (rather than as an array of the records themselves...In Delphi we could declare the array as follows...
MyRecord : array[0..10} of arecordPtr;
The records themselves would have to be created using the new operator (since all we have created so far is an array of pointers).
new(MyRecord[0]);
The elements in each record could then accessed using a combination of array and pointer syntax as follows ...
MyRecord[5]^.amount := 10;
In C++ this would appear as follows...
arecord* MyRecord[10];
Once again each record pointed to by one of these array pointers would have to be individually created by using the new function.
MyRecord[4] = new arecord;
And the individual fields in a given record would once again be accessed in C++ using a combination of the array and pointer deferencing syntax...
MyRecord[5] -> amount = 10;
Keep in mind here that C++ is case sensitive. In Delphi MyRecord and Myrecord or myrecord are all the same variable (Delphi is not case sensitive) while in all three cases in C++ these would be different variables. As well in C++ pointers are not initialized to 'nil' but point at random places causing all those familiar pointer errors when running computer programs (the most common being 'This program has performed an illegal operation and will be shut down'). Pointers can be set to 'nil' in Delphi, or set to '0' in C++ to help avoid these problems (program code would have to ensure that the pointer is never used if it has the null value). In the following C++ example MyRecord is to declared to be a pointer to the structure of type 'arecord' and then is set to '0'...
arecord* MyRecord = 0;
The record pointer could also be declared to be pointing at a record...
arecord* MyRecord = new arecord;
A few other bits and pieces of pointer syntax in C++ as compared to Delphi...
Let's suppose that a pointer is pointing to an integer variable, and we wish to assign the value of this integer to another integer. In the example below we have an integer called 'amount' pointed to by a pointer 'amountPtr' and we wish to assign this integer value to another variable labelled 'storeAmount'.
First we will declare a pointer to an integer and then assign as the value of the pointer the address of the integer variable amount (note that this is the address of the variable amount, and not the value contained by the variable, two different things...a pointer requires the address of the variable which is being pointed to...)
int* amountPtr = &amount;
In Delphi
var amountPtr : ^integer;
begin
amountPtr := @amount;To assign the value of amount to StoreAmount ...
StoreAmount := amount; ... or in C++ ... StoreAmount = amount;
Dereferencing a pointer to do the same thing...
In Delphi..
StoreAmount := amountPtr^; {which means assign the value pointed to by amountPtr}
In C++...
StoreAmount = *amountPtr;
In both cases, because amountPtr points to amount, StoreAmount is assigned the value of amount...
You might notice the reference operator in a C++ program as well. A reference is simply an alias for an already existing variable. References must be initialized when they are declared and are treated like constants (you cannot assign a value to the reference after it has been declared...)
A reference can become an alias for any other variable. For example, rx will become the alias for x in this C++ example...
int x = 12;
int& rx = x;Add 4 to the reference variable rx and x also gets becomes 16. From this point on 'rx' and 'x' refer to the same variable (rx is a reference to x).
Let's assume that a function is declared which uses the 'var' keyword in Delphi.
function TForm1.somefunction(var x : integer):integer;
Now the use of the keyword 'var' tells the compiler to pass the integer to the function 'by reference'. In otherwords if the function changes the value of 'x' it will also be changing the original integer that was passed to the function. If the 'var' keyword is not used then the parameter is passed by value (no reference, or alias is created for the original variable). Any changes to the integer we named 'x' will not alter the original variable that was passed to the function.
In order to achieve the same effect in C++, we pass variables to a function by reference, so the function just declared above would like the following...
int somefunction(int&); ...the function declaration at the top of the unit...
int somefunction(int& x) ...the function header in the body of the unit...
{ ...the code }
Because x is declared to be 'passed by reference' (note the & operator) this has the same effect as using the var keyword in Delphi. An alias is created for whatever integer you pass the function. Any changes made to 'x' in the function will not be local but will also be changed in the original variable you passed the function. For example let's assume that a function takes an integer which we will call 'huey' and a calling routine passes the integer 'dewey' to the function. While the function is in scope, huey is now a reference to dewey, and changes to huey are changes to dewey. If however the function was declared without the var keyword (Delphi) and without the reference operator (in C++) then dewey will be passed by value. Only the value contained by the variable dewey will be passed to function but a reference to the address of the original variable (dewey) will not be passed. Changes to huey will not affect dewey since all huey recieved was the value contained by dewey and not a reference to dewey. This is the difference between passing by reference and passing by value, and it also illustrates one of the more common uses of the reference operator in C++..
Another thing to keep in mind is that when a variable in either language is passed by value this creates a copy of the variable for the reasons noted above, while if the variable is passed by reference no copy is made since the original variable will be used (and changed or altered). So then for really large structures a programmer could either create a global variable or the structure could be passed to functions by reference to avoid the overhead of having a function create a copy of the large structure, as would happen if it was passed by value. In addition, any changes made to this copy would vanish when the function completed and the copied variable went out of scope, which is probably not what you wanted (the original structure would be the same after the function executed, which would mean nothing was really accomplished).
If you wanted to be really safe, however, and do not want the function to alter a structure or large graphic you could pass the variable by reference but declare it to be a constant. This avoids the copying of a large structure, but also protects the original variable from unwanted alterations...
int somefunction(const x&);
The variable x in the above example is both a constant and passed by reference.
In programming code it is probably more typical to see references used as an alias for a pointer, and the end result is that coding is simplified, and thus looks like slightly different in the program code. This syntax describes a pointer which is accessed, like an object or stack declared record, using the dot operator rather the pointer syntax { -> }. There may be other differences internally, but externally this is about all the visible difference that exists between a 'reference' and a 'pointer'...A reference is a pointer declared with the ampersand rather than with the asterik, which points to another pointer to the structure. This sounds confusing but looks something like this....
arecord& MyRefRecord = *new arecord;
MyRefRecord is then declared as a reference to a pointer to arecord structure. The fields of this record are then accessed using the dot operator.
MyRefRecord.amount = 10;
This can be compared to accessing the field of MyRecord assuming it was a pointer to the record...
MyRecord->amount = 10;
A reference might be declared in slightly different ways in a computer program. For example let us assume that we already a variable MyRecord which was a pointer to a record, as just suggested above. We could access MyRecord fields using the -> operator or we could go a step further and create a reference to this pointer, the difference being that we will then access the fields of the record using the dot operator. To do this we do another step and declare a reference to the MyRecord pointer as follows...
arecord& MyRefRecord = *MyRecord;
Note that references must be assigned a value when they are declared (this is not true of pointers which can be assigned a value later, or have their value changed, and this seems to another functional difference between references and pointers...) MyRecord is a pointer and MyRefRecord is a reference which points to the pointer MyRecord, and then the fields of MyRefRecord will access this record using the dot operator as illustrated above. So then if the fields of a structure are accessed in a C++ program using the dot operator, it could be a simple record (a type of local variable stored on the program stack) or it could be a reference to a pointer to a record stored in the computer's memory heap (in this case the program listing will indicate this fact by the presence of the apersand ( & ) in the variable declaration.) You might notice that the ampersand is also the 'address of' function used on variables, but because the context is different, in one case it creates a referenced pointer and in the other it returns the address of a variable (when it is after an equals sign).
Referenced pointer...
arecord& MyRefRecord = *MyRecord;
Address of operator (we will assume that IntPtr is a pointer to an int, and amount is an int)..
IntPtr = &amount;
This places the address of the amount variable into the pointer IntPtr, as described above...
As I mentioned above, memory is dynamically allocated in C++ using the new operator. When a programmer allocates memory it is the responsibility of the programmer to free this memory (to avoid those memory leaks). Note that C++ is more simplified in this respect than simple C which uses malloc (assigns memory), calloc, realloc, and then free to manipulate program memory
One of the advantages of using 'new' and 'delete' (or 'new' and 'dispose' in Delphi) is that program stack memory is limited, but the heap in comparison is very large. For example pushing a large array onto the stack might not be a good idea, as your program will crash or perform unpredictably if stack memory is exhausted, and in the case of very large arrays or strings using a pointer and thus allocating and deallocating heap memory is a better choice, and the only difference is in becoming comfortable with working with pointers (and thus the heap) rather than using static variable definitions (and thus the stack). The memory used by local variables (variables declared in the static manner) is automatically managed by the program, with variables being disposed of when they go out of scope. However memory management becomes the programmers responsibility when using pointers to the heap and the new and delete (or dispose) functions.
For example, here the memory for the record is allocated, and then freed...
arecord* MyRecord = new arecord;
delete MyRecord;
MyRecord = 0;
If you need to delete a memory that was accessed using a referenced pointer the syntax looks like the following...The address operator ( & ) extracts the address of the pointer from the reference and then this pointer is used to delete the actual allocated memory...
delete &MyRefRecord;
In C++ it is also a very bad idea to delete an uninitialized pointer (that was never set to point to something) since this can cause problems in memory wherever the random pointer may be pointing to at delete time. It is also a bad idea to delete a pointer multiple times in different parts of the program. Setting the pointer to null allows for testing of these conditions For this reason a pointer in C++ should be set to point to something (trying to dispose of a pointer in Delphi that points to nothing generates an error).
arecord* MyRecord = 0;
delete MyRecord;
In the above example the pointer is set to Null (MyRecord := nil; in Delphi). I have a habit of setting all pointers to the null value when memory is disposed of to avoid crashing programs (this requires testing for a null value before using a pointer and taking appropriate steps if for some reason a null pointer is being accessed, since accessing a null pointer is enough in itself to generate an 'illegal operation' and a program error).
How it looks in Delphi...
dispose(MyRecord);
MyRecord := nil;Dynamic arrays can also be created and disposed of using new and delete (thus using heap rather than the stack).
Using Delphi 5 (and perhaps 4) creating dynamic arrays has been simplified. First the array declaration indicates that the array is gong to be dynamically allocated.
var
SomeArray : array of integer;Later in program code the array will be dynamically allocated using the 'SetLength' function. For example, suppose we decided that we needed 100 elements in the array...
SetLength(SomeArray, 100 * sizeof(integer));
The situation is a little more complicated in earlier versions of Delphi...see the page on Dynamic arrays for more information on creating and disposing of dynamic arrays in Delphi...
In C++ a dynamic array is created using a pointer and then disposed of using delete[] ... Note that the two square brackets are required when deleting the memory allocated to a dynamic array.
char* SomeBuffer = new char[1000]'
delete[] SomeBuffer;
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

