Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return something not exist

35 views
Skip to first unread message

asetof...@gmail.com

unread,
Sep 30, 2017, 2:39:59 AM9/30/17
to
If we have a class rc
For rc there is a constructor that
request memory from heap

Whap happen to this code at end of loop if the return statement is not executed? This code would/should compile?

rc f(rc v)
{int j;
for(j=0;j<100;++j)
if(operation(j))
{opera(&v);return v}
}

asetof...@gmail.com

unread,
Sep 30, 2017, 2:50:32 AM9/30/17
to
I say this because it seems that code (if I write some other ;)
compile run and when it return out from the loop : return one obj not ok at last the free() function I use says this

Barry Schwarz

unread,
Sep 30, 2017, 3:21:13 AM9/30/17
to
On Fri, 29 Sep 2017 23:39:47 -0700 (PDT), asetof...@gmail.com
wrote:
With the right set of options, your compiler should warn you that
there is a return path from f that does not return a value.

After executing a statement like
x = f(...);
any attempt to evaluate the value of x causes undefined behavior when
all executions of the if statement evaluate as false.

--
Remove del for email

Barry Schwarz

unread,
Sep 30, 2017, 3:27:22 AM9/30/17
to
On Fri, 29 Sep 2017 23:50:23 -0700 (PDT), asetof...@gmail.com
wrote:

>I say this because it seems that code (if I write some other ;)
>compile run and when it return out from the loop : return one obj not ok at last the free() function I use says this

If that incomprehensible sentence means free reports that the value is
not one that was returned by an appropriate allocation function, that
makes perfect sense. After 100 iterations of your for loop never
executing the return statement, the function exits without returning a
value. Attempting to evaluate the non-existent returned value causes
undefined behavior.

In practical terms, the value your calling function receives will
probably be whatever residual bit pattern was left in the stack where
the returned value would have been placed by a return statement. It
is extremely unlikely that this value is one that free can process
properly.

asetof...@gmail.com

unread,
Sep 30, 2017, 3:47:51 AM9/30/17
to
Barry wrote:
After executing a statement like
x = f(...);
any attempt to evaluate the value of x causes undefined behavior when
all executions of the if statement evaluate as false

---------
Yes as above ok..
in x=f(a) in what I would know: f return one obj , = is executed, than would be called the destructor for tmp obj (returned from f)
than the function that free the memory to the heap, free() says that the pointer is not right and it stop the program. I not understand why compile has to emit only one warning for that and not one error (at last for class obj)

Bo Persson

unread,
Oct 3, 2017, 9:53:01 AM10/3/17
to
if(operation(j))
{opera(&v);return v}



It's not an error if operation(j) happens to return true for some value
of j. The compiler might not be able to tell.


Bo Persson

0 new messages