Wednesday, March 23, 2011

C++ Interview questions

[ Matter here is just notes. No authenticity can be assured]

How Exception can be handled at constructor ?
- When exception happened in constructor, destructor will not called and memory leak would happened.
- "function-try-block".is the mechanism, which can use to handle the exceptions at constructor.
- Or separate initialization functions can be used, instead of constructor.

Few Links :
- http://www.cs.technion.ac.il/~imaman/programs/throwingctor.html
- http://www.open-std.org/jtc1/sc22/open/n2356/except.html#except
- http://www.parashift.com/c++-faq-lite/exceptions.html
Notes :
- - A pointer declared inside try block will not be accesible in catch block.


How to make a procedure/function invisible to outside a library ?
- Declare the procedure or function as static. So the function can be accessible inside the file/library only.
Eg. static int get_user_conf()

How is the static variable and static functions in C++
- Static variables are part of class, not with object
- Static variable should be intialised after class definition.
eg. int base::counter=10;
- A non static function also can access static variables.
Eg. void increment_counter()
{
base::counter++; // base:: qualifier required.
}
- A static class can access only static variables, part of the class.

What is placement new ?
- Placement new is a mechanism in c++ which allows user to allocate memory from pre-allocated buffer.
- Standard C++ supports placement new operator, which constructs an object on a pre-allocated buffer (memory, which is already alocated). This is useful when building a memory pool, a garbage collector or simply when performance and exception safety are paramount (there's no danger of allocation failure since the memory has already been allocated, and constructing an object on a pre-allocated buffer takes less time):
Example :
void placement() {
char *buf = new char[1000]; //pre-allocated buffer
string *p = new (buf) string("hi"); //placement new
string *q = new string("hi"); //ordinary heap allocation
}

2 comments:

  1. Hola,

    I am shocked, shocked, that there is such article exist!! But I really think you did a great job highlighting some of the key C++ Interview questions in the entire space.

    I want to use fgets to read a line of text and a word. The text is examined to find the word, but the word is never found even when the word is in the text.
    What am I doing wrong?

    Great effort, I wish I saw it earlier. Would have saved my day :)

    Merci Beaucoup,
    Irene Hynes

    ReplyDelete
  2. casino, sports bets, and reviews - DRMCD
    Play free casino games 강원도 출장안마 and get casino 안산 출장안마 bonuses 여주 출장마사지 from our games & apps for free! Our games 계룡 출장안마 include Blackjack, Roulette, American Roulette, Poker, Vegas 제천 출장마사지 Style Poker, Keno, Video Poker,

    ReplyDelete