what does this do?
Neil Walker

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.

Evert
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

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

Neil Walker

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

Thomas Harte

It has underscores in Safari.

Thomas Fjellstrom

Works fine in Konqueror 4 as well.

someone972

IE 7 shows them fine.

gnolam

Neil: Your Firefox is broken then.

someone972

Perhaps it is a font issue?

Neil Walker

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

anonymous

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

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

Mokkan

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

Firefox Portable (v 2.0.0.11) shows the underscores.

Matthew Leverton

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

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

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.

LennyLen
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
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
Quote:

Too bad gcc issues an error:

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

ixilom

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!

anonymous
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

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

anonymous

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

Neil Walker

I would have thought this would be a warning not an error (unless you have warnings to error enabled, or whatever the flag is).

As for the display, resizing the font does nothing, however bizarrely if I drag/select more than about 5 characters they all appear and stay, less and they don't.

Milan Mimica

In C++ you can just omit the varname if you won't use it.

Karadoc ~~

I really don't see the point of those macros. Is there something wrong with
int main(void)?

I only put in argc and argv if I intend to use them.

Neil Walker

maybe gcc complains if you don't supply them, but also complains when you don't use them :)

Roy Underthump

Actually the int main(int argv, char **argv) is incorrect also as it implies the pointers to the args are variable, as opposed to *argv[]. Slightly easier to key in though.

gnolam
Roy Underthump said:

Actually the int main(int argv, char **argv) is incorrect also as it implies the pointers to the args are variable, as opposed to *argv[]. Slightly easier to key in though.

Err... what? In function prototypes, *foo and foo[] are identical.

Karadoc ~~

gcc does not complain to me about int main(void). (MinGW, gcc version 3.4.2)

Roy Underthump
Quote:

In function prototypes, *foo and foo[] are identical.

IIRC I had a problem with **argv a few years ago, I can't reproduce it now, sorry

Evert
Quote:

gcc does not complain to me about int main(void). (MinGW, gcc version 3.4.2)

Any compiler that does is broken. "int main(void)" and "int main(int, char**)" are both standard C (unlike "void main").

Karadoc ~~

Good! But then I'm back to my original question: what is the point of the do_nothing macro? Why not just not declare the variables?

Evert

Good question. Not knowing about "int main(void)" is one possibility.
There is another one I can think of: SDL, for instance, requires that main has the prototype "int main(int, char**)" for more or less the same reason that Allegro requires END_OF_MAIN().

anonymous

I suppose it has very little use in main, as both signatures are legal. User-defined functions are different though: you might want to have a family of functions with identical signature, except some of these functions might actually not do anything with the arguments.

In C++ for example you might have a class hierarchy where some derived classes don't have a need for arguments to some overridden methods (but you absolutely have to keep the same signature, otherwise it wouldn't be an override). E.g in GUI programming you might often want to ignore additional information about an event - simply knowing what happened is enough, and you don't care where or to whom etc it happened.

Karadoc ~~

Ok. Those sound like good possible reasons. Thanks. :)

CGamesPlay

I like -Wno-unused-parameters better.

Thread #598629. Printed from Allegro.cc