Skip to main content

C++ Questions II


Q11. Should I use NULL or 0?

Ans.
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?

Ans.
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++?

Ans.
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?

Ans.
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;
public:
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?

Ans.
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.

Comments

Popular posts from this blog

JUnit – Run unit test in an Sequence / Order

In JUnit, we can use @FixMethodOrder(MethodSorters.xxx) to run the test methods in a sequence or order.

import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runners.MethodSorters;importstatic org.hamcrest.CoreMatchers.is;importstatic org.junit.Assert.assertThat;//Sorts by method name@FixMethodOrder(MethodSorters.NAME_ASCENDING)publicclassExecutionOrderTest{@TestpublicvoidtestB(){assertThat(1+1,is(2));}@Testpublicvoidtest1(){assertThat(1+1,is(2));}@TestpublicvoidtestA

Create Runnable Jar - Eclipse Options

When exporting to a Runnable Jar, there are three options in eclipse Helios. Extract required libraries into JARPackage required libraries into JARCopy required libraries into sub folder next to JAR. What are differences : Extract required libraries into JAR - Extracts the actual .class files from the libraries your app uses and puts those .class files inside the runnable JAR. So, the runnable JAR will not only contain the .class files of your application, but also the .class files of all the libraries your application uses. Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible. Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the .class files of your application. Option #2 is convenient be…