Redefining or changing macro value
Asked Answered
P

2

31

I am currently working on an already developed project written in MFC C++ and am facing a problem with an already present macro having the definition:

#define HEIGHT_TESTS 13

I am trying to change the value from within the code but I think since its a preprocessed definition, I am unable to do that. Is there a way I could get around this problem without having to change the original macro overall (as it might affect the original functionality of the program). I am just intending to change it in one particular condition, rest everywhere else it remains the same.

Just to let everyone know, I have obviously tried out using a different macro definition with the value (17) I am intending to use, but no luck as such.

Any help would be much appreciated.

Pescara answered 14/2, 2012 at 9:30 Comment(3)
I would recommend having another macro for your purpose, rather than #undefining and redefining it.Ferruginous
Note that you won't be able to save the existing value and restore it, if you need to restore. The only way for you will be to redefine the macro with 13 which is obviously a hard coded value. As for the answers, you already have 2 below which are correct.Wiggs
I did try out using a different macro, but it did not work out. The program crashes. Its only when I change this particular #define value, it gives me a proper result (not the result I am intending to get with this particular condition).Pescara
V
60

You can undefine it and define again:

#include <iostream>

#define AAA 13

int main()
{
    #undef AAA
    #define AAA 7
    std::cout << AAA;
}

outputs: 7

Please note that statements that start with # are preprocessor directives that are taken care of before the code is even compiled. In this case, this constant AAA will be simply replaced by 7, i.e. it works just like a textual replacement with no additional checks of syntax, no type safety etc...

...which is main reason why you should avoid using macros and #defines where they can be replaced by static functions and variables :)


Why "textual replacement" ?

Look at this code:

#include <iostream>

#define AAA 13

void purePrint() {
    std::cout << AAA;
}

void redefAndPrint() {
    #undef AAA
    #define AAA 7
    std::cout << AAA;
}

int main()
{
    #undef AAA
    #define AAA 4
    purePrint();
    redefAndPrint();
    purePrint();
}

preprocessor goes line by line from the top to the bottom, doing this:

  • ah, #define AAA 13, so when I hit AAA next time, I'll put there 13
  • look, purePrint uses AAA, I'm replacing it with 13
  • wait, now they tell me to use 7, so I'll stop using 13
  • so here in redefAndPrint() I'll put there 7

transforming the given code into this one:

#include <iostream>

void purePrint() {
    std::cout << 13;
}

void redefAndPrint() {
    std::cout << 7;
}

int main()
{
    purePrint();
    redefAndPrint();
    purePrint();
}

which will output 13713 and the latest #define AAA 4 won't be used at all.

Veterinary answered 14/2, 2012 at 9:35 Comment(8)
So, we can use #undef and #define again anywhere in the program?Pescara
@Nerds.Dont.Swear : Yes. And when you call #undef AAA, you don't even need to worry whether AAA was defined before.Veterinary
@Nerds.Dont.Swear : Yeah, that was typo ;)Veterinary
This answer is very confusing. Someone thought they could redefine a macro at runtime.Borne
@iharob: This answer does not say anything about doing stuff in "runtime". I don't understand your point.Veterinary
Exactly, that's my point, someone thought they could undef and define the macro like if (someCondition) {#undef macro #define macro newValue} and they claimed that this post told them that it was possible. Although the post doesn't say that, it doesn't warn against that and perhaps it's a good Idea to improve it.Borne
I think you did a good thing by fixing this old post, because it's useful. Thank you.Borne
You shouldn't indent those PP directives. It makes them look as if they're at function scope but preprocessor doesn't have any notion of scope.Gyp
U
5

Something like the following:

#undef HEIGHT_TESTS
#define HEIGHT_TESTS 17

// Use redefined macro

// Restore
#undef HEIGHT_TESTS
#define HEIGHT_TESTS 13
Upward answered 14/2, 2012 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.