0

I'm learning c++ array from this link: https://www.learncpp.com/cpp-tutorial/61-arrays-part-i/. Now I'm confused that why cannot create a fixed array (i.e., an array with a fixed length) with the use of macro symbolic constant. I mean, why is it syntactically doable, but not recommended based on the author's opinion.

// using a macro symbolic constant
#define ARRAY_LENGTH 5
int array[ARRAY_LENGTH]; // Syntactically okay, but don't do this
kkxx
  • 571
  • 1
  • 5
  • 16
  • Looking at the examples, I wonder if the author is discouraging the use of a macro symbolic constant for array lengths or discouraging the use of macro symbolic constants entirely. – CH. Nov 18 '19 at 11:04
  • Indeed, the author goes into detail why they discourage macro symbolic constants [here](https://www.learncpp.com/cpp-tutorial/const-constexpr-and-symbolic-constants/). The myriad of existing answers also cover why. – CH. Nov 18 '19 at 11:07

4 Answers4

2

Because macros are evil.

Seriously, nothing is evil. Everything has its place and valid uses. The cases where you use macros in C++ are extremely rare. To define the size of an array it is simply not worth to trade all the downsides that come with macros for no upsides compared to alternatives.

The most drastic downside is that macros dont know scopes. If you write: void foo(int ARRAY_SIZE);, the processor will turn correct code into a syntax error.

why is it syntactically doable, but not recommended

You can do many things with macros, but you could also write everything in assembler. Why would you?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

One reason to not use macros here is that they don't have a limited scope, so it is easier to get name conflicts or use a wrong variable.

Consider these arrays declared in different scopes:

{
    #define ARRAY_LENGTH 5
    int array[ARRAY_LENGTH];
}
{
    #define ARRAY_LENGTH 5 // Compile error!
    int array[ARRAY_LENGTH];
}
{
    #define ARR_LENGTH 10
    int array[ARRAY_LENGTH]; // Wrong variable name, but not compile error!
}

With constant variables, the code would be more robust:

{
    const size_t ARRAY_LENGTH = 5;
    int array[ARRAY_LENGTH];
}
{
    const size_t ARRAY_LENGTH = 5; // Redefinition is ok
    int array[ARRAY_LENGTH];
}
{
    const size_t ARRAY_LENGTH = 10;
    int array[ARRAY_LENGTH];
}
{
    const size_t ARR_LENGTH = 10;
    int array[ARRAY_LENGTH]; // Compile error
}
VLL
  • 9,634
  • 1
  • 29
  • 54
  • IMO `enum` is slightly tidier than a const variable – M.M Nov 18 '19 at 11:32
  • also it should be mentioned that you do not even need a symbolic constant at all -- you can use `int array[10];` and then later `std::size(array)` to get the length – M.M Nov 18 '19 at 11:35
  • @M.M I think ´enum´ is for lists, not for a single constant. Also the enum value is `int` by default, while array size is `size_t`. – VLL Nov 19 '19 at 07:15
  • An enum with one enumerator is perfectly fine – M.M Nov 19 '19 at 07:31
0

Since c++11 we have constexpr for this case.

I don't know about the authors opinion, but this question elaborates on the problems that can be caused by macros. (I don't see the need to repeat those statements here)

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

Macros do not have scopes and types. They can conflict with other macros. So in C++ it is better to define a named constant like

const size_t ARRAY_LENGTH = 5;
int array[ARRAY_LENGTH];
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335