Jump to content
xisto Community
dexter

Functions Failing When Embedded In If() Just a beserk problem thats plagued me on and off for years

Recommended Posts

I've had this problem on and off over the years (me just being silly, generally), so I thought I might share how to fix it. This has come up several time whilst using sockets, actually, so here's an example.

For instance,

if(hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)  <= 0) {  perror("socket() failure");  exit(1);}

What's the problem?

The problem is that,

hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)

should be encapsulated within parentheses.

Why?

When the call is made, a comparison between the return value and the 0 is made, and the boolean value (0 or 1) is then assigned to hd_, thus causing the value 0 to be stored in hd_ every time, but at the same time, skipping the error catching code.

So, instead:

if((hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP))  <= 0) {  perror("socket() failure");  exit(1);}

This will then work just as it's supposed to.
Watch yer parentheses when embedding... :P

Share this post


Link to post
Share on other sites

Ahem... I'm quite aware that socket() returns -1 for an error, and 0 is possible as a file descriptor value. The comparisons there should've actually been '<' not '<='. That was just from me trying to pin down the problem, and to show an example of how it can go wrong... neeeeed edit.

Share this post


Link to post
Share on other sites

WOow, interesting changes to the t17 skins.
Actually dexter, if you were like the rest of us, you'd stop assigning
the bind function to hd and you'd start testing if it was equal.

==, not =. So the code should read

if(hd_ == bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)  <= 0) { perror("socket() failure"); exit(1);}
Unless VC++ has some weird mechanism to understand = equals == in parentheses :P

Share this post


Link to post
Share on other sites

Erm... actually, looking back, I made an error. The call should've been socket(), not bind(), thus the perror("socket() failed"). I even mentioned the right call in the second post I made. :P

The value was being assigned to hd_ to keep the value of the socket that was created for the host...

And yeah, when I use bind, I don't assign the value to anything, I just do a comparison.

Unless VC++ has some weird mechanism to understand = equals == in parentheses :(


Nope. Don't touch VC++ these days. Too many non-standard featurs. :P Gcc is the only way to go.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.