-1

Let's say I have thre project wide variable blocks with external linkage vByte[1000], vWord[1000] and vQword[1000]. And because of the lack of a better debug solution, every variable that I want to see at runtime has to be in those blocks.

Now I want to implement a set of functions in a module(compilation unit) that has a few important values that I want to be able to monitor. I came up with the following solutions to make a more clear access to these parts of the array, but I am unsure which to use.

At first I would declare the array extern vByte[1000]; in my module and the I would like to give a specific variable of the array a fitting name.

I could just #define the variables I want to use: #define importantValue1 vByte[21] and use them like this.

But I think it might be better to encapsulate the variable in my module, maybe like this:

static byte *importantValue1 = &vByte[21]; 

This would reinforce that this variable is for use in this module. Are there drawbacks to this or is the define just the straight forward approach with less overhead and no drawbacks?

Kami Kaze
  • 2,069
  • 15
  • 27
  • You question is a bit broad (but I didn't downvote it). Generally, if I understand the question, and you have multiple sources that all need to be able to access your "project wide variable blocks". You would generally factor your sources so that each of your blocks are declared and initialized in one source file (e.g. `uint8_t vByte[1000]; `) and then simply declare `yByte` as `extern` where needed in any of the other files. No need to pass it, it is already declared and initialized elsewhere. – David C. Rankin Nov 02 '18 at 08:22
  • @DavidC.Rankin Of course the `vByte` is linked in the module somehow. But the name is quite unspecific. I would like to use parts of the array with a clearer name in my module, that is the reason for the redclaration. I will try to make this more clear in my question – Kami Kaze Nov 02 '18 at 08:39
  • Ok, sorry. What I generally do is declare my program control structs in `main()` and then pass by pointer as required. I guess your data is all numeric and of common type, otherwise a control struct provides a easy way to make various blocks available throughout your code. – David C. Rankin Nov 02 '18 at 08:43

1 Answers1

1

It is hard to answer whether a #define or a pointer is the better approach in general.

From the perspective of reducing dependencies and code complexity, however, reducing the amount of global variables usually reduces dependencies among functions / units and thereby reduces overall complexity.

So I'd say it's not that much a question of whether the #define- or the pointer approach is better; I'd rather strive for functions that do not access these global arrays (regardless through which "variable" technique) but use function parameters/arguments only instead.

BTW: note that the #define-"variable" and the pointer variable need to be used differently, as in the first case you can write importantValue1 = 10, whereas in the second case you'd have to write *importantValue1 = 10. But I think you are aware of that.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58