# Macro vs Function

C/C++ code snippet 1 (macro):

 1 #define ARR_LEN_BY_MACRO(x) sizeof(x)/sizeof(*x) 

C/C++ code snippet 2 (function):

 1 2 3 4 int ARR_LEN_BY_FUNC(int *x) { return sizeof(x)/sizeof(*x); } 

An integer array of 5 elements is declared and initialized in main() as int arr[5]={1,2,3,4,5};.

The macro and function of the code snippets above are declared and defined globally.

From main(), the macro and the function are called with the argument arr as follows,

 1 2 m=ARR_LEN_BY_MACRO(arr); f=ARR_LEN_BY_FUNC(arr); 

What's the absolute difference between the values stored in m and f, i.e., what's the value of $$|m-f|$$ ?

Details and Assumptions:

• m and f are int variables.

• sizeof() is an inbuilt C/C++ function which returns the number of bytes occupied by an object.

• main() is the function block which is called first when the code is compiled and executed.

• Assume that the code is being executed in a 32-bit compiler on a 32-bit machine, which means that an int or int* variable takes 4 bytes of memory, i.e., we have sizeof(int)=sizeof(int*)=4.

×