What is the best practice: global constant or #define?

Possible duplicate:
C ++ - enum vs. const vs. #define

Before I used #define , I used to create constants in my main function and pass them where they were needed. I found that I passed them very often, and that was odd, especially the sizes of the arrays.

More recently, I used #define for the reason that I do not need to pass constants in my main for each individual function.

But now, when I think about it, I could use global constants, but somehow I hesitated a bit with respect to them.

What is the best practice: global constants or #define ?

The answer to a question related to this: Do constants pass from my main, as I described bad practice?

+4
source share
5 answers

They do not do the same. #define allows you to influence code at compile time, while global constants take effect at runtime.

Seeing that #define can give you additional trouble because it doesn't check how you use it, you should use global constants when you can, and #define when you need to. It will be safer and more readable.

Regarding the transfer of constants from main , this is not unreasonable, because it makes the called functions more flexible to accept an argument from the caller than to blindly pull it from some global one. Of course, this argument is not expected to change for the life of a program that you have little to gain.

+6
source

Using constants instead of #define is very preferable. #define replaces the token stupidly in every place where it appears, and can cause all sorts of unintended consequences.

Passing values ​​instead of using global variables is good practice. This makes the code more flexible and modular and more verifiable. Try googling for the "option on top".

+3
source

You should not use #defines or const variables to represent the size of an array; better to make them explicit.

Instead:

 #define TYPICAL_ARRAY_SIZE 4711 int fill_with_zeroes(char *array) { memset(array, 0, TYPICAL_ARRAY_SIZE); } int main(void) { char *za; if((za = malloc(TYPICAL_ARRAY_SIZE)) != NULL) { fill_with_zeroes(za); } } 

which uses (generic, imagine in a generic header or something else) #define to tell the size of an array it is much better to simply pass it to the function as a valid argument:

 void fill_with_zeroes(char *array, size_t num_elements) { memset(array, 0, num_elements); /* sizeof (char) == 1. */ } 

Then just change the call site:

 int main(void) { const size_t array_size = 4711; char *za; if((za = malloc(array_size)) != NULL) { fill_with_zeroes(za, array_size); } } 

This makes the size local in the place where it was allocated; there is no need for the called function to magically β€œknow” something about its arguments, which are not passed through its arguments.

If the array is not dynamically allocated, we can do even better and remove the repeated symbolic size even locally:

 int main(void) { char array[42]; fill_with_zeroes(array, sizeof array / sizeof *array); } 

Here, the well-known expression sizeof x / sizeof *x used to (at compile time) calculate the number of elements in an array.

+2
source

Constants are better. The only difference between the two is that constants are type safe.

+1
source

You should not use the values ​​defined with #define as const parameters. Definitions are mainly used to prevent compilation to compile some parts of the code depending on your needs at compilation time (platform-specific options, optimization at compilation time).

So, if you are not using a definition for these reasons, avoid this and use the costant values.

0
source

Source: https://habr.com/ru/post/1345084/


All Articles