Answer: A pointer which is pointing to an object which no longer exists is a dangling pointer.
MyClass* p(new MyClass);
MyClass* q = p;
delete p;p->DoSomething(); // Watch out! p is now dangling!
p = NULL; // p is no longer dangling
q->DoSomething(); // Ouch! q is still dangling!
2)Why are member functions not virtual by default?
Answer: Because many classes are not designed to be used as base classes. Also, objects of a class with a virtual function require space needed by the virtual function call mechanism - typically one word per object. This overhead can be significant, and can get in the way of layout compatibility with data from other languages.
3) Should I use NULL or 0?
Answer: In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
4)What is faster ++i or i++, where i is an interger variable?
Answer: The answer to this lies in the fact, how they used. With ++i(PreIncrement) the variable is incremented and new value is returned. So it requires one instruction to increment the variable.In case of i++(post Increment), the old value has to be returned or used in the expression and then the variable is incrememented after the expression is evaluated. Since you need one instruction to save the old value to be used in the expression and other instruction to increment the variable, its comparatively slower.
5) Why is "this" not a reference?
Answer: Because "this" was introduced into C++ before references were added.
6) Why doesn't C++ have an equivalent to realloc()?
Answer: If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occationally does copy its argument array. In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.
7) Can I mix C-style and C++ style allocation and deallocation?
Answer: Yes, in the sense that you can use malloc() and new in the same program. No, in the sense that you cannot allocate an object with malloc() and free it using delete. Nor can you allocate with new and delete with free () or use realloc() on an array allocated by new.
The C++ operators new and delete guarantee proper construction and destruction; where constructors or destructors need to be invoked, they are. The C-style functions malloc(), calloc(), free(), and realloc() doesn't ensure that. Furthermore, there is no guarantee that the mechanism use by new and delete to acquire and release raw memory is compatible with malloc() and free(). If mixing styles works on your system, you were simply "lucky" - for now.
If you feel the need for realloc() - and many do - then consider using a standard library vector. For example
// read words from input into a vector of strings:
vector words;
string s;
while (cin>>s && s!=".")words.push_back(s);
The vector expands as needed.
What's the value of i++ + i++?
Answer: It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:
v[i] = i++;
Related example:
f(v[i],i++);
Here, the result is undefined because the order of evaluation of function arguments are undefined. Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.
Write a function to display an integer in a binary format.
Answer:
void displayBits( unsigned value )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1<< i =" 1;" 8 ="=">
What is Memory Leak?
Answer: Memory which has no pointer pointing to it and there is no way to delete or reuse this memory(object), it causes Memory leak.
{
Base *b = new base();
}
Out of this scope b no longer exists, but the memory it was pointing to was not deleted. Pointer b itself was destroyed when it went out of scope.
Write the hello world program . . . Without using any semi-colon's ";" ?
Answer:
#include
void main()
{
if(printf("Hello World"))
{ } }
No comments:
Post a Comment
Thanks to given comments.......