My Blog List

C++

Use the review part in " Crack Code"

                                                          

C++ vs Java                                       

1.Java runs in a virtual machine2.C++ natively supports unsigned arithmetic


      Either signed int or unsigned int, it is a sequence of binary bits in the memory. The difference is how software gives it semantic meaning. In C++ a binary sequence can be treat as a signed int or unsigned int. But Java can only treat it as signed int. Thus if you want to use unsigned int and related operation, Java will automatically use unsigned long to guarantee the range of value. 
3   In Java, parameters are always passed by value (or, with objects, their references are passed by value) In C++, parameters can be passed by value, pointer, or by reference
C++ doesn't give a strict requirement on how to implement reference. Compiler usually implements reference by using a pointer const. But whether they are identical internally, they are totally different usage. 1. Pointer can point to some else but reference cannot change once it has been binding with something.2. A reference must be binding with something when it is declared. A pointer can point to null but reference can not.3. Reference doesn't have algorithm operations like int *p ++

4   Java has built-in garbage collection5   C++ allows operator overloading

TODO: why doesn't Java support operator overloading?
6   C++ allows multiple inheritance of classes

                                                                      

 13.2  Compare Hash table vs an STL map   

The first in mind difference is STL map is implemented in Binary Search Tree that each operation runs in O(logn). However unordered_map is implemented in actual hash.
 The trade off for larger time complexity is STL map maintains the order in the mean time. That means it support operations related to the 
1. find min/max 2. print the elements in sorted 3. find the exact or the most closed (STL::map::lower_bound, find the first equal or bigger; STL::map::upper_bound, find the first 
 The implementation of a hash 
1. a good hash function to ensure uniform 2. a collision resolving 3. dynamically increase or decrease table size

                                                                   

13.3 Virtual functions in C++?                 
 Virtual functions is a very important part in C++ to represent Polymorphism. Let's talk about Virtual function first then step further to Polymorphism.
  • What's virtual function
The class function defined with virtual keyword is a virtual function. The difference between a virtual function and a normal class function is : 
if a function is defined as 'virtual' in the base class, the most derived class's implementation of function is called according to the actual type of the object referred to, regardless of the declared type of pointer or reference.


The result of program would be "Derived" rather then "Base".

  • Static binding & Dynamic binding
In static binding compiler will know each function's address(offset) at compile time. In dynamic binding, compiler can know the exact function called only during the run time. Thus need a additional structure to store this information called vtable for each class.

  • Insight of Virtual Function implementation
    • Each class X will have an virtual table for each base class which X derives from (C++ is multiple inheritance language).
    • Each object of class X will have a vptr variable pointing to the first virtual table of class X.
    • If a virtual function is not overridden by the derived class, the vtable of the derived class stores the address of the function in his parent class.
    • When a virtual function is called, the virtual table got by vptr is used to resolve the address function.
The vptr is usually stored at the very first position in a object. vptr is a pointer which points to the first virtual table. Within each virtual table, the pointer to virtual functions are stored. Consider the following program.



    • Because there are two vptr in the derived class, thus the size of derived class is 8 bytes (each pointer has 4 bytes in 32 bit system).
    • Explicit convert the vptr to a int pointer. We will find out the address of first virtual table.
    • Add 1 to the address of first virtual table, we will get the address of second virtual table.
    • Refer to the address of second virtual table, and plus 1 then we will get the address of second virtual function in the second virtual table
    • Use a function pointer then we can call this virtual function manually. Looks cool aha.





  • Trade-off of the virtual function
It takes more memory space because each class also needs to store a virtual table and each instance of the class needs to store a pointer to the virtual table. Also the function address is not know during the compile time rather it is known during the run-time which calculation is needed because it will first find the pointer to virtual table and then use the offset to find out the address of the virtual function.
  • Pure Virtual Function & Abstract Class
A pure virtual function is a virtual function whose deceleration is ended as '=0'. A pure virtual function makes its class as a abstract class which can not be instantiated until all the pure virtual function has been implemented.
  • Object Oriented Language
 Modern OO Language provides three capacities
    • Encapsulation
    • Inheritance
    • Polymorphism 
  • Polymorphism in Programming Language
In programming language 'polymorphism' means some code or operations behave differently in different contexts. Things like Template, overloading, overriding and virtual function are all representation of polymorphism.
  • Overloading & Overriding
    •  Overloading is when you define two function with same name in same class but distinct signature.
    • Overriding is when you define the function already defined in the parent class with the same signature
  • TODO: What programming experience do you have related to Polymorphism

                                                                   

13.4 Deep copy & Shallow copy                 
Shallow copy only copy the pointer to the data.
Deep copy copies the exact data.
  • TODO: The bug about memcpy in Database Project related to overlay

                                                                   

13.5 Keyword "volatile"                              

  • For compiler, keyword 'volatile' does two things.


  1. Don't cache the value in a temp register. Instead always read the value from memory 
  2. Avoid compiler optimizing the code seemed useless within current scope however may be modified from a outer operation

  • In practice keyword 'volatile' does two things.

  1. For hardware access like interrupt routine
  2. For inter- thread communication

  • C++11

According to the C++11 ISO Standard, the volatile keyword is only meant for use for hardware access; do not use it for inter-thread communication. For inter-thread communication, the standard library provides std::atomic<T> templates.


For multi-thread application programming, volatile doesn't  It does not provide any synchronization, it does not create memory fences, nor does it ensure the order of execution of operations. It does not make operations atomic. It does not make your code magically thread safe
  • TODO: Review CS295 about consistency of multithread programming and hardware arcitecture
  • TODO: Find out about memory fence, atomic operation in this post
  • http://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading

                                                                   

13.6 Name Hiding                                  
In C++, when you have a class with an overloaded method, and you then extend and override that method, you must override all of the overloaded 

methods

                                                                   

13.7 Why destructor  virtual                          
The constructor and destructor of a derived class will call all the based classes's corresponding constructor and destructor function. 
If a based class pointer points to a derived class object, the derived class's destructor won't be called if the destructor is not a virtual function.



                                                                   

13.7 Smart Pointer                                    
"Note that after Delete or Free operation, the value of a pointer may not change."
Use a class that contains a pointer to point the wanted block of memory which could avoid memory leak because when a smart point is out of scope the destructor will be called automatically to free the space. Within the class it stores a pointer to point to the block of memory and a integer which stores the reference number that points to this memory.


                                                                   

13.8 Iterator                                                
 C++ uses iterator to give a general way for programmer to access the container. It is a general interface which hides the detail of how to implement. It also provide a way for some common algorithm to be implemented beyond different types of  containers like sort, reverse function.

No comments:

Post a Comment

Enter you comment