Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   No return in LONG InterlockedDecrement ( LONG *lpAddend ) (http://www.velocityreviews.com/forums/t632538-no-return-in-long-interlockeddecrement-long-lpaddend.html)

slackcode 08-27-2008 05:22 AM

No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
I use MiniMFC in Linux. And found the function InterlockedDecrement
don't has the "return xxx":
LONG InterlockedDecrement ( LONG *lpAddend )
{
*lpAddend = *lpAddend - 1;
}

I write a test program like this function, and it return the value of
*lpAddend without the return sentence in the end of the function.
Maybe It return EAX by default, but I'm not sure about that.

CString of MiniMFC use this function like:
void CString::Release()
{
if (GetData() != _afxDataNil)
{
ASSERT(GetData()->nRefs != 0);
if (InterlockedDecrement(&GetData()->nRefs) <= 0)
FreeData(GetData());
Init();
}
}

So the return value of InterlockedDecrement is very important. And I
am not sure the return value and the CString::Release() will occur
some mistagke


slackcode 08-27-2008 05:23 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Sorry!

So the return value of InterlockedDecrement is very important. And I
am not sure the return value and the CString::Release() will occur
some mistake. Could some one can explain that? Thank you very much!

regards,
slackcode

Flash Gordon 08-27-2008 07:16 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
slackcode wrote, On 27/08/08 06:22:
> I use MiniMFC in Linux. And found the function InterlockedDecrement
> don't has the "return xxx":
> LONG InterlockedDecrement ( LONG *lpAddend )
> {
> *lpAddend = *lpAddend - 1;
> }
>
> I write a test program like this function, and it return the value of
> *lpAddend without the return sentence in the end of the function.
> Maybe It return EAX by default, but I'm not sure about that.


It happened to be somewhere that allowed you to pick it up by chance.
Change something apparently unrelated and that could change. Either make
the function return an appropriate or change the return type of the
function to void. Also see if you can turn up the warning level on your
compiler, some can warn about problems like this.

> CString of MiniMFC use this function like:
> void CString::Release()


<snip>

C++ and C are different languages with separate groups. Since
comp.lang.c++ is next to comp.lang.c in most newsgroup lists I find it
hard to understand how people miss the group and end up posting C++ here.
--
Flash Gordon

Nick Keighley 08-27-2008 08:02 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:

> I use MiniMFC in Linux. And found the function InterlockedDecrement
> don't has the "return xxx":
> LONG InterlockedDecrement ( LONG *lpAddend )
> {
> * * * * *lpAddend = *lpAddend - 1;
>
> }


that is an error. Or more formally "Undefined Behaviour".
The implementation can do whatever it likes.


> I write a test program like this function, and it return the value of
> *lpAddend *without the return sentence in the end of the function.


such as that

> Maybe It return EAX by default, but I'm not sure about that.
>
> CString of MiniMFC use this function like:
> void CString::Release()
> {
> * * * * if (GetData() != _afxDataNil)
> * * * * {
> * * * * * * * * ASSERT(GetData()->nRefs != 0);
> * * * * * * * * if (InterlockedDecrement(&GetData()->nRefs) <= 0)
> * * * * * * * * * * * * FreeData(GetData());
> * * * * * * * * Init();
> * * * * }
> }
>
> So the return value of InterlockedDecrement is very important. And I
> am not sure the return value and the *CString::Release() will occur
> some [mistake. can someone explain this?]


you have Undefined Behaviour

--
Nick Keighley

Programming should never be boring, because anything
mundane and repetitive should be done by the computer.
~Alan Turing


Ian Collins 08-27-2008 08:13 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Nick Keighley wrote:
> On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
>
>> I use MiniMFC in Linux. And found the function InterlockedDecrement
>> don't has the "return xxx":
>> LONG InterlockedDecrement ( LONG *lpAddend )
>> {
>> *lpAddend = *lpAddend - 1;
>>
>> }

>
> that is an error. Or more formally "Undefined Behaviour".
> The implementation can do whatever it likes.
>

Which really should be a constraint violation.

--
Ian Collins.

Richard Bos 08-27-2008 09:53 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Ian Collins <ian-news@hotmail.com> wrote:

> Nick Keighley wrote:
> > On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
> >
> >> I use MiniMFC in Linux. And found the function InterlockedDecrement
> >> don't has the "return xxx":
> >> LONG InterlockedDecrement ( LONG *lpAddend )
> >> {
> >> *lpAddend = *lpAddend - 1;
> >>
> >> }

> >
> > that is an error. Or more formally "Undefined Behaviour".
> > The implementation can do whatever it likes.
> >

> Which really should be a constraint violation.


It can't be; it is possible (with thanks to Gödel and related theorems)
to write a function in which all paths terminate in a return statement,
but the implementation isn't able to prove that.

Richard

James Kuyper 08-27-2008 10:42 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Richard Bos wrote:
> Ian Collins <ian-news@hotmail.com> wrote:
>
>> Nick Keighley wrote:
>>> On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
>>>
>>>> I use MiniMFC in Linux. And found the function InterlockedDecrement
>>>> don't has the "return xxx":
>>>> LONG InterlockedDecrement ( LONG *lpAddend )
>>>> {
>>>> *lpAddend = *lpAddend - 1;
>>>>
>>>> }
>>> that is an error. Or more formally "Undefined Behaviour".
>>> The implementation can do whatever it likes.
>>>

>> Which really should be a constraint violation.

>
> It can't be; it is possible (with thanks to Gödel and related theorems)
> to write a function in which all paths terminate in a return statement,
> but the implementation isn't able to prove that.


The standard could make it a constraint violation to leave out return
statements in certain locations, even if the programmer can figure out
that they will never be executed. If "certain locations" is specified
appropriately (which I will not attempt to do), then checking whether
this has been done would become feasible in all cases.

I don't think that this would be popular. Compilers which currently
perform a sufficiently sophisticated check for unreachable code might,
as a QoI issue, have to turn off the corresponding warning in cases
where leaving out the unreachable code would be a constraint violation
under the new rule.

Willem 08-27-2008 11:48 AM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Ian Collins wrote:
) Nick Keighley wrote:
)> On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
)>
)>> I use MiniMFC in Linux. And found the function InterlockedDecrement
)>> don't has the "return xxx":
)>> LONG InterlockedDecrement ( LONG *lpAddend )
)>> {
)>> *lpAddend = *lpAddend - 1;
)>>
)>> }
)>
)> that is an error. Or more formally "Undefined Behaviour".
)> The implementation can do whatever it likes.
)>
) Which really should be a constraint violation.

