Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory Allocation

Similar presentations


Presentation on theme: "Dynamic Memory Allocation"— Presentation transcript:

1 Dynamic Memory Allocation

2 Pointers Revisited What is variable address, name, value?
What is a pointer? How is a pointer declared? What is address-of and dereference operators? How can a pointer be assigned a value? How can a value of a memory location referred to by a pointer be accessed? What is a constant pointer? What is a pointer to a constant? What is the relationship between array name and a pointer? What is null pointer? What is lose pointer problem? Can pointers be used with objects? How can a method be invoked on an object using pointer? What is -> operator?

3 Why Dynamic Memory When a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated Not all memory can be allocated or efficiently allocated at the compilation time we are manipulating an set of data of arbitrary size what if we allocate an array too small? what if we allocate an array too large? static memory allocation - memory allocation at compile time dynamic memory allocation - allocation at execution time Dynamic memory allocation is more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow heap - the system structure where the memory is allocated heap is separate for every program and it is removed when the program finishes

4 new and delete new and delete - operations are used to dynamically manipulate memory new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declare pointer ip = new int; // ip points to integer variable Note: the variable has no name and the only way to access the variable is though pointer: cin >> *ip; *ip += 20; cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the variable delete - releases memory back to heap so that it can be reused delete ip; Note: the memory pointed to by ip goes away not the pointer variable itself dynamic variable - variable created with new automatic variable – local variables, parameters, temp. variables

5 Memory Leak Problem Note: the pointer that points to the dynamic variable is the only way to access this variable If the pointer is reassigned the dynamic variable is “lost”. This is called a memory leak problem int *ptr = new int; ptr = new int; // ERROR - memory leak Is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int;

6 Pointers and Arrays Array name is equivalent to: base_type * const
That is array name is a pointer that cannot be changed Array name points to the first element of the array Array name can be used as pointer Pointer can be used as an array name int a[10], *p1, *p2; p1=a; // where does p1 point now? p1[2]=5; // which element does p1 access a=p2; // is this legal? p1[20]; // is this legal?

7 Dynamic Arrays Arrays can be created dynamically just as non-aggregate variables new has to be passed the number of elements of the array int *p1, p2, num; p1 = new int[10]; The number of array elements need not be constant: cin >> num; p2 = new int[num]; new returns the pointer to the first element of the dynamically allocated array. This pointer can be used just like regular array name: p1[2]=42; Note: unlike regular array name this pointer can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1;


Download ppt "Dynamic Memory Allocation"

Similar presentations


Ads by Google