|
|
This thread is locked; no one can reply to it.
|
1
2
|
| Will this code work? |
|
type568
Member #8,381
March 2007
|
#include <stdio.h> #define FATAL_ERROR {int* a=NULL; *a=1;} int main(int argc, char** argv){ if(1==0) FATAL_ERROR return 0; }
|
|
Thomas Fjellstrom
Member #476
June 2000
|
That specific code might work. You really don't want to try and access invalid pointers like that. just use abort() or even exit() instead. Also, theres an issue with the {}s. If you try and put it in certain places, you'll start to get confused. -- |
|
StevenVI
Member #562
July 2000
|
It was easier to open up the Allegro.cc forums to ask this rather than putting it into a compiler? Define "work". __________________________________________________ |
|
type568
Member #8,381
March 2007
|
Thomas Fjellstrom said: Also, theres an issue with the {}s. If you try and put it in certain places, you'll start to get confused. I edited the first post before your reply. Is it ok now? StevenVI said: It was easier to open up the Allegro.cc forums to ask this rather than putting it into a compiler? It compiles. Quote: Define "work". No. Append: That's not coz I don't want to.
|
|
Jeff Bernard
Member #6,698
December 2005
|
My initial thought was no... {"name":"599918","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/7\/87aa5d17258a1186c51946c1ded42232.jpg","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/7\/87aa5d17258a1186c51946c1ded42232"} EDIT-- ah, but now now I see this has to do with Matthew... -- |
|
Thomas Fjellstrom
Member #476
June 2000
|
type568 said: I edited the first post before your reply. Is it ok now? It looks the same to me You generally don't want to put bare {}s in a define. do something like: #define BLAH do{ codehere }while(0) instead. Then you can use it like so: if(foo) BLAH; -- |
|
Billybob
Member #3,136
January 2003
|
Yes. And no, you don't want to do it.
|
|
Vanneto
Member #8,643
May 2007
|
Hmmm, made me think, what if: if (1 == 0) { } Actually returned true? What would that mean? In capitalist America bank robs you. |
|
GullRaDriel
Member #3,861
September 2003
|
It will never return true because it's the same if "a=1;b=0; if(a==b)" would have returned true: nothing would have worked as expected. "Code is like shit - it only smells if it is not yours" |
|
BAF
Member #2,981
December 2002
|
What if I were a millionaire? What would that mean? |
|
GullRaDriel
Member #3,861
September 2003
|
That you are still asleep and dreaming, BAF ;-) "Code is like shit - it only smells if it is not yours" |
|
BAF
Member #2,981
December 2002
|
|
type568
Member #8,381
March 2007
|
Vanneto said: Actually returned true? What would that mean? That would mean a FATAL ERROR I suppose..
|
|
anonymous
Member #8025
November 2006
|
Vanneto said: Hmmm, made me think, what if: if (1 == 0) { } Actually returned true? What would that mean? That would basically mean the compiler malfunctioned, and given such a compiler, god knows what FATAL_ERROR would do. |
|
type568
Member #8,381
March 2007
|
Thomas Fjellstrom said: You generally don't want to put bare {}s in a define. I guess last question in this thread.. Thomas, is there a real reason not to put a {} in a define?
|
|
GullRaDriel
Member #3,861
September 2003
|
No real one except it's unneeded. They're needed in case of a if/do/while/for statement inside the #define, but you gain nothing adding {} in some simple statements as int *it; it=NULL, except huge problem spotting out the bug if you forgot to close/open a {} "Code is like shit - it only smells if it is not yours" |
|
Audric
Member #907
January 2001
|
I've used similar code (writing at NULL) to test my signal handler. |
|
GullRaDriel
Member #3,861
September 2003
|
Audric: I use kill -signal for that kind of things. "Code is like shit - it only smells if it is not yours" |
|
type568
Member #8,381
March 2007
|
Actually, I DO need the {} in the define, as otherwise the above code won't even compile. As the macro is just replaced with it, and only one line is scoped in to an if statement(and the a won't be declared identifier).
|
|
GullRaDriel
Member #3,861
September 2003
|
You DO need it because of your implementation. Keep in mind that #define are just preprocessing substitution. Doing so would also work: #include <stdio.h> #define FATAL_ERROR int* a=NULL; *a=1 int main(int argc, char** argv){ if(1==0) { FATAL_ERROR; } return 0; }
Plus your macro is evil. "Code is like shit - it only smells if it is not yours" |
|
type568
Member #8,381
March 2007
|
That's what I meant.. But I wanna be referring to a single line MACRO as to a single line.. I still wonder what did Thomas mean though. Append: GullRaDriel said: Plus your macro is evil.
Is my if statement less evil?
|
|
Thomas Fjellstrom
Member #476
June 2000
|
type568 said: I still wonder what did Thomas mean though. Well for one, when you look at a bare macro do you always remember it has {}s surrounding it? What if you put it some place that causes a compiler error? Will you remember then that it is a block? I already gave the proper alternative. a do{CODE HERE}while(0) construct. That way its a statement, and not a block. And can be used in any place a regular statement can be, without any problems. -- |
|
Billybob
Member #3,136
January 2003
|
Thomas Fjellstrom said: That way its a statement, and not a block. I have a feeling you are correct, but I am curious for an example as well. An example of where his implementation would cause an error, and yours would not.
|
|
Audric
Member #907
January 2001
|
Example (google on "define while 0"): |
|
type568
Member #8,381
March 2007
|
Thomas Fjellstrom said: Well for one, when you look at a bare macro do you always remember it has {}s surrounding it? What if you put it some place that causes a compiler error? Will you remember then that it is a block? That's it.. I can't imagine such a case. Unless I decided to do some assignment works out of scope of a function.. However, is not this: #define FATAL_ERROR {int* a=NULL; *a=1;};
Easier? I didn't check this one, but I'm quite sure that MSVC(latest one) at least won't give errors to that.. Furthermore, I encountered a ;; in my code, without any warnings, and of course without side effects as well. a ; is a ;, it doesn't harm if doubled, unless really misplaced.
|
|
|
1
2
|