missing return

  • Thread starter Carlos Martinez Garcia
  • Start date
C

Carlos Martinez Garcia

Hi all:

I have a piece of code similar to this:

bool MyClass::myMethod() {
... (there is no return)
}

I have a random behaviour. It depends on if I'm debuging the code or
not. When I'm debuging it returns false, but in normal execution it
returns true.

I have no error/warnings when compile it. ¿Is it the right behaviour?

Thanks in advance
 
N

Neelesh Bodas

Carlos said:
Hi all:

I have a piece of code similar to this:

bool MyClass::myMethod() {
... (there is no return)
}

A function declared to return a value of specific type must return a
value of that type. Else it is undefined what value would be returned.
The only exception is "main" which implicitly returns 0.
 
R

Ron Natalie

Neelesh said:
A function declared to return a value of specific type must return a
value of that type. Else it is undefined what value would be returned.
The only exception is "main" which implicitly returns 0.
It's not only undefined what value might be returned, it's undefined
behavior period. While small things like bools are probably returned
via registers and just result in indeterminate values, things that the
compiler has to do more involved copy semantics for (like returning
classes) might cause the application to blow up.
 
S

Sigmathaar

When programming a multithread application sometimes there are problems
in the synchronisation of each thread. Most of the type a bad
synchornisation may result on a missing return code or a return without
any sense. For further aid you may post the code you are working at so
we can see what the problem is.
 
S

Sigmathaar

If you are programming a multithread application you may obtain a
return ucode sing the debug feature of your compiler because using the
debugger may give the thread the time for doing whatever it's supposed
to do.
 
H

Howard

Sigmathaar said:
When programming a multithread application sometimes there are problems
in the synchronisation of each thread. Most of the type a bad
synchornisation may result on a missing return code or a return without
any sense. For further aid you may post the code you are working at so
we can see what the problem is.

Please quote the text of the message you're responding to.

The OP stated that there was no return (statement) for the function that was
declared to return a bool. That makes the code exhibit undefined behavior.
Threading is not relevant to the issue. If you declare a function to return
a value, then it must in fact return a value via a return statement.

-Howard
 
D

Dervish

I have no error/warnings when compile it
Check for compiler warning level. You should get either Error: function
does not return, or Warning: not all control paths return a value.

Other possibility - you use really ancient compiler.
but in normal execution it returns true
Better to say: it returns true with some probability. Most likely
probability = 1/256.
 
I

izida9

better to say: it returns true with some probability. Most likely probability = 1/256.

The probability is 255/256 since every value different from zero is
interpreted as "true".
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top