Will this code work?
type568
#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

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

It was easier to open up the Allegro.cc forums to ask this rather than putting it into a compiler?

Define "work".

type568

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

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"}599918

EDIT-- ah, but now now I see this has to do with Matthew...
http://www.allegro.cc/forums/thread/602288

Thomas Fjellstrom
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

Yes.

And no, you don't want to do it.

Vanneto

Hmmm, made me think, what if:

if (1 == 0) {
}

Actually returned true? What would that mean?

GullRaDriel

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.

BAF

What if I were a millionaire? What would that mean?

GullRaDriel

That you are still asleep and dreaming, BAF ;-)

BAF

:P

type568
Vanneto said:

Actually returned true? What would that mean?

That would mean a FATAL ERROR I suppose..

anonymous
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

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

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 {}

Audric

I've used similar code (writing at NULL) to test my signal handler.

GullRaDriel

Audric: I use kill -signal for that kind of things.

type568

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

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. ;D

type568

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:

Plus your macro is evil.

Is my if statement less evil?
All this thread was sort of a joke.. Bit smelling with philosophy- as of what an actually working code is.

Thomas Fjellstrom
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

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
type568

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.

BAF

No, that's still not right. Check out the link Audric posted, but anyway, here's your example:

if(crash) FATAL_ERROR;
else printf("okay");

Just using plain {}, that code would turn into:

if(crash) {/*blah*/};
else printf("okay");

... which obviously isn't right.

type568

And if that would be

#define FATAL_ERROR exit();

It also wouldn't be right, due to the risk of having a:

if(crash) exit();;
else printf("okay");

?

BAF

Don't put the ; in the define.

Billybob

Thank you for the link to Stack Overflow, that explains everything.

Tobias Dammers
type568 said:

It compiles.

Doesn't mean sh* with macros.

#define foobar Hello there, you don't know me but I compile! Yay!@@#$%$QWQU

int main() {
  return 0;
}

Vanneto

There is a difference between just defining a macro and actually using it. :P

type568

Well, I did compile it and did put it in to the code(binary code was generated using the Macro) hence your notes seem to be irrelevant.

BAF

Okay, have fun then. It's not like I'm going to have to deal with your messes... at least I hope not. I'll watch out for you on the daily wtf. 8-)

type568

:)

Thread #602399. Printed from Allegro.cc