1 C++ Dynamic Allocation Memory needs were determined before program execution by defining the variables needed. Sometime memory needs of a program can only be determined during runtime, or the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete. // Example of program using dynamic memory int main() int memsize, n; int *p; cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize]; if (p == NULL) cout << "Error: memory could not be allocated"; else for (n=0; n < memsize; n++) cout << "Enter number: "; cin >> p[n]; cout << "You have entered: "; for (n=0; n < memsize; n++) cout << p[n] << ", "; delete[] p;
2 How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, Memory in your C++ program is divided into two parts: 1. The stack: All variables declared inside the function will take up memory from the stack. 2. The heap: This is unused memory of the program and can be used to allocate the memory dynamically when program runs. Many times, you are not aware in advance how much memory you will need to store particular information in a defined variable and the size of required memory can be determined at run time. You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator. If you are not in need of dynamically allocated memory anymore, you can use delete operator, which de-allocates memory previously allocated by new operator. Allocating memory There are two ways that memory gets allocated for data storage: Compile Time (or static) Allocation Memory for named variables is allocated by the compiler Exact size and type of storage must be known at compile time For standard array declarations, this is why the size has to be constant Dynamic Memory Allocation Memory allocated "on the fly" during run time dynamically allocated space usually placed in a program segment known
3 as the heap or the free store Exact amount of space or number of items does not have to be known by the compiler in advance. For dynamic memory allocation, pointers are crucial // Program to demonstrate the use of operator 'new' // and 'delete' to dynamically allocate memory in C++ int main() char *sp; int size; cout << How many characters to enter: ; cin >> size; sp = new char[size+1]; if (sp) cout << "String? "; cin.getline(sp, size+1); cout << "You entered: " << sp << endl; else cout << "Request failed\n"; return 1; delete[] sp; // delete Examples on the usage of 'new' operator // An expression to allocate a single char new char // An expression to allocate a single int new int
4 // An expression to allocate a single double new double // An expression to allocate a single pointer to a float new float* // One char char* ptr_char = new char; // One int int* ptr_int = new int; // One double double* ptr_double = new double; Using 'delete' operator to delete allocated memory int* ptr = new int ; delete ptr ; // delete the allocated memory int* ptr = new int[10] ; delete [] ptr ; // delete array or memories // How to initialize value when allocating memory char* ptr_char = new char('a') ; int* ptr_int = new int(65) ; float* ptr_float = new float(1.234) ; C++ struct Keyword and typedef Keyword #include <cstring> typedef struct char title[50]; char author[50]; char subject[100];
5 int Books; book_id; int main( ) Books Book1; // Declare Book1 of type Book // book1 specification strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Print Book1 info cout << "Book 1 title : " << Book1.title <<endl; cout << "Book 1 author : " << Book1.author <<endl; cout << "Book 1 subject : " << Book1.subject <<endl; cout << "Book 1 id : " << Book1.book_id <<endl; Book 1 title : Learn C++ Programming Book 1 author : Chand Miyan Book 1 subject : C++ Programming Book 1 id : 6495407 Dynamic Memory Allocation for Objects Objects are no different from simple data types. For example, consider the following code where we are going to use an array of objects to clarify the concept: class Box public: Box() cout << "" <<endl;
6 ; ~Box() cout << "" <<endl; int main( ) Box* myboxarray = new Box[4]; delete [] myboxarray; // Delete array If you were to allocate an array of four Box objects, the Simple constructor would be called four times and similarly while deleting these objects, destructor will also be called same number of times. If we compile and run above code, this would produce the following result: