




















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!





















Pointers & Dynamic Arrays
Read: Chpt. 9
Recall that every data object is stored in some location in memory.
Example:
void func() { int x; int y = 10; … }
Hence, every variable in a program has two attributes: Location: Address of memory Content of the location: Value
Example: int y = 10;
y:
Accessing a Data Object :
A New Referencing Mechanism : One may use address of memory location (pointer variable) alone to access a data object. We won’t even need an assigned symbolic name for the variable.
Pointer: The memory address of a variable.
Pointer Variables: A variable that stores the actual memory address of a data object.
Remarks:
Declaring a Pointer Variable: Use a “*” following the type (or preceding the identifier which is to be the pointer variable):
Syntax: type_name pointer_name; type_name pointer_name; type_name * pointer_name;
Assignment Operator: The assignment operator = is used to assign the value of one pointer to another.
Example: int v, *p1, *p2;
v = 168; p1 = &v; // p1 is a pointer pointing to variable v *p1 = 268; // the variable pointed to by p1 holds 268
cout << “v = ” << v << endl;
p2 = p1; // v, p1, p2 reference the same variable *p2 = 368; cout << “v = ” << v << endl;
Output: v = 268 v = 368
Remarks:
NULL Pointer Value: Null is a special constant pointer value that can be assigned to a pointer variable of any type.
Example: int* p = NULL; double* q = NULL;
Q: How do we make pointers point to things?
Using &-Operator: int *p, v; v = 168; p = &v;
p? v
p v
Dynamic Allocation: To create new unnamed instances of variables during run-time.
Example: void dynamicallyAllocate() { double* p = new double; p = 10.7; int val = new int(7); cout << “Values: ” << *p << ‘ ’ << *val << endl; }
heap
Allocating & De-allocating Dynamic variables: Basic Memory Management:
Example: int *p, *q; p = new int(168); q = p; delete p; cout << *q; // What happens?
The pointers p and q are dangling pointers.
Scope considerations:
Example:
double* calculate(double a, double b) { // Return a pointer to a dynamically allocated // variable holding the product a*b. // Caller responsible for deleting the // dynamically allocated memory!
double* p = new double(a*b); return p; }
void aCaller() { double* p = calculate(3.7, -2.6); cout << “The answer is: ” << *p << endl; delete p; }
Defining Your Own Types:
Example: typedef int exam; typedef double average; … exam numOfExam; average examAve;
Note that numOfExam is of exam type and hence also is of int type. Similarly, examAve is of average type and hence also is of double type.
Remarks:
Array and Pointer Variables: In C++, an array variable is actually a (const) pointer variable pointing to the first indexed array variable.
Example: int a[10]; typedef int* IntPtr; IntPtr p;
Since a is a pointer variable that points to a[0], the following statement causes p to point to the same location as a. p = a; // This is legal!
It is illegal to assign a pointer value to an array variable. a = p; // This is illegal!
Pointer variable p can also be used as if it were an array variable.
Example: p = a; for (index = 0; index < 10; index++) cout << p[index] << " ";
Recall that in using an array in C++, the array size must be determined when the program is written.
Q: What if the size is unknown to the programmer? Estimate too large → Memory is wasted! Estimate too low → Program wouldn’t work!
Remedy: Using vectors or dynamic arrays.
Dynamic Array: Dynamic array variables are array variables that are created while a program is running.
Remarks:
Creating Dynamic Arrays using Pointer Type: typedef typeName* yourTypeName; yourTypeName arrayExample; arrayExample = new type_name[array_size];
Example: int numExam = 3; typedef double* avePtr; avePtr examAve; examAve = new double[numExam];
Remark: This will create an array examAve of size 3 with base type double. If numExam is an integer variable or an input, then the size of examAve will be determined during run-time!
Destroying Dynamic Arrays using delete Operator: delete [ ] arrayPtr;
Example: delete [ ] examAve;
Warning: If the bracket is omitted, only the first array variable pointed at by examAve will be deleted.
delete examAve; // delete only the first array element.
Example: The following program reads in a number of integers, store them in a dynamic array, sort them, and then output the integers in sorted order.