Returning an assignment?

S

signuts

Given this line:


if (!constmap_init(&maprh, pm.s, pm.len, 0)) return flagrh = -1;

What happens if constmap(..) returns 0? flagrh is a static global, and
could have any value in it. Is flagrh assigned -1 then returned, or is it
the other order, or possibly even undefined behavior?

Only reason I ask is i'm tracking down a bug and am unable to reproduce it
on my system.

Thanks
 
N

Nudge

signuts said:
Given this line:

if (!constmap_init(&maprh, pm.s, pm.len, 0)) return flagrh = -1;

What happens if constmap(..) returns 0? flagrh is a static global, and
could have any value in it. Is flagrh assigned -1 then returned, or is it
the other order, or possibly even undefined behavior?

I'll give it my best shot.

Your code is equivalent to:

if (constmap_init(&maprh, pm.s, pm.len, 0) == 0)
{
flagrh = -1;
return -1;
}
 
I

Ivan Vecerina

| if (!constmap_init(&maprh, pm.s, pm.len, 0)) return flagrh = -1;
|
| What happens if constmap(..) returns 0? flagrh is a static global, and
| could have any value in it. Is flagrh assigned -1 then returned, or is it
| the other order, or possibly even undefined behavior?

When executing:
return flagrh = -1
-1 is first assigned to flagrh. Then the value of flagrh is returned.
The behavior is well-defined.

| Only reason I ask is i'm tracking down a bug and am
| unable to reproduce it on my system.

The only thing I would check is the type of flagrh.
Say for example that it is defined as:
static unsigned char flagrh;
the return value might not be -1.

Other than that, I see nothing wrong in this one line...

hth -Ivan
 
E

Eric Sosman

Nudge said:
I'll give it my best shot.

Your code is equivalent to:

if (constmap_init(&maprh, pm.s, pm.len, 0) == 0)
{
flagrh = -1;
return -1;
}

Almost right, but not quite. For example, consider

int foo(void) {
static unsigned char flagrh;
if (that_big_mess)
return flagrh = -1;
...
}

The returned value is UCHAR_MAX, not -1: a positive
value rather than a negative.

To the O.P.: The construct you see is perfectly
well-defined and perfectly legal. However, you might
want to check whether the `=' might be a misspelling
of `=='; it's just possible that the intent is not to
do an assign-and-return but a compare-and-return.
 
B

Barry Schwarz

Given this line:


if (!constmap_init(&maprh, pm.s, pm.len, 0)) return flagrh = -1;

What happens if constmap(..) returns 0? flagrh is a static global, and
could have any value in it. Is flagrh assigned -1 then returned, or is it
the other order, or possibly even undefined behavior?

If constmap returns 0, the if evaluates to true. The return statement
is executed. flagrh is first assigned the appropriate value of -1 (it
depends on type of flagrh (integer/floating, signed/unsigned, etc).
Then the new value in flagrh is converted to the return type and sent
back to the calling function.
Only reason I ask is i'm tracking down a bug and am unable to reproduce it
on my system.

Thanks



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top