



































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































































For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234;
the compiler constructs the object being declared, which means that it:
**1. Allocates memory needed for that type of object
Read (§8.4 & §8.5)
#include
int main() { int i = 11, j = 22; double d = 3.3, e = 4.4; // pointer variables that: // store addresses of ints
Output produced: &i = 0x7fffb7f &j = 0x7fffb7f &d = 0x7fffb7e &e = 0x7fffb7e
int * iptr, * jptr; double * dptr, * eptr; iptr = &i; jptr = &j;
// value of iptr is address of i // value of jptr is address of j dptr = &d; // value of dptr is address of d eptr = &e; // value of eptr is address of e cout << "&i = " << iptr << endl << "&j = " << jptr << endl << "&d = " << dptr << endl << "&e = " << eptr << endl; }
// store addresses of doubles
Example
A dereferenced pointer is a variable and can thus be used in the same way as all variables of the type to which the pointer is bound
Recall the C++ function to exchange the values of two int variables:
void Swap(int & a, int & b) { int temp = a; a = b; b = temp; }
The values of two int variables x and y can be exchanged with the call:
Swap(x,y);
The first C++ compilers were just preprocessors that read a C++ program, produced functionally equivalent C code, and ran it through the C compiler. But C has no reference parameters. How were they handled?
Translate the function to
void Swap(int * a, int * b) { int temp = *a; *a = *b; *b = temp; }
and the preceding call to: Swap(&x, &y);
This indicates how the call-by-reference parameter mechanism works:
A reference parameter is a variable containing the address of its argument (i.e., it's a pointer variable) and that is automatically dereferenced when used.
Parameter
Argument
A Note About Reference Parameters:
Named Variables
Type * ptr; ptr
ptr = new Type ; ptr^ Memory for Type value
anonymous variables dptr
eptr
dptr
eptr
*dptr
*eptr
Note 1
Note 2: new must be used each time a memory allocation is needed.
double *dptr, *eptr; dptr = eptr = new double;
eptr = new double allocates memory for a double and assigns its
dptr = eptr simply assigns this same address to dptr; it does not allocate new memory.
*dptr = 3.14159; *eptr = 0.1423; cout << *dptr << " " << *eptr << endl;
What’s output? "Alias problem" dptr
eptr
Note 2
“dangling pointers"
Recall that arrays as we know them have their capacities fixed at compile time. For example, array a declared by double a[50]; has exactly 50 elements.
This is adequate if a fixed-capacity array can be used to store all of the data sets being processed. However, this is often not true because the sizes of data sets vary. In this case, we must either:
— Make array's capacity large enough to handle biggest data set — an obvious waste of memory for smaller data sets. or — Change capacity in array's declaration in source code and recompile (yuck!).
It would be nice if the user could specify the capacity of the array at run time and an array of that capacity would then be allocated and used. This is
possible in C++.
The operator new can be used in an expression of the form
where n is an integer expression, to allocate an array with n elements, each of type Type ; it returns the base address of that array.
This allocation occurs when this expression is executed , that is, at run-time, not at compile-time. This means that the user can input a capacity, and the program can allocate an array with exactly that many elements!
n-
Allocating an Array During Run-Time: new gets memory from the heap (as opposed to the stack , which holds global variables, activation records for each function, etc. for that program). This is the main difference between just saying array[n] and using this dynamic allocation (also passing of multidimensional arrays). new replaces free, malloc, and calloc in C.
Docsity.com
When the heap has been exhausted, new returns the value 0 (called the null address or null pointer ). Common to picture a null pointer variable using the electrical ground symbol:
Attempting to dereference a null (or uninitialized or void) pointer variable produces a segmentation fault.
So check whether a pointer is null before attempting to dereference it. double dptr = new double;… if (dptr == 0) // check if pointer has null value { cerr << "\n** No more memory!\n"; exit(1); } or use an assertion: (Remember to #include