473,772 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird bit of code-- legal???

A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

Sep 13 '06
29 2581
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamc op.netwrites:
Kenny McCormack wrote:
[the usual]
What's OT? What's non-portable? What's wrong? What's non-discussable
here?

Kenny McCormack is a self-proclaimed troll. Please ignore him. I
recommend killfiling him; you won't miss anything useful or
interesting.
Yes, but at least his "blah blah blah" posts are usually in response
to things that could be considered OT.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Sep 14 '06 #21
Old Wolf wrote:
>
Kenneth Brody wrote:
<gb******@excit e.comwrote:
>
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );
Now, the post is missing the definitions for all referenced names, but
assuming that TwoWayFunc and FourWayFunc are functions (or pointers to
functions) with the same prototype, there's nothing wrong here.

The functions don't have to have prototypes; this is accepted
by my compiler:

int foo();
int bar(int x) { return 0; }

int main()
{
return (0 ? foo : bar)(1);
}

int foo() { }
AFAIK, the above invokes UB, because you're calling a function
without a prototype in scope (foo) with the wrong parameters.

However, given that your compiler _probably_ uses the "caller
cleans up the stack" convention, the odds are that it will, in
fact, "work" on many platforms. (By "work", I mean "give the
expected results".)
In fact, this is also accepted and runs correctly, although I'm not
sure how well-defined it is:

#include <stdio.h>

int foo();

int bar(char *x) {
printf("bar: %p\n", x);
return 0;
}

int main()
{
return (getchar() == 'x' ? foo : bar)(0);
}

int foo(int x) {
printf("foo: %d\n", x);
return 0;
}
I will probably fail miserably on platforms with:

all-bits-zero being a trap representation for a pointer
or
sizeof(char *) != sizeof(int)

And, I assume that if you place the actual definition of foo()
above main, rather than the "function that takes an unknown
set of arguments" type, that the compiler complains about a
mismatch between foo() and bar()?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Sep 14 '06 #22
jaysome wrote:
On 13 Sep 2006 08:09:05 -0700, "Ancient_Hacker " <gr**@comcast.n et>
wrote:
>A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Para m1, Param2, Param3);
}
else
{
Result = FourWayFunc(Par am1, Param2, Param3);
}
Hey, that looks like a good job for a conditional expression!

result = Mode == 2 ? TwoWayFunc(Para m1, Param2, Param3) :
FourWayFunc(Par am1, Param2, Param3);

I presume nobody's scratching their head here. Whether you do that with an
if-statement or a conditional depends on how comfortable you are with
stuffing things on one line. We're assuming, of course, that "Param1" and
friends really are called that, and are not placeholders for two mile long
expressions with side effects.

But hold on, is "2" a magic value here? Wouldn't a variable called "Mode"
hold some sort of state parameter? Well then

switch (Mode) {
case 2: TwoWayFunc(Para m1, Param2, Param3); break;
default: FourWayFunc(Par am1, Param2, Param3); break;
}

is a nice way of emphasizing this. Now, "2" shouldn't be a magic value, so
toss in a #define or make Mode of enum type, but that's changing around
semantics.
The original "really good programmer" bit off more than he could chew,
from the standpont if understandabili ty and maintainability , as the
equivalent code should not cause your head to scratch at all.

In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
Must be nice to be able to cull the herd like that. I have to take the
occasional clue-by-four to my colleagues when I get called in to fix a
problem that's ultimately the result of bozotic code... admittedly not in C,
I don't think they're ready for that. ("What's wrong with an empty exception
handler? I really want to ignore errors here!")

S.
Sep 14 '06 #23

jaysome wrote:
On 13 Sep 2006 08:09:05 -0700, "Ancient_Hacker " <gr**@comcast.n et>
wrote:
A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Para m1, Param2, Param3);
}
else
{
Result = FourWayFunc(Par am1, Param2, Param3);
}

The original "really good programmer" bit off more than he could chew,
from the standpont if understandabili ty and maintainability , as the
equivalent code should not cause your head to scratch at all.
The original code shouldn't have caused anyone to do any head
scratching either, unlesss they are a junior level programmer who needs
to get up to speed in a hurry.
>
In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
So the world in which you deal is loaded with junior programmers who
have no business being anywhere near safety critical software. Dumbing
down the coders is not the solution. You need better programmers.
>
Best regards
--
jay
Sep 14 '06 #24
jaysome wrote:
> Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Para m1, Param2, Param3);
}
else
{
Result = FourWayFunc(Par am1, Param2, Param3);
}
It isn't exactly equivalent. In your version of the conditional
expression, you can put functions prototyped with any parameters, not
only functions which take three parameters of the types of Param1,
Param2, Param3.

The original example in the compact syntax actually puts a compile-time
constraint on this, and your "equivalent " syntax does not.

Sep 14 '06 #25
jaysome wrote:
In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
If I told you that the compact syntax made it possible for us to
continue using a code generator, or if I told you it helped
significantly in debugging, or something of that nature, you'd still
show me the door? After I'd been working for you for 12 years?

Please think before you go off about how low your standards are for
firing someone. Nobody who had a choice, would want to work for you if
you really had a policy like that.

In another post, I gave you a reason why your "equivalent " code isn't
exactly. Of course, the difference between code you got from me, and
the code in the example, is that my code would have comments.

Forgive me, I haven't had my coffee today, and as it turns out, I'm
rewriting someone else's bad code this morning.

