Download presentation
Presentation is loading. Please wait.
Published byDortha Snow Modified over 8 years ago
1
© Janice Regan, CMPT 128, February. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Pointers
2
© Janice Regan, CMPT 128, 2007-2013 1 Dynamic and Automatic Variables Dynamic variables Created during program execution Destroyed during program execution Must remember to destroy all dynamic variable created Automatic variables Declared within function definition Not dynamic Automatically created when function is called Automatically destroyed when function call completes Function scope local variables are an example
3
Dynamic variables: creation C++ Use the keyword new int *myVariablePointer; myVariablePointer = new int; Variable can only be referenced using the pointer myVariablePointer Variable does not have an identifier (name) © Janice Regan, CMPT 128, 2007-2013 2
4
Dynamic variables: creation C Use function malloc() int *myVariablePointer; myVariablePointer = (int *)malloc( sizeof(int) ); Variable can only be referenced using the pointer myVariablePointer Variable does not have an identifier (name) © Janice Regan, CMPT 128, 2007-2013 3
5
Dynamic variables: destruction If you dynamically allocate a variable you must also destroy it after you are finished it. C use free C++ use delete © Janice Regan, CMPT 128, 2007-2013 4
6
Dynamic variables: creation C++ Use the keyword delete int *myVariablePointer; myVariablePointer = new int; … delete myVariablePointer; © Janice Regan, CMPT 128, 2007-2013 5
7
Dynamic variables: destruction C Use function free() int *myVariablePointer; myVariablePointer = (int *)malloc( sizeof(int) ); ……. free (myVariablePointer) © Janice Regan, CMPT 128, 2007-2013 6
8
7 Initializing pointers After you declare a pointer double *v1p, v1, *v2p, *v3p; It is good programming practice to initialize the pointer to NULL or some other particular value. v1p = (double *)malloc( sizeof(double) ); v2p = NULL; v3p = new int; Now consider the new (C++) and malloc() (C)
9
© Janice Regan, CMPT 128, 2007-2013 8 Automatic variables When you declare an automatic variable you provide an identifier memory is allocated for the variable at compile time (within the executable) The allocated memory is available till the program terminates The variable is referred to by the provided identifier
10
© Janice Regan, CMPT 128, 2007-2013 9 Memory Management Heap memory or freestore memory Reserved for dynamically-allocated variables All new dynamic variables consume heap memory Possible to consume all heap memory If all heap memory is consumed further allocations of dynamic variables will fail interaction with your computer’s virtual memory system may cause unexpected behavior Your program may not know when heap is exhausted
11
© Janice Regan, CMPT 128, 2007-2013 10 Dynamic variables When you create a dynamic variable you provide a pointer to a variable of the type of the variable you wish to create memory is allocated for the pointer to the variable at compile time You use new or malloc() to create the variable while your program is running (from heap storage) You are responsible for freeing or deleting the memory before your program terminates
12
© Janice Regan, CMPT 128, 2007-2013 11 C++ Using the new Operator Operator new dynamically creates variables of any type, for example double *v3p; v3p = new double; creates a new double variable Value of expression ( new double ) is a pointer to the new double variable. The new variable has no identifier The pointer to the new variable is assigned to (placed in) pointer to a double variable v3p.
13
© Janice Regan, CMPT 128, 2007-2013 12 Pointers in assignment statements double *v3p; v3p = new double; ? v3p Pointer Identifier Value of pointer variable 1004 v3p address Pointer Identifier Value of pointer variable ? Variable Identifier Variable Value
14
© Janice Regan, CMPT 128, 2007-2013 13 Pointers in assignment statements double *v3p; v3p = new double(77.2); ? v3p Pointer Identifier Value of pointer variable 1004 v3p address Pointer Identifier Value of pointer variable 77.2 Variable Identifier Variable Value
15
© Janice Regan, CMPT 128, 2007-2013 14 new Success – New Compiler Compilers following standards after 2002: If new operation fails: Program terminates automatically Produces error message Can still make your program continue after a failed allocation by Catching the exception (more later) Using a modified version of the NULL check
16
© Janice Regan, CMPT 128, 2007-2013 15 Checking new Success Can still test if null returned by call to new: int *p; p = new(nothrow) int; if (p == NULL) { cout << "Error: Insufficient memory.\n"; // exit if this is a critical allocation // otherwise continue }
17
© Janice Regan, CMPT 128, 2007-2013 16 Other examples of ‘new’ int *ap=NULL; double *bp=NULL; double *dp = NULL; char *cp = NULL; ap = new int; bp = new double; cp= new char[23]; dp = new double[100];
18
© Janice Regan, CMPT 128, 2007-2013 17 C Using malloc() Operator new dynamically creates variables of any type, for example int *v3p; v3p = (int*)malloc(sizeof(int)); creates a new integer variable Function malloc returns a void pointer to the new variable. You cast the pointer The new variable has no identifier The pointer to the new variable is assigned to (placed in) pointer to integer variable v3p.
19
© Janice Regan, CMPT 128, 2007-2013 18 Pointers in assignment statements int *v3p; v3p = (int*)malloc(sizeof(int)); ? v3p Pointer Identifier Value of pointer variable 1004 v3p address Pointer Identifier Value of pointer variable ? Variable Identifier Variable Value
20
© Janice Regan, CMPT 128, 2007-2013 19 Checking malloc() Success Test if null returned by call to malloc(): int *p = NULL; p = (int*)malloc(sizeof(int)); if (p == NULL) { cout << "Error: Insufficient memory.\n"; // exit if this is a critical allocation // otherwise continue }
21
© Janice Regan, CMPT 128, 2007-2013 20 Other examples of malloc() int *ap=NULL; double *bp=NULL; double *dp = NULL; char *cp = NULL; ap = (int *)malloc( sizeof(int)); bp = (double *)malloc(sizeof(double)); cp = (char *)malloc( 23*sizeof(char)); dp = (double *) malloc( 100*sizeof(double));
22
© Janice Regan, CMPT 128, 2007-2013 21 Memory management We have seen half of the tools needed for memory management in C++ We can create variables dynamically The other half is to be able to destroy the dynamic variables we have created after we are done with them. Destroying variables we have created is our responsibility, must assure that all dynamically allocated variables are destroyed so that the memory allocated to them can be returned to the heap To destroy dynamically allocated variables use the delete operator
23
© Janice Regan, CMPT 128, 2007-2013 22 C++ delete Operator De-allocate dynamic memory Returns memory to the heap Example: int *v1p; p = new int(5); … //Some processing… delete p;
24
© Janice Regan, CMPT 128, 2007-2013 23 De-allocate dynamic memory Returns memory to the heap Example: int *p; p = (int *)malloc( sizeof(int) ); *p = 5; … //Some processing… free(p); C free() Function
25
© Janice Regan, CMPT 128, 2007-2013 24 C++ creating and deleting arrays Creates an array of integers of size 5 After using the array deletes the memory that was used for the array int *p; p = new int[5]; … //Some processing… delete [ ] p;
26
© Janice Regan, CMPT 128, 2007-2013 25 Creates an array of integers of size 5 After using the array frees the memory that was used for the array int *p; p = (int *)malloc( 5* sizeof(int) ); … //Some processing… free(p); C creating and deleting arrays
27
© Janice Regan, CMPT 128, 2007-2013 26 Potential problems It is possible to Try to access memory you have already deleted: Lose all references to a piece of memory and be prevented from deleting that memory Means the heap is reduced in size for the rest or your program and for other programs run after your program completes. Memory that is dynamically allocated but not deleted may not be returned to heap
28
© Janice Regan, CMPT 128, 2007-2013 27 Memory Leaks Beware; do not loose your last reference to memory thatwas allocated using new or malloc() 53 88 23 v1p 53 88 v2p v1p v2p All reference to this memory has been lost. It is assigned to the program but cannot be used or deleted. It has 'leaked' from memory management within the program. Available heap has been decreased. v2p = new int; v2p = (int *)malloc(sizeof(int));
29
© Janice Regan, CMPT 128, 2007-2013 28 C++ Dangling Pointers delete p; // terminate allocation of memory // pointed to by p The program no longer owns the memory pointed to by p but p still points to that memory p is now a dangling pointer If p is dereferenced ( *p ) results are not predictable Avoid dangling pointers Assign pointer to NULL after delete: delete p; // p still points to the previously allocated memory p = NULL; // p now points “nowhere” attempts at // dereferencing will cause a predictable error.
30
© Janice Regan, CMPT 128, 2007-2013 29 C Dangling Pointers free(p); // terminate allocation of memory // pointed to by p The program no longer owns the memory pointed to by p but p still points to that memory p is now a dangling pointer If p is dereferenced ( *p ) results are not predictable Avoid dangling pointers Assign pointer to NULL after delete: free(p); // p still points to the previously allocated memory p = NULL; // p now points “nowhere” attempts at // dereferencing will cause a predictable error.
31
© Janice Regan, CMPT 128, 2007-2013 30 C Dangling Pointers free(p); // terminate allocation of memory // pointed to by p The program no longer owns the memory pointed to by p but p still points to that memory p is now a dangling pointer If p is dereferenced ( *p ) results are not predictable Avoid dangling pointers Assign pointer to NULL after delete: free(p); // p still points to the previously allocated memory p = NULL; // p now points “nowhere” attempts at // dereferencing will cause a predictable error.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.