Compiler missed an error

I just found a compiler error. I neglected the parens on a functional call and no error was generated and there also was no call to the function. This was in a sketch for a Pro Mini. I've been hunting this error all morning and, trust me, I've watched the IDE program and reprogram that device, so it's not just that I didn't notice that it had found the error, wasn't programming, and I wasn't paying attention. Also, I checked and there is no other definition or declared variable name "S_done" in the sketch.

As coded: S_done ;

As intended: S_done() ;

S_done() is a void function, eg,

void S_done() {
blah blah
}

That’s not a compiler error.

What you did was test that the function exists, and threw away the result.
The compiler will have issued a warning, along the lines of "statement is a reference, not call, to function “foo” "

It’s not a bootloader issue either.

Thanks. I wondered if the text alone was actually defined.

There was nothing "to throw away." It was a void function (actually a serial message). I still think it's an error. There is no coding reason to insert the text of a function without calling the function. The function was definitely not called. Counter examples welcome.

It’s exactly as if you had typed

void foobar ()
{
  8;

}

Albeit with a slightly different warning.

It’s perfectly legal C/C++.

If you don’t want the warning, just put (void) in front, to show the compiler who is boss.

You’ll notice in a lot of the examples in setup () something like if (!Serial)

Here, you’re not throwing away the result.

It’s not an error; it’s been around since the earliest days of K&R (PBUT) - get over it, and enable compiler warnings.

Thanks. I come from 6 other languages, so the fine points often elude me. I would have thought that your
if (!Serial)
would have required
#ifdef Serial
or maybe #if defined(Serial)
constructs.

As a person without formal training in C++, I could continue grousing by pointing out that I’ve seen a lot of “examples” written by experts that depend on obscure compiler rules that aren’t apparent in the code (or comments), but I’ll let you get back to real work.

I’ll take a look at compiler warnings and see if that would have saved my morning.

I have virtually no C or C++ training either, but I can see that having only six languages could be limiting.

Always remember that most older languages will allow you to shoot yourself in the foot; C hands you the gun, safety off, cocked, with a round in the chamber, pre-sighted on your shoe

Dr_Quark:
There was nothing “to throw away.”

There was - the Boolean that the function pointer was or was not null.

Dr_Quark:
I still think it's an error. There is no coding reason to insert the text of a function without calling the function. Counter examples welcome.

void FunctionName() {};
...
attachInterrupt(digitalPinToInterrupt(pin), FunctionName, RISING);

Here the attachInterrupt() function needs the address of the function to call when the interrupt occurs. You will get an error if you try to pass 'FunctionName()'.

In c/c++ a naked function name, like you provided, evaluates to a pointer to the function. i,e, - the ADDRESS of the function, which is just a number. As pointed out, it is a perfectly valid c expression. And some/many/most/nearly all other languages also allow for a valid expression, or statement, to be just a number. c started out life, in the mid-70s, as a low-level "system programming language". It was used to create itself, as well as the original unix operating system. So, it was deliberately designed to be very "close to the metal", and not even try to unduly protect the programmer from himself.

In the IDE, under File, Preferences, select "Compile warnings: All"

You would have seen the warning then.

"Compile warnings: All" will be annoying at first. Really annoying. But it will make you a better C++ programmer if you fix the warnings. (It will also highlight sloppy library coding).

Ray: "It was used to create itself, as well as the original unix operating system." Did you know that the first C compiler was written in C?

SteveMann:
Ray: "It was used to create itself, as well as the original unix operating system." Did you know that the first C compiler was written in C?

That's exactly what I was getting at...

Did you know that the first C compiler was written in C?

I once saw a headline on one of those newsprint rags at the checkout counter, about a woman who gave birth to herself.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.