Transcript Lecture4
Dynamic memory
allocation and Pointers
Lecture 4
Pointers
Special type of variable declaration
Indicate we are pointing to an address in
memory
Allow us to use memory outside of the
stack
Numerically equivalent to a positive
integer (A memory address)
Pointers
Have their own rules
Operations
Treated differently to static data declarations
Memory used by a pointer
int myInt; //assign 4 bytes
char myChar; //assign 1 byte
char *cptr; //4 bytes
int *iptr; //4 bytes;
You can check this by using the
sizeof(<var>); function
These are static declarations
Assigning a pointer variable
A pointer variable can only be assigned an
address in memory
We use the address of operator : &
int *iptr = 5; //invalid
int a = 3;
Int *iptr = &a; //valid
Declaring pointer variables
int* iptr = NULL;
int * iptr = NULL;
int *iptr = NULL;
These are all the same
int& b = *iptr;
Dereferencing
float f = 8.3;
float* fptr =&f;
cout << fptr; // prints the address in
memory
cout << *fptr //prints the value held in fptr
cout << &fptr //prints the address of the
address in memory
int number = 12; Is the same as *iptr = 12;
number++;
(*iptr)++;
cin >> letter;
cin >> *cptr;
if(letter == 'A') if(*cptr == 'A')
cout << 10 number;
for (number=O;
number<5;
number++)
cout << 10 *iptr;
for(*iptr=0;
*iptr<5;
(*iptr)++)
DMA
Processes
OS is first process loaded
OS manages the loading of
subsequent processes
multiple processes can be
loaded at the same time
processes are loaded in lowest
RAM address space available
all memory above the highest
process is called the heap
CODE
compiler executable code
DATA
globally declared variables
STACK
local variables & parameters
return address
usage varies during program
execution
DATA + STACK
maximum 1Mb
Process can ask OS to:
Reserve memory on the
heap as and when
required
Release memory
previously reserved on
the heap
Operating system
Knows memory has
been reserved
ONLY releases memory
when asked by a
process
Everything up to now has been
static
The compiler determines:
Memory required for the data type
Reservation for the process stack
Usually only 1mb reserved (more these days)
It’s a brave new world
The new keyword asks the OS for the
required memory
In C this is done using the malloc function
void* malloc (size_t size);
void* operator new(size_t size);
new
To be safe always use assignment to a
pointer variable
int *iptr = new int;
For everything you
new
You must
delete
The delete keyword
Requests that the values stored in memory
are removed from the memory address
For safety anything you delete should be
assigned a NULL value;
Simple example
#include <iostream>
using namespace std;
void main()
{
int *iptr = NULL;
iptr = new int;
*iptr = 12;
cout << *iptr << endl;
delete iptr;
// IF you were to display the value of the pointer
iptr = NULL; variable it would be a negative number
}
So back to functions
void myfunc(const int* v1);
Will not allow us to change v1
What is this doing?
void myfunc (int* & val);
Static wizards
Wizard w;
Wizard w(“Zedd”);
w.setName(“Zedd”);
Pointers to a class
Instantiation
Wizard* w1 = new Wizard();
Wizard* w1 = new Wizard(“Richard”);
Use
w1->setName(“Richard”);
cout << w1->getName();
Memory release
delete w1;
Arrays and Pointers
A pointer can be used to point to a block of
memory such as an array
A string is a character array
A string is therefore also a character
pointer
What about this?
int* ptrarray = myarr;
cout << *(ptrarray - 100) << endl;
myarr[-100] would surely crash!
Dynamic arrays
Wizard * w = NULL;
Can hold a pointer to a wizard object
Or an array of Wizard objects
w = new Wizard[6];
w[4].setName(“Nathan”);
What about:
w->setName(“Bob”);
double* distance = new double[1000];
float* wage = new float[67];
int* age = new int[44];
Wizard* w = new Wizard[19];
delete[]
delete[]
delete[]
delete[]
distance;
wage;
age;
w;
delete[] distance, wage, age, w;
syntactically correct will get past compiler
only first array released properly
causes memory leak
delete[] distance, []wage, []age, []w;
invalid syntax
generates syntax error