constraint violations are usually restricted to things that can be easily
checked by a compiler. Ensuring that a non-void function always returns a
value requires code path analysis at the very least.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Andrew Poelstra 08-27-2008 02:25 PM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
On 2008-08-27, Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
> Ian Collins <ian-news@hotmail.com> wrote:
>
>> Nick Keighley wrote:
>> > On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
>> >
>> >> I use MiniMFC in Linux. And found the function InterlockedDecrement
>> >> don't has the "return xxx":
>> >> LONG InterlockedDecrement ( LONG *lpAddend )
>> >> {
>> >> *lpAddend = *lpAddend - 1;
>> >>
>> >> }
>> >
>> > that is an error. Or more formally "Undefined Behaviour".
>> > The implementation can do whatever it likes.
>> >

>> Which really should be a constraint violation.

>
> It can't be; it is possible (with thanks to Gödel and related theorems)
> to write a function in which all paths terminate in a return statement,
> but the implementation isn't able to prove that.
>


Indeed. C# requires superfluous return statements, and it is a source
of great frustration, since the programmer needs to add a comment saying
that he's being program-illogical to appease the compiler.

--
Andrew Poelstra apoelstra@wpsoftware.com
To email me, use the above email addresss with .com set to .net

Ian Collins 08-27-2008 07:09 PM

Re: No return in LONG InterlockedDecrement ( LONG *lpAddend )
 
Willem wrote:
> Ian Collins wrote:
> ) Nick Keighley wrote:
> )> On 27 Aug, 06:22, slackcode <slackc...@gmail.com> wrote:
> )>
> )>> I use MiniMFC in Linux. And found the function InterlockedDecrement
> )>> don't has the "return xxx":
> )>> LONG InterlockedDecrement ( LONG *lpAddend )
> )>> {
> )>> *lpAddend = *lpAddend - 1;
> )>>
> )>> }
> )>
> )> that is an error. Or more formally "Undefined Behaviour".
> )> The implementation can do whatever it likes.
> )>
> ) Which really should be a constraint violation.
>
> constraint violations are usually restricted to things that can be easily
> checked by a compiler. Ensuring that a non-void function always returns a
> value requires code path analysis at the very least.
>

Ensuring a non-void function has at least one return statement isn't hard.

--
Ian Collins.


All times are GMT. The time now is 06:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.