2

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!

sud03r
  • 19,109
  • 16
  • 77
  • 96
  • I assume it's probably an accident (or something), but in case it's not - you don't want the '`=`' or '`;`' characters, and you want it to be part of a `#define`, like: `#define ARRAY_SZ(a) (/*the expression */)` – Michael Burr Apr 07 '10 at 14:44
  • Also, look at the following SO answer for a somewhat more complex version of the macro that has a bit more safety (it's less likely to accept a pointer that gives a bogus result): http://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array/1598827#1598827 – Michael Burr Apr 07 '10 at 14:47

6 Answers6

6

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
  • 48,267
  • 11
  • 78
  • 120
4

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
  • 7,835
  • 2
  • 29
  • 36
3

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
  • 219,715
  • 46
  • 258
  • 445
  • 1
    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. Apr 07 '10 at 13:40
  • @Matthieu: Yes, I forgot to mention that. Thanks for adding it! – sbi Apr 07 '10 at 13:48
2

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.

2

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.

Community
  • 1
  • 1
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
1

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
  • 167,383
  • 100
  • 513
  • 979