Skip to main content

C++ Questions II

Q11. Should I use NULL or 0?

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.

Q12. How are C++ objects laid out in memory?

Like C, C++ doesn’t define layouts, just semantic constraints that must be met. Therefore different implementations do things differently. Basically, C++ constructs objects simply by concatenating sub objects. Thus
struct A
int a , b ;
} ;
is represented by two ints next to each other, and
struct B : A
int c;
} ;
is represented by an A followed by an int; that is, by three ints next to each other.
Virtual functions are typically implemented by adding a pointer (the vptr) to each object of a class with virtual functions. This pointer points to the appropriate table of functions (the vtbl). Each class has its own vtbl shared by all objects of that class.

Q13. What’s the value of i++ + i++?

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. This is not a good practice. 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).

Q14. What is a function object?

A function object is a more general concept than a function because a function object can have state that persist across several calls (like a static local variable) and can be initialized and examined from outside the object (unlike a static local variable). For example:
class Sum
int val;
Sum ( int i ) : val ( i ) { }
operator int () const { return val ; } // extract value
int operator () ( int i ) { return val += i ; } // application
} ;
void f ( vector <int> v )
Sum s = 0 ; // initial value 0
s = for_each ( v.begin(), v.end(), s ) ; // gather the sum of all elements
cout << “the sum is ” << s << “\n” ;
// or even:
cout << “the sum is ” << for_each ( v.begin(), v.end(), Sum(0) ) << “\n” ;
Note that a function object with an inline application operator inlines beautifully because there are no pointers involved that might confuse optimizers. To contrast: current optimizers are rarely ( never? ) able to inline a call through a pointer to function.
Function objects are extensively used to provide flexibility in the standard library.

Q15. Why can’t I resume after catching an exception?

In other words, why doesn’t C++ provide a primitive for returning to the point from which an exception was thrown and continuing execution from there?
Basically, someone resuming from an exception handler can never be sure that the code after the point of throw was written to deal with the execution just continuing as if nothing had happened. An exception handler cannot know how much context to “get right” before resuming. To get such code right, the writer of the throw and the writer of the catch need intimate knowledge of each others code and context. This creates a complicated mutual dependency that wherever it has been allowed has led to serious maintenance problems.
If you want to check to see if you can fix a problem before throwing an exception, call a function that checks and then throws only if the problem cannot be dealt with locally. A new_handler is an example of this.


Popular posts from this blog

Exploring Node.js Internals

I found a great article explaining Node JS internals, must read : Some other articles : Introduction to Node.js Being an official website, explains what Node.js is, as well as its package managers, and lists web frameworks built on top of it. “ JavaScript & Node.js ”,  The Node Beginner Book This book by  Manuel Kiessling  does a fantastic job of explaining Node.js, after warning that JavaScript in the browser is not the same as the one in Node.js, even though both are written in the same language. Beginning Node.js This beginner book goes beyond an explanation of the runtime. It teaches about packages and streams and creating a web server with the Express framework. LibUV This is the official documentation of the supporting C++ code of the Node.js runtime. V8 This is the official documentation of the JavaScript engine that makes it possible to write Node.js with JavaScript.

Tum mujhko karna maaf priye – Poem By Dr. Kumar Vishwas

Lyrics : O kalp-vraksh ki son-juhi, O amal-tas ki amar kali Dharti ke aatap se jalte, Man par chaayi nirmal badli Main tumko madhu-sad-gandh yukt, sansaar nahi de paunga ! Tum mujhko karna maaf priye main pyar nahi de paunga !! Tum kalp-vraksh ka fool aur Main dharti ka adna gayak Tum jeewan ke upbhog yogya Main nahi adhuri gazal shubhey Tum saam-gaan si pawan ho Himshikharon par sehsa kaundha Bijuri si tum man bhawan ho Isliye vyarth shabdo wala, vyapar nahi de paunga ! Tum mujhko karna maaf priye main pyar nahi de paunga !! Tum jis shaiya par shayan karo, Veh sheer sindh si pawan ho Jis aangan ki ho maul-shree Veh aangan kya vrandawan ho Jin adhron ka chumban pao Veh adhar nahin ganga tat ho Jiski chaya ban saath raho Veh vyakti nahin vanshi-vat ho Par main vat jaisa saghan chaaon ,vistaar nahin de paunga ! Tum mujhko karna maaf main pyar nahi de paunga !! Main tumko chand sitaron ka Saupun uphar bhala kaise Main yayavar banjara sadhu sur-sansar bhala kaise Main jeewan