Difference between revisions of "C/C++ FAQ"

From CDOT Wiki
Jump to: navigation, search
(C/C++ FAQ)
Line 45: Line 45:
 
<br>
 
<br>
 
'''Q:''' When using square-bracket pointer arithmetic, does ptr[-k] decrement the pointer?<br>
 
'''Q:''' When using square-bracket pointer arithmetic, does ptr[-k] decrement the pointer?<br>
'''A:''' Yes. 'ptr' is the address of an element of the array; the array's element size is multiplied by the value of 'k', negated by '-', and then 'added' (in practice subtracted) to the address stored in 'ptr'. ptr[-k] moves k elements earlier than the element pointed to by ptr.
+
'''A:''' Yes. 'ptr' is the address of an element of the array; the array's element size is multiplied by the value of 'k', negated by '-', and then 'added' to the address stored in 'ptr'. ptr[-k] moves to the element 'k' elements earlier than the element pointed to by ptr.
  
  

Revision as of 15:17, 19 September 2012

C/C++ FAQ

Q: Why is the postfix increment/decrement operator (e.g. a++ and a--) evaluated differently on different compilers?

A: The evaluation of expressions, especially arithmetic expressions are based on sequence points which are undefined by the language. Arithmetic expressions containing complex postfix calculations are evaluated differently across different compilers because each compiler is unequally efficient. That is to say, these expressions are not portable as each compiler uses a different way to evaluate the expression based on its efficiency implementation. This can be noted by observing the process time of an expression across different platforms, which will be different for the same expression, due to different methods of evaluation.
For example, consider the following snippet:

   #include <iostream>
   using namespace std;

   #define PI 3.14

   int main()
   {
        double a = 2.35;
        cout << PI * ((a++) * (a++))<< endl;                              
        return 0;
   }

The GNU compiler prints 17.3407 as the output whereas the Borland compiler prints 24.7197 as the output.


Submitted by: Gideon Thomas and Marie Karimizadeh



Q: When we define a function-like macro, does the compiler ignore or take into consideration the spaces in between the parentheses?
For example:

#define F( x ) ((x) + (x))
int main()
{
   int a = 5;
   cout << F( a );
   return 0;
}

Which one of the following will the cout statement change to and why:

  1. cout << ((a) + (a));
  2. cout << x ) ((x) + (x)) a);

Note: in the second case, F( is considered the symbolic constant and is replaces by the rest of the string i.e. x ) ((x) + (x)) wherever it occurs.


Submitted by: Gideon Thomas and Marie Karimizadeh



Q: When using square-bracket pointer arithmetic, does ptr[-k] decrement the pointer?
A: Yes. 'ptr' is the address of an element of the array; the array's element size is multiplied by the value of 'k', negated by '-', and then 'added' to the address stored in 'ptr'. ptr[-k] moves to the element 'k' elements earlier than the element pointed to by ptr.


Submitted by: Kevin Kofler and Lucas Passarella, Team 9