1 / 27

Exam 1 on July 18, 2005 – 10:00-11:40am

Exam 1 on July 18, 2005 – 10:00-11:40am. Pointers. 2004. 2012. 2000. x number ch. Addresses in Memory. When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable.

rainer
Download Presentation

Exam 1 on July 18, 2005 – 10:00-11:40am

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. Exam 1 on July 18, 2005 – 10:00-11:40am

  2. Pointers

  3. 2004 2012 2000 x number ch Addresses in Memory • When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable. int x; float number; char ch;

  4. Obtaining Memory Addresses • The address of a variable can be obtained by using the address-of operator ‘&’ int x; float number; char ch; cout << “Address of x is “ << &x << endl; cout << “Address of number is “ << &number<<endl; cout << “Address of ch is “ << &ch << endl;

  5. What is a Pointer Variable? • A pointer variable is a variable whose value is the address of a location in memory • To declare a pointer variable, you must specify the type of value that the pointer will point to. int *ptr; // ptr will hold the address of an int char *q; // q will hold the address of a char

  6. Using a Pointer Variable 2000 int x; x = 12; int *ptr; ptr = &x; 12 x 3000 2000 ptr • Because ptr holds the address of x, we say that ptr “points to” x

  7. Unary operator * is the deference operator 2000 int x; x = 12; int *ptr; ptr = &x; cout << *ptr; 12 x == *ptr 3000 2000 ptr • The value pointed to by ptr is denoted by *ptr

  8. 2000 12 5 x 3000 2000 ptr Using the dereference operator int x; x = 12; int *ptr; ptr = &x; *ptr=5; cout << x; • *ptr=5 changes the value at address ptr to 5

  9. 2000 A Z ch 3000 4000 2000 2000 q p Another Example char ch; ch = ‘A’; char *q; q = &ch; *q = ‘Z’; char *p; p = q

  10. Address pointer reference C++ Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double

  11. The NULL Pointer • There is a pointer constant 0 called the “null pointer” denoted by NULL in cstddef. • But NULL is not memory address 0. • It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer’s job to check for this. if (ptr != NULL) { … //ok to use *ptr here }

  12. Allocation of Memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time. DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new.

  13. Three Kinds of Program Data • STATIC DATA: memory allocation exists throughout execution of program. • static long SeedValue; • AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function. • DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete

  14. Using Operator new • If memory is available in an area called the free store (or heap), operator new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. • Otherwise, the null pointer 0 is returned. • The dynamically allocated object exists until the delete operator destroys it.

  15. 2000 ??? ptr Dynamically Allocated Data char *ptr; ptr = new char; *ptr = ‘B’; cout << *ptr;

  16. 2000 ptr Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr;

  17. 2000 ptr Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; ‘B’

  18. 2000 ??? ptr Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; delete ptr; • Delete deallocates the memory pointed to by ptr • The pointer is considered unassigned. The memory is returned to the free store.

  19. Dynamic Arrays • Array limitations • Must specify size first • May not know until program runs! • Must ‘estimate’ maximum size needed • Sometimes OK, sometimes not • ‘Wastes’ memory • Dynamic arrays • Can grow and shrink as needed

  20. ??? ptr 6000 6000 ptr Dynamic Array Allocation char *ptr; //ptr is a pointer variable that can hold //the address of a char ptr = new char[5]; //dynamically, during run time, allocates memory for 5 //characters and places into the contents of ptr their //beginning address

  21. 6000 6000 ‘B’ ‘B’ ‘u’ ‘y’ ‘e’ ‘e’ ‘\0’ ‘\0’ 6000 6000 ptr ptr Dynamic Array Allocation char *ptr; ptr = new char[5]; strcpy(ptr, “Bye”); ptr[1] = ‘u’;

  22. Deleting Dynamic Arrays • Allocated dynamically at run-time • So should be destroyed at run-time • Use “delete [] ptr;” • De-allocates all memory for dynamic array • Brackets indicate ‘array’ is there

  23. Memory Leak • A memory leak occurs when dynamic memory (that was created using operator new) has been left without a pointer to it by the programmer, and so is inaccessible.

  24. 8 ptr -5 ptr2 8 ptr -5 ptr2 Causing a Memory Leak int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2;

  25. A Dangling Pointer • Occurs when multiple pointers point to the same object and delete is applied to one of them.

  26. 8 ptr 8 ptr NULL ptr2 -5 ptr2 Leaving a Dangling Pointer int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; delete ptr2; ptr2 = NULL;

  27. array array array array idx=3 3.4 2.6 5.7 Dynamic Array Example Sort an array of float numbers from standard input. The first element is the size of the array. For example: 5 3.4 2.6 5.7 100.4 43.5 sortFromInput( ) { float *array; int size, idx; cin >> size; array = new float[size]; for (idx=0; idx<size; idx++) cin >> array[idx]; Sort(array, size); OutputSortedArray(array, size); delete[] array; }

More Related