2. Basic Introductions About Pointers
Dynamic Memory Allocation
Memory Leaks and Dangling pointers
Data Structures
3. C++ allows two ways of accessing variables
› Name (C++ keeps track of the address of the
first location allocated to the variable)
› Address/Pointer
Symbol & gets the address of the variable that
follows it
Addresses/Pointers can be displayed by the
cout statement
› Addresses displayed in HEXADECIMAL
Data Structures
4. #include <iostream.h>
void main( )
{
int data = 100;
float value = 56.47;
cout << data << &data << endl;
cout << value << &value << endl;
}
Output:
100 FFF4
56.47 FFF0
Data Structures
56.47
100
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
value
data
5. The pointer data type
› A data type for containing an address rather than a data value
› Integral, similar to int
› Size is the number of bytes in which the target computer
stores a memory address
› Provides indirect access to values
Data Structures
Declaration of Pointer VariablesDeclaration of Pointer Variables
• A pointer variable is declared by:
dataType *pointerVarName;
o The pointer variable pointerVarName is used to point to a value of
type dataType
o The * before the pointerVarName indicates that this is a pointer
variable, not a regular variable
o The * is not a part of the pointer variable name
5
7. Data Structures
float data = 50.8;
float *ptr;
ptr = &data; 50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
8. Data Structures
float data = 50.8;
float *ptr;
ptr = &data;
FFF4
50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
9. A pointer can be initialized during declaration by
assigning it the address of an existing variable
float data = 50.8;
float *ptr = &data;
If a pointer is not initialized during declaration, it
is wise to give it a NULL (0) value
int *ip = 0;
float *fp = NULL;
Data Structures
11. • Static Allocation: Allocation of memory
space at compile time.
• Static Allocation means, that the memory for your variables is automatically allocated, You do
not have to reserve extra memory using them, but on the other hand, have also no control
over the lifetime of this memory. E.g: a variable in a function, is only there until the
function finishes.
• void func() { int i; /* `i` only exists during `func` */ }
• Dynamic Allocation: Allocation of memory
space at run time.
• Dynamic memory allocation is a bit different. You now control the exact size and the lifetime
of these memory locations. If you don't free it, you'll run into memory leaks, which may cause
your application to crash, since it, at some point cannot allocation more memory.
• int* func() { int* mem = malloc(1024); return mem; } int* mem = func(); /* still accessible */
Data Structures
12. Pointers need to be used for dynamic
allocation of memory
Use the operator new to dynamically
allocate space
Use the operator delete to later free this
space
Data Structures
20. When you dynamically create objects, you can
access them through the pointer which is
assigned by the new operator
Reassigning a pointer without deleting the
memory it pointed to previously is called a
memory leak
It results in loss of available memory space
Data Structures
21. Data Structures
Dangling Pointer example
int *ptr1 = new int;
int *ptr2;
*ptr1 = 8;
ptr2 = ptr1;
ptr1
8
ptr2
delete ptr1;
ptr1
ptr2