Reputation: 1405
Is it possible (and if so is it a good idea) to have a preprocessor directive, particularly #define to apply across a whole application, or at least multiple files?
I'm trying to manipulate an array in multiple parts of a program, so I have to make sure the memory allocated to it is still in scope.
To do this I've decided I want to declare the array inside my main object, which wont be deleted until the app is quit. The problem is I don't know at compile time how large my array needs to be, so I want to use #define MAX_ARRAY_SIZE 20
.
This is fine, but I'd like to only need to change the max size in one place. Is there a good (and safe) way to do this or should I suck it up and put the #define in all the files that need it?
Upvotes: 0
Views: 1568
Reputation: 18109
If you use gcc/g++ or compatible tools you can add it as a compiler directive, for example in your makefile.
g++ ... -DMAX_ARRAY_SIZE=20 ...
This is a common way to #define across the whole build and it makes the change very isolated, which is nice.
Upvotes: 3
Reputation: 101446
This is often handled by the build toolchain, rather than in the code. It's surprisingly difficult to apply a #define
to a whole program, especially when that program is large.
If you're using cmake to create the makefiles, you can do cmake ... -DMAX_ARRAY_SIZE=20
. Under linux using gcc, this ultimately ends up becoming g++ ... -DMAX_ARRAY_SIZE=20
, which you can also do if your building from hand rather than using makefiles.
On another note, this is not a great use for macros IMO. Macros skirt around the C++ type system, are very difficult to debug & maintain, and should usually be avoided unless you have no other choice. There are times when you have no other choice -- but this isn't one of them. instead of a macro, use a real variable:
static const size_t MaxArraySize = 20;
This should be in a header included everywhere you want to use it. There will be only one place where this declaration exists.
Upvotes: 1
Reputation: 36
It is fine to have a #define across multiple files. You could do this with a header file that is shared. However, it doesn't sound like that will solve your problem.
If you don't know the size of your array at compile time, this will not help--since #define will only work at compile time.
You could simply have the size of the array as another variable and dynamically allocate your array using new and delete it using delete[]. Or simply use std:vector.
Upvotes: 2
Reputation: 18338
You have several options here. Classic approach is to to move your #define
to a header file and include it from any relevant source file.
A better approach might be this:
template <class T, int maxSize = 20>
class myClass
{
T myArr[maxSize];
...
};
int main()
{
myClass<int> x;
return 0;
}
This way there is no need for all the logistics of the macro approach.
Upvotes: -1
Reputation: 1179
Yep! declare it as a MACRO or just as a gobal variable in a header file. Make sure to include this header file where you use/ modify.
if you are also looking to modify it then global variable is simplest way to handle it. It is also possible to be done using MACRO
just use:
undef MAX_ARRAY_SIZE
define MAX_ARRAY_SIZE 100
Upvotes: 0
Reputation: 1911
create a global header-file and use header-guard. After inclusion of this global header in each of your headerfiles / *.c / *.cpp - files you will have the macro available to your local code. I would include the global header in your implementation-files IF you dont want to use the macro in your headers ... which is what i also recommend.
example :
global.h:
#ifndef GLOBAL_H
#define GLOBAL_H
#define MYCONSTANT 0xFF
#endif
child-files:
#ifndef GLOBAL_H
#include "global.h"
#endif
also dont forget to use
#pragma once
Upvotes: 2