using macro defined in header files

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!

Answers


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.


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.


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.


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.


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.


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.


Need Your Help

Amazon Beanstalk Web Server with Self Signed certificate?

ssl amazon elastic-beanstalk

Is it possible to start Amazon Beanstalk Web Server in SSL mode with the Self Signed certificate?

Spotify: How to prevent it remembers listening more then 1 song?

javascript api spotify playlist

I am making an application for Spotify. With this application I have a simple 'Subscribe' function which allows me to add a playlist of the album from the current track the user is listening to.