Skip to main content

Posts

Showing posts with the label Stack

One way : Heap or Stack allocation

Took my time to find solution for this and it actually made me realize how much i have to learn. Now question is how can we enforce the creation of objects whether on heap or stack. Creating object on Heap only. Make your destructor private and create a destroy function. Class A {  ~A(){} public:  A(){} void destroy() { delete this; } } Now if you can not create your object: A a; // error: 'A::~A()' is private A  a = new A(); Great. Now all the objects will be allocated to heap only. Making destructor private a better choice than constructor as we don't know number of constructor so early in programming. Creating object on stack While i knew that it has to do something with operator new, but could not figure out how can i create object on stack and return the memory back to class initialization. Though answer was simple, i had to google for that: Overload operator new, new[], operator delete and delete[] and make them private ;) class A { pr...

Stack

22. No of permutations with n numbers. (1/(n+1)2ncn). 23. A string with a’s and b’s and with one special character ‘x’. Find out whether its palindrome or not. 24. Implement queue by stack with enque and deque of order O(1); 25. Convert infix expression to postfix.