Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic memory allocation and Pointers Lecture 4.

Similar presentations


Presentation on theme: "Dynamic memory allocation and Pointers Lecture 4."— Presentation transcript:

1 Dynamic memory allocation and Pointers Lecture 4

2 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)

3 Pointers Have their own rules Operations Operations Treated differently to static data declarations Treated differently to static data declarations

4 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( ); function These are static declarations

5 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

6 Declaring pointer variables int* iptr = NULL; These are all the same int& b = *iptr;

7 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

8 number++;(*iptr)++; cin >> letter; cin >> *cptr; if(letter == 'A') if(*cptr == 'A') cout << 10 - number; cout << 10 - *iptr; for (number=O; number<5; number++) for(*iptr=0; *iptr<5; (*iptr)++) int number = 12; Is the same as *iptr = 12;

9 DMA Processes OS is first process loaded OS is first process loaded OS manages the loading of subsequent processes multiple processes can be loaded at the same time multiple processes can be loaded at the same time processes are loaded in lowest RAM address space available processes are loaded in lowest RAM address space available all memory above the highest process is called the heap all memory above the highest process is called the heap

10 CODE compiler executable code compiler executable codeDATA globally declared variables globally declared variablesSTACK local variables & parameters local variables & parameters return address return address usage varies during program execution usage varies during program execution DATA + STACK maximum 1Mb maximum 1Mb

11 Process can ask OS to: Reserve memory on the heap as and when required Reserve memory on the heap as and when required Release memory previously reserved on the heap Release memory previously reserved on the heap Operating system Knows memory has been reserved Knows memory has been reserved ONLY releases memory when asked by a process ONLY releases memory when asked by a process

12 Everything up to now has been static The compiler determines: Memory required for the data type Memory required for the data type Reservation for the process stack Reservation for the process stack Usually only 1mb reserved (more these days) Usually only 1mb reserved (more these days)

13 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* malloc (size_t size); void* operator new(size_t size); void* operator new(size_t size);new To be safe always use assignment to a pointer variable To be safe always use assignment to a pointer variable int *iptr = new int; int *iptr = new int;

14 For everything you new

15 You must

16 delete The delete keyword Requests that the values stored in memory are removed from the memory address Requests that the values stored in memory are removed from the memory address For safety anything you delete should be assigned a NULL value; For safety anything you delete should be assigned a NULL value;

17 Simple example #include using namespace std; void main() { int *iptr = NULL; iptr = new int; *iptr = 12; cout << *iptr << endl; delete iptr; iptr = NULL; } // IF you were to display the value of the pointer variable it would be a negative number

18 So back to functions void myfunc(const int* v1); Will not allow us to change v1 Will not allow us to change v1

19 What is this doing? void myfunc (int* & val);

20 Static wizards Wizard w; Wizard w(“Zedd”); w.setName(“Zedd”);

21 Pointers to a class Instantiation Wizard* w1 = new Wizard(); Wizard* w1 = new Wizard(); Wizard* w1 = new Wizard(“Richard”); Wizard* w1 = new Wizard(“Richard”);Use w1->setName(“Richard”); w1->setName(“Richard”); cout getName(); cout getName(); Memory release delete w1; delete w1;

22 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

23 What about this? int* ptrarray = myarr; cout << *(ptrarray - 100) << endl; myarr[-100] would surely crash! myarr[-100] would surely crash!

24 Dynamic arrays Wizard * w = NULL; Can hold a pointer to a wizard object Can hold a pointer to a wizard object Or an array of Wizard objects Or an array of Wizard objects w = new Wizard[6]; w[4].setName(“Nathan”); What about: w->setName(“Bob”); w->setName(“Bob”);

25 double* distance = new double[1000]; float* wage = new float[67]; int* age = new int[44]; Wizard* w = new Wizard[19]; delete[] distance; delete[] wage; delete[] age; delete[] w; delete[] distance, wage, age, w; syntactically correct will get past compiler syntactically correct will get past compiler only first array released properly only first array released properly causes memory leak causes memory leak delete[] distance, []wage, []age, []w; invalid syntax invalid syntax generates syntax error generates syntax error


Download ppt "Dynamic memory allocation and Pointers Lecture 4."

Similar presentations


Ads by Google