Friday, October 23, 2009

Q. What are the differences between new and malloc?
A. Both malloc and new has been used for dynamic memory allocation. Followings are the differences:
1. You will have to typecast the return value of malloc, because malloc returns (void*), but new doesn't require any typecasting.
2. malloc is a C function whereas new is a C++ operator.
3. malloc allocate the amount of memory you ask and return it. But new allocate memory as well as it also call the constructor for which memory has been allocated.
4. On failure malloc return null pointer, but new never returns a null pointer, instead it throws an exception.


Q. What is the difference between delete and delete[]?
A. delete is used to destroy a single object. It will call the destructor of the object and free the memory.
delete[] is used to free the memory which was allocated using new []. delete [] will call the destructor for each object in array and free the memory.

Q. What are the advantages and disadvantages of using #define?
A. It takes up the least memory. Constants defined in this manner are simply placed directly into your source code, with no variable space allocated in memory.
When you use the preprocessor to create constants, you place control of those constants outside the scope of the compiler.
Suppose, #define PI 3.14159
No type checking is performed on the name PI and you can’t take the address of PI (so you can’t pass a pointer or a reference to PI).
PI lasts from the point it is defined to the end of the file;
The preprocessor doesn’t recognize scoping.
The preprocessor has no permission to access class member data. This means preprocessor
macros cannot be used as class member functions.
Q. Explain the Inline Function

A. C++ implements the macro as inline function, which is a true function in every sense. Any behavior you expect from an ordinary function, you get from an inline function. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the
overhead of the function call is eliminated.


Any function defined within a class body is automatically inline, but you can also make a non-class function inline by preceding it with the inline keyword.
you must include the function body with the declaration, otherwise the compiler will treat it as an ordinary function declaration. Thus,
inline int plusOne(int x);
has no effect at all other than declaring the function (which may or may not get an inline definition sometime later). The successful approach provides the function body:inline int plusOne(int x) { return ++x; }
You’ll almost always want to put inline definitions in a header file. When the compiler sees such a definition, it puts the function type (the signature combined with the return value) and the function body in its symbol table. When you use the function, the compiler checks to ensure the call is correct and the return value is being used correctly, and then substitutes the function body for the function call, thus eliminating the overhead.


The inline code does occupy space, but if the function is small, this can actually take less space than the code generated to do an ordinary function call (pushing arguments on the stack and doing the CALL).

Any function you define inside a class definition is automatically an inline.

Inlines & the compiler
To understand when inlining is effective, it’s helpful to know what the compiler does when it encounters an inline. As with any function, the compiler holds the function type (that is, the function prototype including the name and argument types, in combination with the function return value) in its symbol table. In addition, when the compiler sees that the inline’s function type and the function body parses without error, the code for the function body is also brought into the symbol table. Whether the code is stored in source form, compiled assembly instructions, or some other representation is up to the compiler.
When you make a call to an inline function, the compiler first ensures that the call can be correctly made. That is, all the argument types must either be the exact types in the function’s argument list, or the compiler must be able to make a type conversion to the proper types and the return value must be the correct type (or convertible to the correct type) in the destination expression. This, of course, is exactly what the compiler does for any function and is
markedly different from what the preprocessor does because the preprocessor cannot check types or make conversions.
If all the function type information fits the context of the call, then the inline code is substituted directly for the function call, eliminating the call overhead and allowing for further
optimizations by the compiler. Also, if the inline is a member function, the address of the object (this) is put in the appropriate place(s), which of course is another action the preprocessor is
unable to perform.


Limitation:

1. The compiler cannot perform inlining if the function is too complicated. This depends upon the particular compiler, but at the point most compilers give up, the inline probably wouldn’t gain
you any efficiency.


2. The compiler also cannot perform inlining if the address of the function is taken implicitly or explicitly. If the compiler must produce an address, then it will allocate storage for the function
code and use the resulting address. However, where an address is not required, the compiler will probably still inline the code.
Q. What are the advantages and disadvantages of using inline and const?
A.
Inline:
An inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.

you must include the function body with the declaration, otherwise the compiler will treat it as an ordinary function declaration.
Const:
The modifier const tells the compiler that a name represents a constant.
Named constant that is just like a variable, except that its value cannot be changed.
you can take the address of a const.
A const has a scope, just like a regular variable.
you can make it a local const inside a function and be sure that the name will not affect the rest of the program.
Q. What is the difference between a pointer and reference?
A.
1. A reference (&) is like a constant pointer that is automatically dereferenced.
2. A reference must be initialized when it is created. (Pointers can be initialized at any time.)
2. Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.)
3. You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That's because there's no need to test the validity of a reference before using it.
Q. When would you use a pointer? a reference?
A.
Use Pointer: whenever you need to take into account the possibility that there's nothing to
refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points).
Use Reference: whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything else.

Q. What does it mean to take the address of a reference?
A. Taking the address of a reference, does give you the address of the object referred to.

Q. What does it mean to declare a function or variable as static?
Q. What is the order of initialization for data?
Q. What is name mangling/name decoration?
Q. What kind of problems does name mangling cause?
Q. How do you work around them?
Q. What are the differences between a struct and a class in C++?
Q. What is a constructor initialize list?
Q. What is conversion constructor?
Q. What does mean by destructor as static?
Q. What do you understand by "resource acquisition is initialization" ?
Q. What is difference between operator new and the new operator?
Q. What is placement new?
Q. What happens if an exception is throws from an object's constructor?
Q. What happens if an exception is throws from an object's destructor?
Q. When would you choose to return an error code rather than throw an exception?
Q. What is partial specialization or template specialization?
Q. How can you force instantiation of a template?
Q. What is smart pointer?
Q. What is wrong with this statement?
std::auto_ptr ptr(new char[10]);
Q. It is possible to build a C++ compiler on top of a C compiler. How would you do this?

No comments: