Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » what does this do?

This thread is locked; no one can reply to it. rss feed Print
 1   2 
what does this do?
Neil Walker
Member #210
April 2000
avatar

Hello,
Just come across this in code, am I missing something as I have no idea what it's meant to achieve:

int main(int argc, char** argv)
{
  B2_NOT_USED(argc);
  B2_NOT_USED(argv);

Where:

#define B2_NOT_USED(x) x

and argc/argv aren't used anywhere in the code either. Searching the internet showed this, presumably the previous version of the code:

#ifdef MSVC
#define B2_NOT_USED(x) x
#else
#define B2_NOT_USED(x)
#endif

[edit]ooh, found a bug in Matthews parser in the single line of code dropping the underscores.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Evert
Member #794
November 2000
avatar

Quote:

am I missing something as I have no idea what it's meant to achieve:

It's adding in a statement that does nothing (yes, "var;" is a valid C statement, as is "3;"), tricking the compiler into thinking those variables are used and suppressing a variable-declared-but-not-used warning.

Quote:

found a bug in Matthews parser in the single line of code dropping the underscores.

No, you haven't. Maybe in your webbrowser though.

Thomas Fjellstrom
Member #476
June 2000
avatar

The typical version of the above that I see is:

#define NOT_USED(v) (void)(v)

int main(int argc, char **argv)
{
   NOT_USED(argc);
   NOT_USED(argv);

Mainly because at least one compiler will complain that the resulting statement's value is unused, so you have to tell it to ignore it all with (void).

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

Neil Walker
Member #210
April 2000
avatar

ah, thanks.

Evert, I'm using Firefox. If I have one line using underscores with a # at the start in code then the underscores disappear, if I add some text to a second line, the underscores appear again. No # and all is well.

This is ok

#this_has_underscores
second line

This has no underscores
#this_has_underscores_but_is_not_showing_underscores

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Thomas Harte
Member #33
April 2000
avatar

It has underscores in Safari.

Thomas Fjellstrom
Member #476
June 2000
avatar

Works fine in Konqueror 4 as well.

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

someone972
Member #7,719
August 2006
avatar

IE 7 shows them fine.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

gnolam
Member #2,030
March 2002
avatar

Neil: Your Firefox is broken then.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

someone972
Member #7,719
August 2006
avatar

Perhaps it is a font issue?

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Neil Walker
Member #210
April 2000
avatar

This (it's an image) is what I see in firefox 304, but I doubt vista has anything to do with it:

http://www.allegro.cc/files/attachment/597289

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

anonymous
Member #8025
November 2006

I think I've seen also the following which I suppose is defined as follows (in wxWidgets).

#define UNUSED(x)

int main(int UNUSED(argc), char** UNUSED(argv))
{
}

As I see this doesn't try to outsmart the compiler (the other macro produces "statement has no effect" warnings, at least with GCC) and is purely informative - it shows that the arguments are not used, and also with the name what they would be used for if they did.

The warning itself is useful because it actually helps spot logic errors in functions - e.g. accidentally using a different variable instead of the intended argument. If you did mean to use the argument, the warning should raise some alarms, and if you didn't just leave the name out or use a macro of your liking.

------

Evert
Member #794
November 2000
avatar

Quote:

This (it's an image) is what I see in firefox 304, but I doubt vista has anything to do with it:

Firefox 3.0.4, OS X. It works fine.

Vanneto
Member #8,643
May 2007

Firefox 3.0.4, Windows SP3, same problem as Neil. :/

In capitalist America bank robs you.

Mokkan
Member #4,355
February 2004
avatar

Hold the mouse button down when your cursor is over said text, and pull the cursor down. That will show you the underscores. I think it's something to do with the element that has the code in it not being tall enough.

EDIT: Holding your mouse over the text and scrolling the mouse wheel down works, too.

HardTranceFan
Member #7,317
June 2006
avatar

Firefox Portable (v 2.0.0.11) shows the underscores.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Matthew Leverton
Supreme Loser
January 1999
avatar

Firefox has a well known off-by-one rounding error that affects things like that.

So if you want to "fix" it, add something like this to your custom CSS:

.snippet {
  line-height:1.21 /* or some value that "fixes" it */
}

Evert
Member #794
November 2000
avatar

Doesn't seem to affect Firefox on OS X. I don't know, maybe they use a different rendering engine there.
I can indeed scroll the text by one pixel in Linux, but it doesn't prevent me from seeing the underscores.

Tobias Dammers
Member #2,604
August 2002
avatar

Probably a combination of Firefox, the default font size you selected in FF, and maybe your available system fonts. You can try and see if changing the zoom factor ([Ctrl] + [+] | [-]) makes any difference.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

LennyLen
Member #5,313
December 2004
avatar

Quote:

Probably a combination of Firefox, the default font size you selected in FF, and maybe your available system fonts.

It's not just an FF thing. The underscores vanish when the default font and size are used on Opera as well. On the 120% scale setting that I use, they're visible though.

Peter Wang
Member #23
April 2000

Quote:

I think I've seen also the following which I suppose is defined as follows (in wxWidgets).

#define UNUSED(x)

int main(int UNUSED(argc), char** UNUSED(argv))
{
}

As I see this doesn't try to outsmart the compiler (the other macro produces "statement has no effect" warnings, at least with GCC) and is purely informative - it shows that the arguments are not used, and also with the name what they would be used for if they did.

Too bad gcc issues an error:

t.c:1: error: parameter name omitted

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Too bad gcc issues an error:

I would have to guess that it only works in C++.

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

ixilom
Member #7,167
April 2006
avatar

Firefox portable 2.0.0.13, the underlines are visible for me at 100% zoom. They disappear if I zoom out one step, reappear if I zoom out two steps.

As for the B2_NOT_USED macros... eww!

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

anonymous
Member #8025
November 2006

Quote:

I would have to guess that it only works in C++.

Whoa, this was completely unexpected! :-X You can do all kinds of crazy stuff with function declarations in C but the compiler only allows it if their only purpose seems to be to introduce bugs and not if it is actually useful.

Well, as the original macro simply replaces one warning with another, perhaps this would be sufficient for the time being (until the compiler gets bold enough to warn about this too, because normally this can only indicate of a serious error):

#define UNUSED(x) x = x

Peter Wang
Member #23
April 2000

What's wrong with the canonical (void)x; ?

anonymous
Member #8025
November 2006

That would be OK (assuming that this is an established way to avoid statements without effect warnings, and not just something compilers are yet not bold enough to whine about).

 1   2 


Go to: