views:

98

answers:

6

I have a macro definition in header file like this:

// header.h
ARRAY_SZ(a) = ((int) sizeof(a)/sizeof(a[0]));

This is defined in some header file, which includes some more header files.

Now, i need to use this macro in some source file that has no other reason to include header.h or any other header files included in header.h, so should i redefine the macro in my source file or simply include the header file header.h.

Will the latter approach affect the code size/compile time (I think yes), or runtime (i think no)?

Your advice on this!

+1  A: 

Including that header and all other headers included into it will increase the compile time. It can affect runtime if there're other definitions that will change how your code compiles - if your code compiles differently because of those defines it will of course run differently. Although the latter is not usual be careful.

sharptooth
+2  A: 

You ask:

Will the latter approach affect the code size/compile time (I think yes)

In the case of the specific macro, the answer is "no" to the size, because the sizeof expression can be evaluated at compile time, and therefore "yes" to the time. Neither are likely to be remotely significant.

anon
+4  A: 

Including the header in the source file might affect compile time slightly unless you are using pre-compiled headers. It shouldn't affect the code size though. Redefining the macro shouldn't have any effect on compile time or size. It is more of a maintenance and consistency issue though.

Ryan
+4  A: 

Include the header file or break it out into a smaller unit and include that in the original header and in your code.

As for code size, unless your headers do something incredibly ill-advised, like declaring variables or defining functions, they should not affect the memory footprint much, if at all. They will affect your compile time to a certain degree as well as polluting your name space.

plinth
+1 Factoring out useful features from headers to avoid possibly unneeded includes/clutter.
Mark B
+3  A: 

should i redefine the macro in my source file or simply include the header file header.h.

Neither. Instead you should clean up the code and break header.h so that one can use ARRAY_SZ() without also getting unrelated stuff.

sbi
And it's important to note that if you include the newly created header in `header.h`, then for all intent and purpose it will be transparent for the actual users of `header.h`, so definitely an easy change!
Matthieu M.
@Matthieu: Yes, I forgot to mention that. Thanks for adding it!
sbi
+2  A: 

Unless you're running this on a really limited bit of hardware, or this is called billions and billions of times, you won't notice any difference between the two at either compile time or run time.

Go for whatever seems more readable / maintainable.

Personally, I'd suggest there are better ways of achieving what you're doing there without involving macros (namely inline functions and/or function templates). You have to be careful using your solution because there are a few gotchas you need to keep an eye on.

Jon Cage