Monday, 25 August 2014

Why stack and heap both are necessary?

Both has its unique features. Stack is more efficient for allocating and deallocating the memory. Then you may ask whats the need for the heap.

Although heap is slower in allocation and deallocation of memory, heap as a unique feature that it can be allocated and deallocated at any place the programmer wish.

Consier the following example:

int *a=new int(10000000);
float *b=new  float(1);

In the above case, either a or b can be deallocated first based on the need. But in the case of stack it will deallocate in orderly fashion


Where the memory for pointer variable will be allocated?

It is allocated on the stack. (Remember it is Pointer variable not the dynamic memory allocated)



Thursday, 14 August 2014

#include<cstdio>

void callme(int callme)
{
    printf("\n I was called!");
}

class test
{
    int main;
   
};


int main()
{
    int main;
    // float main; Gives error
    //test main; Gives error
   
    callme(4);
    return 0;
}

Do you think it gives error?

No it will not give compile error.

Do you think C++ is mad to allow like this?

Not so, The above statement is allowed because  function name main is in different scope and integer main is in different scope. But The following program gives the error

int main; // main occured 1st time in scope
int main() // Oh! main occured 2nd time in scope. Error
{
    int main;
    // float main; Gives error
    //test main; Gives error
   
    callme(4);
    return 0;
}

Here the 1st main (integer) and second main(function name) both in same scope. so it will give the error.

To better understood watch the following program.


#include<cstdio>


int main()
{
    int my_var;
    {
        int my_var;
        {
            int my_var;
            {
                int my_var;
            }
        }
    }
    printf("Hurray! No Compiler error")    ;
    return 0;
}

Can we create a object of same type for the class?


#include<stdio.h>


class fruit
{
    // fruit f; Not possible
    static fruit f; // Not part of class objects memory
    fruit *fp;
    fruit &fr;
}

int main()
{
    fruit apple;
}





No is is not possible at all.

Why class cannot data member object of self type?


The class cannot have the data member of self type. Because the size of the data member object was not known at the compile time. say suppose one int and self type in the class

32-bit machine

class fruit
{
    int a;// 4 bytes
    fruit f;// 4+ sizeof(fruit);
}

Now the size of the fruit object declared inside the class can be found using the following formula

4+sizeof(fruit)

What will be the result of sizeof(fruit) value?

It is combination of a int variable and fruit object f.

This loops continuosly as mentioned below

4 + sizeof( 4 + sizeof( 4+ sizeof(4 + sizeof(..................))))

 

So why pointer and reference objects are allowed?


It is allowed because its size is known. By defauly pointer size is common to all kinds of classes. So we need to worry and stack allocates the memory without any trouble.




Wednesday, 13 August 2014

What will be the output of following program?

#include<cstdio>

class empty
{
};

class subclass : public empty
{
}


int main()
{
    empty e;
    printf("Size of object created for empty class :: %ld",sizeof(e));
}


Answer


 Size of object created for empty class :: 1

Reason



Why size is not zero but 1?

Size is not zero. It is 1 by default. It is mandtory defined by standard to
keep the size of the object of empty class as 1. This is because to differentiate
the two differnt object created for that class. They will differentiate the two different classes with the memory address.


Size of derived while inherting the empty class

#include<cstdio>

class empty
{
};

class subclass : public empty
{

};        


int main()
{
    empty e;
    printf("Size of object created for empty class :: %ld",sizeof(e));
    subclass s;
    printf("\nSize of object created for derived class inherited to empty class:: %ld",sizeof(s));
   
}

Answer:


Size of object created for empty class :: 1
Size of object created for derived class inherited to empty class ::1

Reason:


Why size of object with inherited class also 1 instead of 2?

Actually nothing will be stored in the empty classes. The 1 byte is allocated to uniquely refer the object in the program. So 1 byte is required. There will be one memory address for the one byte and we can uniquely identify the object in the program.


Thanks to http://www.geeksforgeeks.org/ for intiating this idea

Thursday, 7 August 2014


#include<iostream>
using namespace std;

class node
{
 
 public:
  int value;
  node *next;
 
  node()
  {
   next =NULL;
   value = 0; 
  }
  
};

class List 
{
 public:
 
  List()
  {
   head = NULL;
  }
  
  node *head;
  
  bool insert_node(node *p_head,int p_value)
  {
  
   node *temp= new node();
   temp->next = NULL;
   temp->value = p_value;
   
   if(! p_head)
   {
    head = temp;
    return true;
   }
   
   while(p_head->next != NULL)
   {
    p_head = p_head->next;
   }
   
   p_head->next = temp;
   return true;
  }
  
  bool deduct_loop(node *p_head)
  {
   node *slow=NULL,*fast=NULL;
   if(p_head)
    slow = p_head->next;
   int a;
   a=5;
   if(p_head->next)
    fast = p_head->next->next;

 /* Slow(Tortoise) will move one step at each loop and fast(Hare) moves two steps at each loop */

 while(slow && fast && fast->next)
   {


// Hare captured the tortoise. Always hare only capture the tortoise

 if(slow == fast) 
    {
     cout<<"\n Loop deducted!\n ";
     return true;
    }
    slow=slow->next; // Move one step
    fast=fast->next->next; // Move two step
   }


 // Reached End of Loop because Fast(Hare) is null 


// If no loop then end of loop is always reached by hare 


 cout<<"\n Good Job! No Loop deducted!";
   return false;
  }
};


int main()
{

 List list;
 
 list.insert_node(list.head ,10);
 list.insert_node(list.head ,20);
 list.insert_node(list.head ,30);
 list.insert_node(list.head ,60);
 list.insert_node(list.head ,40);
 
 cout<<"\n Demonstrating Linked list without loop";
 
 list.deduct_loop(list.head);
 
 cout<<"\n Demonstrating Linked list with loop";
 (list.head)->next->next->next = (list.head)->next;
 
 list.deduct_loop(list.head);
 
 return 0;
}