James
Sep 14 '06 #26
pete <pf*****@mindsp ring.comwrites:
[...]
Kenny can't remember anything.
If he could, then he would remember that he's always wrong
when he tries to be topical on this newgroup,
and that that's why he decided to become a troll.
And that's why *he should be ignored*. Please stop feeding the troll.
And please ignore whatever idiocy he posts in response to this.

I recommend killfiling him. You won't miss anything useful or
interesting.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 14 '06 #27
Kenneth Brody wrote:
Old Wolf wrote:

The functions don't have to have prototypes; this is accepted
by my compiler:

int foo();
int bar(int x) { return 0; }

int main()
{
return (0 ? foo : bar)(1);
}

int foo() { }

AFAIK, the above invokes UB, because you're calling a function
without a prototype in scope (foo) with the wrong parameters.
Well, foo is never called in the above example.
In fact, this is also accepted and runs correctly, although I'm not
sure how well-defined it is:

#include <stdio.h>

int foo();

int bar(char *x) {
printf("bar: %p\n", x);
return 0;
}

int main()
{
return (getchar() == 'x' ? foo : bar)(0);
}

int foo(int x) {
printf("foo: %d\n", x);
return 0;
}

I will probably fail miserably on platforms with:

all-bits-zero being a trap representation for a pointer
or
sizeof(char *) != sizeof(int)
Why does the compiler decide that 0 is a pointer and
not an integer, in the case of calling foo?
>
And, I assume that if you place the actual definition of foo()
above main, rather than the "function that takes an unknown
set of arguments" type, that the compiler complains about a
mismatch between foo() and bar()?
Yes, because the definition of foo serves as a prototype.

Sep 15 '06 #28
On Thu, 14 Sep 2006 07:59:50 -0700, jmcgill
<jm*****@email. arizona.eduwrot e:
>jaysome wrote:
>> Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Para m1, Param2, Param3);
}
else
{
Result = FourWayFunc(Par am1, Param2, Param3);
}

It isn't exactly equivalent. In your version of the conditional
expression, you can put functions prototyped with any parameters, not
only functions which take three parameters of the types of Param1,
Param2, Param3.

The original example in the compact syntax actually puts a compile-time
constraint on this, and your "equivalent " syntax does not.
If function prototypes are required, I don't see how your argument
holds. This is valid C:

int TwoWayFunc(int, int,int);
int FourWayFunc(int ,int,int);
int TwoWayFunc_v(in t, ...);
int FourWayFunc_v(i nt, ...);
int Mode;
int Result;
int main(void)
{
int Param1 = 0;
int Param2 = 0;
int Param3 = 0;
Result = (Mode == 2 ? TwoWayFunc : FourWayFunc ) (Param1,
Param2, Param3 );
Result = (Mode == 2 ? TwoWayFunc_v : FourWayFunc_v) (Param1,
Param2, Param3 );
return 0;
}

Best regards
--
jay
Sep 15 '06 #29
On Thu, 14 Sep 2006 08:13:16 -0700, jmcgill
<jm*****@email. arizona.eduwrot e:
>jaysome wrote:
>In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.

If I told you that the compact syntax made it possible for us to
continue using a code generator, or if I told you it helped
significantl y in debugging, or something of that nature, you'd still
show me the door? After I'd been working for you for 12 years?
When I referred to that "really good programmer", I assumed it was
clear that I was talking about a real person, not some robotic code
generator. Nevertheless, even a code generator has to be programmed by
some real person. And the nature of the code that said code generator
generates says a lot about the person programming the code generator.

In this case, I cannot think of any good reason why such a code
generator would generate code like this, let alone how such code could
"significan tly" help in debugging. In the world of safety critical
software, tools like code generators may need to be verified as if a
real person wrote the code.
>Please think before you go off about how low your standards are for
firing someone. Nobody who had a choice, would want to work for you if
you really had a policy like that.
Of course.
>In another post, I gave you a reason why your "equivalent " code isn't
exactly. Of course, the difference between code you got from me, and
the code in the example, is that my code would have comments.
I'm skeptical that you did. But comments are, of course, good.
>Forgive me, I haven't had my coffee today, and as it turns out, I'm
rewriting someone else's bad code this morning.
Forgive me too. I may not have had my coffee today either :^)

Best regards
--
jay

Sep 15 '06 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1909
by: SenderX | last post by:
I am a basically a life-long Win32 C programmer specializing in high-end winsock servers. Before I dive into portable C++ programming, I wanted to code up an example of what I thought would be a workable Collection template library. The idea is to design API's that operate on interfaces, and provide multi-implementations to that "single" API. C++ Interfaces and Implementations, seems to behave like Win32 COM objects?
9
1480
by: Marco Jez | last post by:
Is it legal, in a class template's member function, to instantiate the same class template but with a different type as argument? The following code is very simple and I'd expect it to compile, but it makes both MSVC and gcc freeze and never return. Sometimes I get an "internal compiler error" instead. struct Instance_base { virtual ~Instance_base() {} virtual Instance_base* mkptr() = 0;
8
1436
by: Deano | last post by:
Here's the code; Private Sub txtTeachName_LostFocus() If IsNull(Me.txtName) Then 'line A Forms!frmMainform!frmSubform.Locked = True GoTo Exit_txtName Else 'line B Forms!frmMainform!frmSubform.Locked = False 'line C
30
2143
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.