Why the size of any emply class in C++ in of 1 byte
The compiler will generate 1 byte of memory to mark the existence of the class. This doesn’t answer WHY though. The reason is the language standard states that all classes must have a memory size of at least 1 byte so that the class doesn’t occupy the same memory space with another class. This is to prevent name mangling. i.e., if I declare a class A {};, the compiler will still generate an entry in its table to something called “A”. If behind that I declare another class, say class B, if A takes 0 bytes of memory, and B’s data gets written in the place where A was declared. In this case, an instantiation of A would take on the properties of B.
What is OCCI Library
Filed under: C++ Interview Questions, Linux Programming Interview Questions, Oracle Certification Exams Interview Questions
Oracle C++ Call Interface (OCCI) is an Application Programming Interface (API) that provides C++ applications access to data in an Oracle database. OCCI enables C++ programmers to utilize the full range of Oracle database operations, including SQL statement processing and object manipulation.
What is the difference between declaration and definition
Filed under: C Interview Questions, C++ Interview Questions
The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j–) //function body
cout << *;
cout << endl; }
What is stack unwinding in C++ ? Give an example for stack unwinding
Filed under: C++ Interview Questions, Microsoft Interview Questions
When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding. The automatic objects are destroyed in reverse order of their construction. (Automatic objects are local objects that have been declared auto or register, or not declared static or extern. An automatic object x is deleted whenever the program exits the block in which x is declared.)
If an exception is thrown during construction of an object consisting of subobjects or array elements, destructors are only called for those subobjects or array elements successfully constructed before the exception was thrown. A destructor for a local static object will only be called if the object was successfully constructed.
If during stack unwinding a destructor throws an exception and that exception is not handled, the terminate() function is called. The following example demonstrates this
#include
using namespace std;
struct E {
const char* message;
E(const char* arg) : message(arg) { }
};
void my_terminate() {
cout << "Call to my_terminate" << endl;
};
struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};
struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};
int main() {
set_terminate(my_terminate);
try {
cout << "In try block" << endl;
A a;
B b;
throw("Exception thrown in try block of main()");
}
catch (const char* e) {
cout << "Exception: " << e << endl;
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}
cout << "Resume execution of main()" << endl;
}
The following is the output of the above example:
In try block
In constructor of A
In constructor of B
In destructor of B
In destructor of A
Call to my_terminate
