Page 248 - DCAP404 _Object Oriented Programming
P. 248
Unit 11: Pointers and Dynamic Memory Management
else Notes
return(&y);
}
if input = 7 13
then, output : The bigger value is 13
Self Assessment
Fill in the blanks:
8. Pointer is a variable, which contains …………………….. of a variable.
9. A function is a user defined operation that can be ………………….. by applying the call
operator (“( )”) to the function’s name.
10. A pointer to a variable holds its memory address using which the memory area storing
the data value of the variable can be …………………… accessed.
11.5 Dynamic Memory Management
Dynamic memory is allocated by the new keyword. Memory for one variable is allocated as
below:
ptr=new DataType (initializer);
Here,
1. ptr is a valid pointer of type DataType.
2. DataType is any valid c++ data type.
3. Initializer (optional) if given, the newly allocated variable is initialized to that value.
Task Dynamic memory allocation is the allocation of memory at “run time”. Discuss.
Example:
//Example Program in C++
#include<iostream.h>
void main(void)
{
int *ptr;
ptr=new int(10);
cout<<*ptr;
delete ptr;
}
LOVELY PROFESSIONAL UNIVERSITY 241