1 / 18

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

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Recall Arrays • int numbers[5]; • The name of the array, numbers, contains the base address of the array. • numbers, is a pointer.

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

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

  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. Pointers to structs Pointers to structs follow the same rules: struct stateParty { int dems; int repubs; }; stateParty *p; p = new stateParty; dems repubs p ? ?

  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; dems repubs p 10000 7938

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

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

  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. 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. 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. Diagram the pointers electric *a, *b, *c; a = new electric; b = new electric; c = new electric; a = b; b = c; c = a;

More Related