Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers

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

3 3 Recall Arrays int numbers[5]; The name of the array, numbers, contains the base address of the array. numbers, is a pointer.

4 4 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.

5 5 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 ?

6 6 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; p15.5

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

8 8 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

9 9 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 q 20

10 10 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.

11 11 Pointers to structs Pointers to structs follow the same rules: struct stateParty { int dems; int repubs; }; stateParty *p; p = new stateParty; p ?? demsrepubs

12 12 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; p 100007938 demsrepubs

13 13 Orphan structs If you assign one pointer to another, the struct previously pointed to by the latter is no longer accessible: q = p; p 69472352 demsrepubs q 69474000 demsrepubs

14 14 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; 1000 1001 1002 1003 1004 2000... Heap p 1000 1001 1002 1003 1004 2000... Heap p

15 15 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.

16 16 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.

17 17 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");

18 18 Diagram the pointers electric *a, *b, *c; a = new electric; b = new electric; c = new electric; a = b; b = c; c = a;


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers."

Similar presentations


Ads by Google