Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Will this code work?

Credits go to anonymous, BAF, Billybob, GullRaDriel, Jeff Bernard, StevenVI, Thomas Fjellstrom, Tobias Dammers, and Vanneto for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Will this code work?
type568
Member #8,381
March 2007
avatar

#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
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

StevenVI
Member #562
July 2000
avatar

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

Define "work".

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

type568
Member #8,381
March 2007
avatar

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
avatar

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

--
I thought I was wrong once, but I was mistaken.

Thomas Fjellstrom
Member #476
June 2000
avatar

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;

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

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
avatar

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"
Allegro Wiki, full of examples and articles !!

BAF
Member #2,981
December 2002
avatar

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

GullRaDriel
Member #3,861
September 2003
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

BAF
Member #2,981
December 2002
avatar

type568
Member #8,381
March 2007
avatar

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
avatar

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
avatar

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"
Allegro Wiki, full of examples and articles !!

Audric
Member #907
January 2001

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

GullRaDriel
Member #3,861
September 2003
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

type568
Member #8,381
March 2007
avatar

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
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

type568
Member #8,381
March 2007
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Billybob
Member #3,136
January 2003

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

type568
Member #8,381
March 2007
avatar

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 


Go to: