Wednesday, 30 October 2013

Nested Classes... brief discussion

Nested class are used for hiding implementation details, exactly hiding the classes.
PROS:

class List
{
    public:
        List(): head(NULL), tail(NULL) {}
    private:
        class Node
        {
              public:
                  int   data;
                  Node* next;
                  Node* prev;
        };
    private:
        Node*     head;
        Node*     tail;
};
Here I don't want to expose Node as other people may decide to use the class and that would hinder me from updating my class as anything exposed is part of the public API and must be maintained forever. By making the class private, I not only hide the implementation I am also saying this is mine and I may change it at any time so you can not use it.

CONS:-
One more example,
Let's imagine the following code :

class A
{
   public :
      class B { /* etc. */ } ;

   // etc.
} ;
Or even:
class A
{
   public :
      class B ;

   // etc.
} ;

class A::B
{
   public :

   // etc.
} ;
So:
  • Privilegied Access: A::B has privilegied access to all members of A (methods, variables, symbols, etc.), which weakens encapsulation
  • A's scope is candidate for symbol lookup: code from inside B will see all symbols from A as possible candidates for a symbol lookup, which can confuse the code
  • forward-declaration: There is no way to forward-declare A::B without giving a full declaration of A
  • Extensibility: It is impossible to add another class A::C unless you are owner of A
  • Code verbosity: putting classes into classes only makes headers larger. You can still separate this into multiple declarations, but there's no way to use namespace-like aliases, imports or usings.

Conclusion:-
As a conclusion, unless exceptions (e.g. the nested class is an intimate part of the nesting class... And even then...), the flaws outweighs by magnitudes the perceived advantages.
On the pro-side, you isolate this code, and if private, make it unusable but from the "outside" class...
-Coutesy : stackOverflow
------------------------------------------------------------------------------------------------------------
Reference link:- http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr061.htm

Saturday, 19 October 2013

Notes on Memory management

Notes on Memory management- 

Note:-  These notes are just bullet points for refreshing the knowledge on Memory management. This is not written with the intention to explain any concept or for reference. 

1.       Main memory and the registers build into the processors itself are storage that CPU directly access.

2 .       Registers are generally accessible within one CPU cycle of CPU clock.

3.       Protection on memory space is provided by two registers-
             ·         Base registers(Relocation register)- holds the smallest legal physical address.
             ·         Limit address- specifies the size of range

4.       Protection of memory is accomplished by having the CPU hardware compare every address (generated in user mode, ie generated by user code) is within the above two register address. Any attempt to access any memory outside the range is marked as fatal error.

5.       The runtime mapping from virtual to physical address is done by hardware device called Memory Management Unit(MMU)

6.       Dynamic loading – a routine is not loaded into memory until it is called.
           ·         Relocatable  linker table – loads the desired routines into the memory and updates the program address tables to reflect this change.

7.       Static Linking – In which the system language libraries are treated like any other object  module and combined by the loader into the binary program image.
  In dynamic linking –the linking is postponed until execution time.

8. Version information of library is included in both the program and library. More than one version of library can be loaded into the memory and each program uses its own version information to decide which copy of library to use.

9. Roll in & roll out- the process of swapping in and swapping out process for execution.

10. Relocation registers  = base registers of the process.
Physical address = logical address + base(Relocation)  register value.

11. Transient Operating system code – The OS code that is not required too frequently.

12. Variable partition scheme (M.V.T) = Satisfy the request of size ‘n’ from the list of available free holes.
       There are many suggestions to above problem,
·         First Fit
·         Best fit
·         Worst fit


      13. External fragmentation – it exists when there is enough total memory space to satisfy the request but the available spaces are not contiguous.
      
      14. Internal fragmentation – it is the unused memory that is internal to the partition. This occurs when we take the approach of breaking the physical memory into fixed sized partition and allocate in unit based on block size.

      15. Compaction – shuffling the memory contents as to place all the free memory space in once large block.

      16.  Backing store – the memory where the swapped out process is kept.
      
       17. Paging is the memory management scheme that permits physical address space of the process to be non contiguous.
·         It avoids external fragmentation.
·         Compaction is not needed.
·         Also saves the memory management problem in backing store.

       18. Frames – Breaking physical memory into fixed size of blocks called frame.
             Pages – breaking logical memory into fixed size of blocks called pages. 
       Page size is defined by the hardware.
       
        19. Frame table –it is the data structure that has entry for each physical page frame, indicating that whether              the later is free or allocated and if allocated to which page of which process

        20.   For protection - protection bits are used that is associated with each frame.

               We have access type buts and valid and invalid buts associated in page table.