Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts

Wednesday, August 1, 2012

Can we have a Virtual Constructor?

A constructor assigns value to variables at compile time and the virtual table is created on the fly.
So it is impossible to a have a virtual constructor since the time a constructor is invoked the vtable won't be available in memory;

When does a Copy Constructor get invoked?

The following cases give rise to call a copy constructor:
 1. When an object is returned by value
 2. When an object is passed (to a function) by value as an argument
 3. When an object is thrown
 4 .When an object is caught
 5 .When an object is placed in a brace-enclosed initializer list

What and why are the operators that can't be overloaded?

We can't overload the 'sizeof', ':?', '::', '.' and '.*' operators in C++ since they take name as their argument whereas all other operators take value as argument.

Monday, July 23, 2012

What is the difference between a Reference and a Pointer?


1. References cannot be null, whereas pointers can;
2. References must be initialized as soon as they are created. On the contrary, it is not mandatory for pointers.
3. A pointer must be manually freed whereas a reference gets automatically dereferenced.
4. You can create the array of Pointer; but you can't create the Array of reference.
5. A pointer can point to many different objects during its lifetime; but a reference can only object during its lifetime.
6. You can declare a constant pointer; but you can't declare a constant reference though it is inherently constant.


Friday, July 20, 2012

How does compiler differentiate virtual and pure virtual function?

The compiler stores a NULL pointer to the pure virtual function in the V-Table and thus identifies it.

What is the difference between copy constructor and assignment?


A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but the basic difference are as follows :
  1. There is no need to test to see if it is being initialized from itself.
  2. There is no need to clean up (eg, delete) an existing value (there is none).
  3. A reference to itself is not returned.

Friday, October 7, 2011

What is the difference between process and thread?

Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space while processes run in separate memory spaces.

All the threads running within a process share the same address space, file descriptor, stack and other process related attributes. onthe other hand, each process has thier own virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution.

What is the difference between static and shared library?

In Unix, Static library has an extension of ".a" which is equivalent to ".lib" in Windows. on the contrary, dynamic or shared library has got ".so" as extension equivqlent to ".dll" in Windows.

Static libraries increase the size of the code in the binary. It is directly linked into the program at compile time. A program using a static library takes copies of the code from the static library and makes it part of the program. They're always loaded with the currently compiled version of the code. As the code is connected at compile time there are not any additional run-time loading costs.

Dynamic libraries are stored and versioned separately. Dynamic libraries aren't necessarily loaded; they are usually loaded when first called and can be shared among components that use the same library (multiple data loads, one code load). It allows you to replace the shared object with one that is functionally equivalent, but may have added performance advantages without needing to recompile the program that makes use of it.Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.

Saturday, September 17, 2011

What is 'Deadlock' and 'Livelock'?

Deadlock generally occurs when 2 threads try to acquire same shared resource. In this case each thread end up waiting for other to release lock and deadlock occurs. Deadlock is a common problem in multiprocessing where many processes share a specific type of mutually exclusive resource known as a software lock or soft lock. This could be a case when two threads are trying to write to or update record(s) in a database at a time.

A livelock, on the other-hand, occurs when a request for an exclusive lock is repeatedly denied because a series of overlapping shared locks keep interfering. It is an endless loop in program execution. This could be a case when two threads exit allowing each other to write to or update record(s) in a database.

Tuesday, September 13, 2011

What is inherited from the base class?


In principle, a derived class inherits every member of a base class except:
·         its constructor and its destructor
·         its operator=() members
·         its friends