4

What are the advantages and disadvantages of using #define over const (and vice versa)?

When I read about bad programming practices specifically magic numbers, I found myself using #define more frequently. Some questions popped into my mind such as:

Is it bad to use #define a lot?
Does it take memory space?
Would it be faster to use const instead?

I read a bit about this but I'm still not sure, from what I've understood:
#define defines a macro (not sure what macro means) and it deals with pre-processing. It replaces all instances of the defined keyword into something else before the code is being processed. const on the other hand is variable whose value cannot be changed midway during runtime.

The only reason I can think of using const is if the value relies on other variables. For example:

#define PI 3.14159f
#define RADIUS 3.0f
#define AREAOFCIRCLE PI*RADIUS*RADIUS

would be inefficient since every instance of AREAOFCIRCLE would be replaced by PI*RADIUS*RADIUS so the program would compute for it everytime you use AREAOFCIRCLE. On the other hand:

const float areaofcircle = PI*RADIUS*RADIUS;

would be more effecient since the program would only compute for it once.

So back to the original question, how does #define compare to const?

a3f
  • 8,517
  • 1
  • 41
  • 46
DarkPotatoKing
  • 499
  • 2
  • 9
  • 17

3 Answers3

10

Don't worry about efficiency in this case since all of them will be computed in compile-time.

You should stop using Macros (at least to define constants) whenever you can. Macros are wild things against namespaces and scopes. On the other hand const objects have type and this can reduce unintended mistakes.

It's always useful to read Stroustrup's piece of advises: "So, what's wrong with using macros?"

masoud
  • 55,379
  • 16
  • 141
  • 208
  • what does "all of them computes in compile-time." mean? – DarkPotatoKing Oct 27 '13 at 08:10
  • It doesn't matter you're using Macro or const, in both case the result of `PI*RADIUS*RADIUS` will be computed in compile-time and final value will be replaced to assign. – masoud Oct 27 '13 at 08:23
  • so does that mean #define AREAOFCIRCLE PI*RADIUS*RADIUS would only be computed by the program once and not everytime you call AREAOFCIRCLE? – DarkPotatoKing Oct 27 '13 at 08:32
  • Yes, in run-time you have the calculated value everywhere you used `AREAOFCIRCLE`. – masoud Oct 27 '13 at 08:42
  • Macros used to define manifest constants have types. With `#define SIZE 1`, `SIZE` has type `int`. – Pete Becker Oct 27 '13 at 14:27
  • @PeteBecker: Yes, they have the default types that compiler choose them not the types may the programer wants. However the programmer can insert the actual type by `#define SIZE ((size_t)1)` but it's ugly! – masoud Feb 02 '15 at 13:52
4

"const" in C++ is invaluable. Consts enforce type checking in expressions while the "#define"d constants are just expanded.

Moreover as the constants are typed the compiler can interfere better optimizations while with defines it has to guess (1.0 is a float or a double? Using suffixes as 1.0d or 1.0f might help, but I have seldom seen real numbers written so consciously...).

So use consts for defining constants

const float PI = 3.14159f;
const float RADIUS = 3.0f;
const float AREAOFCIRCLE = PI*RADIUS*RADIUS;

You will get at least the same compile time optimizations + some additional type check.

Alex
  • 9,891
  • 11
  • 53
  • 87
-1

"const" is more efficient due to it takes up memory once and be referenced every where. In contrast, "#define" will take up space and addition operation anywhere it be used.

Alan Yang
  • 65
  • 4