Posts

Showing posts from August, 2017

new, operator new, Placement new, delete

New operator: C++ supports dynamic allocation  of objects using the new operator. This operator allocate memory for object from a pool call free store.The new operator calls the special function operator new. 1. We can allocate the memory for any data type. e.g int *p = new int; 2. We can also initialize the memory using new operator. e.g int *p = new int(50); 3. We can allocate the memory for array using new operator. e.g int *p = new int[10]; If enough memory is not available in the free store to allocate, the new request indicates failure by throwing an exception  of type std::bad_alloc and new operator returns a pointer. e.g #include <iostream> #include <new> int main() {     try {         while (true) {             new int[100000000ul];         }     } catch (const std::bad_alloc& e) {         std::cout << "A...

chain-of-responsibility

Image
1. Chain of Responsibility is a behavioral design pattern. 2. The basic idea behind this pattern is that a request or a command passes through a chain of objects until it is handled. 3. Objects involved in this pattern are of two types. Processing Objects and Command Objects. 4. Processing objects handle the commands given by command objects. 5. Each processing object knows what it can handle and passes the command to the next member in the chain if not handled. Flow diagram Class Diagram Example #include<iostream> using namespace std; class chain; class chain {     protected :         chain *next;     public:         void  addnext(chain *obj)         {             next = obj;         }         virtual void  perform(i...