Macros vs. Functions

Consider the a is_empty_int_stack() function in the int_stack ADT. In many cases, we may decide that we don't want to pay the overhead of the function call; for efficiency we want to replace this function with a macro.

When replacing a function with a macro:


	#define	is_empty_int_stack(_s)	(len_int_stack(_s) == 0)


	int	(is_empty_int_stack)(const int_stack);



	int
	(is_empty_int_stack)(
		const int_stack	stack
		)
	{
	  /* this is NOT recursive: refers to the macro */
	  return is_empty_int_stack(stack);
	}

The old function definition can be discarded (which is perhaps appropriate for small functions) or alternatively, kept around for verification. Thus, in the implementation file:


 	/* this is the original version of the function; kept around
	 * only for verification.
	 */
	static int
	func_something_complex_T(
		const T	t
		)
	{
	  /* ... */
	}

	/* this is the function corresponding to the macro */
	int
	(something_complex_T)(
		const T	t
		)
	{
	  int	res = something_complex_T(t);

	  #if( VERIFY_T )
	    verify(res == func_something_complex_T(t));
	  #endif

	  return res;
	}


Next Prev Main Top Feedback