180 likes | 329 Views
Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers. Multiple includes for .h files. Sometimes more than 1 source file needs to include a given .h file. The compiler may give an error, since the .h file is defined twice.
E N D
Data StructuresCSCI 132, Spring 2014Lecture 10Dynamic Memory and Pointers
Multiple includes for .h files Sometimes more than 1 source file needs to include a given .h file. The compiler may give an error, since the .h file is defined twice. Use a pre-processor directive to avoid this problem: #ifndef STACK_H #def STACK_H typedef .... class Stack { ... }; #endif
Recall Arrays • int numbers[5]; • The name of the array, numbers, contains the base address of the array. • numbers, is a pointer.
Declaring a Pointer • A pointer is a variable that can store the memory address of some piece of data. • Declaring a pointer: float *p; • p is a pointer that can store a memory address of a floating point variable.
Allocating Memory Declaring a pointer does not automatically give it a valid memory address: float *p; To allocate memory, use the key word: new p = new float; This is dynamic allocation of memory. p p ?
Accessing memory pointed to To access the memory pointed to by a pointer, use the indirection operator (*): *p = 15.5; Example: float *p; p = new float; *p = 15.5; cout << "The content of memory cell pointed to by p is " << *p << endl; p 15.5
General form for pointers Declaration of a pointer: type *variableName; Allocation of memory: new type or new type[n] //allocates n elements of specified type Accessing data with pointer: *pointerName
Illegal Pointer operations Because pointers hold addresses, you cannot assign data of type int or float or other data types to a pointer. The following is not allowed: int *p; float *q; p = 1000; // Not Allowed q = 15.5; // Not Allowed
Assigning a pointer One pointer can be assigned to another of the same type: int *p; int *q; p = new int; q = p; *p = 20; cout << *p << " " << *q << endl; p 20 q
Pointer arithmetic Pointer arithmetic allows accessing of items in an array: int *p; p = new int[5]; *p is value of p[0] *(p+i) is value of p[i] Each increment of p moves the pointer by enough memory to move to next item in the array.
Pointers to structs Pointers to structs follow the same rules: struct stateParty { int dems; int repubs; }; stateParty *p; p = new stateParty; dems repubs p ? ?
Accessing struct members Struct members are accessed with the selection operator: p = new stateParty; (*p) . dems = 10000; //The parentheses are essential! (*p) . repubs = 7938; Alternatively: p - > dems = 10000; p - > repubs = 7938; dems repubs p 10000 7938
Orphan structs If you assign one pointer to another, the struct previously pointed to by the latter is no longer accessible: q = p; dems repubs p 6947 2352 dems repubs q 6947 4000
The memory heap C++ maintains a storage pool of available memory called the heap. The keyword new allocates memory from this storage pool. p = new stateParty; Heap p 1000 1000 1001 1001 p 1002 1002 1003 1003 1004 Heap 1004 . . . 2000 . . . 2000
Returning memory to the heap • Use the keyword, delete, to return memory to the heap: • delete p; • p no longer points to a valid memory location • The memory is returned to the heap and can be reallocated later.
Things to be careful about WARNINGS: • If more than one pointer points to the same structure, if one is deleted, the other may still point to that location. However, C++ may reassign that memory, causing errors in the program execution. • Make sure you no longer need a structure before you return it to the heap. • Only use delete with pointers whose values were set by new.
Diagram each line struct electric { String20 current; int volts; }; electric *p, *q; p = new electric; strcpy( p - > current, "AC" ); p - > volts = 220; q = new electric; q -> volts = p - > volts; strcpy ( q - > current, "DC");
Diagram the pointers electric *a, *b, *c; a = new electric; b = new electric; c = new electric; a = b; b = c; c = a;