1 / 22

Dynamic Memory Allocation Fall 2008

Dynamic Memory Allocation Fall 2008. Dr. David A. Gaitros dgaitros@admin.fsu.edu. Review of Pointers. Pointers are variables that contain memory addresses instead of values. A variable directly references a value A pointer indirectly references a value

cala
Download Presentation

Dynamic Memory Allocation Fall 2008

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. Dynamic Memory AllocationFall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

  2. Review of Pointers • Pointers are variables that contain memory addresses instead of values. • A variable directly references a value • A pointer indirectly references a value • Referencing a value through a pointer is called indirection • The reason why we still have pointers is because internal to the machine this is how items are referenced.

  3. Review of Pointers • Pointers can be declared to point to any object of any type including class objects. • You may initialize a pointer to zero (0), NULL, or to an address. • A pointer with the value of zero (0) or NULL points to nothing • Trying to reference or assign something to a pointer with an address of zero (0) or NULL will cause your program to be abnormally terminated (ABEND).

  4. Pointers • Example: int y=5; int *yPtr; yPtr = &y; cout << *yPtr << endl; • The unary operator “*”, or indirection operator returns the value of the object. • The unary operator “&” returns the address of an object.

  5. Pointers • Example int *countptr, count; float *xPtr, *yPtr; • The above declares countptr to hold an address that points to an integer. • count on the other hand holds the value of an integer. • Both xPtr and yPtr hold address that pointo to a floating point number.

  6. Pointers #include <iostream.h> int main(void) { int a; int *aPtr; a = 7; aPtr = &a; cout << “Address of a is “<< (&a) << endl; cout << “Value of a is “<< a << endl; cout << “Value of aPtr is “ << aPtr <<endl; cout << “Value of *aPtr is “ << *aPtr <<endl; cout << “ &*aPtr is “ << &*aPtr <<endl; cout << “ *&aPtr is “ << *&aPtr<< endl; return 0; }

  7. Pointers Output from previous program Address of a is 0x7fff1d834ee4 Value of a is 7 Value of aPtr is 0x7fff1d834ee4 Value of *aPtr is7 &*aPtr is 0x7fff1d834ee4 *&aPtr is 0x7fff1d834ee4

  8. Pointers • Pointer expressions and arithmetic • There are a limited number of pointer arithmetic operations • Incremented ++ • Decremented - - • Integer added ( + or +=) and of course subtraction (- or -=) • The arithmetic operations work a little differently on addresses. • Adding 1 to an address actually increases the value of the pointer by the size of the type.

  9. Pointers • Examples int v[10]; int *vPtr; // vPtr can be initialized // to point to the array v with // either of the following // statements vPtr = v; // or vptr = &v[0]; // Remember that by definition // an array is a pointer !

  10. Pointers • Let’s assume that the value (or address pointed to) of vPtr is 3000 (decimal). • If the size of the integers is 4 bytes and we add 2 to vPtr, it now has the value 3008. • If the size of the integers is 2 bytes and we add 2 to vPtr, it now has the value 3004. • In either case, vPtr now points to v[2]. if you were then to execute the lines of code vPtr ++ or ++vPtr then vPtr would point to v[3].

  11. Pointers • Example int x,v[10],*vPtr,*vPtr2; vPtr = &v[0]; // 1st element vPtr2 = &v[9]; // 10th element x = vPtr2 – vPtr; // x now contains the number of // array elements between v[0] // and v[9] which would be 10.

  12. Pointers • A pointer can be assigned to another pointer if and only if they are the same type. • Any pointer can be assigned a void * pointer • Comparison of pointers is only meaningful if they pointer to objects in the same array. • We commonly check to see if a pointer has the value of zero (0). In other words, does it point to a valid address.

  13. Pointers • Call by Value // The following is a call by // Value. A copy of the value // is passed in. Changing it // will not affect the original. void CallByValue ( int x) { x = x+1; }

  14. Pointers • Call by Reference // The following is a call by // Reference. The address // of the actual variable // is passed in. Changing it // will affect the original! void CallByReference ( int *x) { // Note the () around the *x. (*x)++; }

  15. Pointers • Sample calls int x, *y; CallByValue (x); CallByValue (*y); CallByReference(&x); CallbyReference(y);

  16. DMA • Remember that memory allocation comes in two flavors • Static (compile time): Size and type must be known at compile time. • Dynamic (run time): Memory allocated a run time. Exact sizes can be variable. Dynamic memory does not have a name so pointers must be used.

  17. DMA • Allocate with new operator. const int size=10; int * ptr = new int; double * nums = new double[size]; // The fist one assigns one new // integer. // The second declares a double pointer // num and assigns an array of 10.

  18. DMA • Clean up memory with delete delete ptr; // deallocate int delete [] nums; // deallocates // double array int *ptra, * ptrb; ptra = new int; ptrb = ptra; &ptra = 4; delete ptrb; cout << &ptra << endl; // What is the problem?

  19. DMA of Objects Fraction *fp1, *fp2, * flist; fp1 = new Fraction; fp2 = new Fraction(3,5); flist = new Fraction [20]; delete fp1; delete fp2; delete [] flist;

  20. DMA of Objects • Dot Operator vs. arrow- operator Faction fp1,flist1[10],*fp2,*flist2; fp2 = new Fraction; flist2 = new Fraction[10]; fp1.Show(); fp2->Show(); flist2[4].Show(); flist1[4].Show(); (*fp2).Show(); // Note: Array (static and dynamic // work the same way. Why?: // By definition, an array is a // pointer.

  21. DMA of Objects • Change size of Array: • Dynamically create a new array of a desired size. • Copy the old array’s contents into the new one. • Deallocate the memory of the old array. • Adjust pointers so they point to the location of the new array. // What is inefficient about this?

  22. DMA of Objects Fraction *MainPtr, *TempPtr; MainPtr = new Fraction [100] ; // Create new list TempPtr = new Fraction [200]; // Copy the contents of MainPtr to TempPtr; delete [] MainPtr; MainPtr = Temptr;

More Related