473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is there a default result?

The code as follows:

#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}

int main()
{
int a;

a = fun( fun( 9, 8 ), fun( 7, 8 ) );
printf( "%d\n", a );

return 0;
}

It is obvious that the value of fun( 9, 8 ) is 9. But, the definition of fun
has no logic to get this result.
May 14 '07 #1
8 1403
wmaple wrote:
The code as follows:

#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}
If `x >= y`, then the result of calling this function is
an unspecified value and any use of it yields undefined
behaviour. A decent compiler will tell you something's
ikky.

eg:
gcc -W -ansi -pedantic -Wall oops.c
oops.c: In function 'fun':
oops.c:6: warning: control reaches end of non-void function

The warning text says you can get out of the function without
returning a value.
int main()
{
int a;

a = fun( fun( 9, 8 ), fun( 7, 8 ) );
printf( "%d\n", a );

return 0;
}

It is obvious that the value of fun( 9, 8 ) is 9. But, the definition of fun
has no logic to get this result.
That's right. So your `main` can print anything it likes, including
the right answer, 1829, a Shakespearian sonnet, or a treatise on the
unification of QM and GR. Or delete all your files.

What's /probably/ happening is that when the test in `fun` fails,
the value in some machine register gets returned as the function
result -- like the value of `x`, which could easily be in a register
because (a) it's the first argument to the function or (b) it had
to be loaded into that register to do `x < y` or (c) something else
equally implementation-specific. If that happens to be the right
answer, you will be deceived into thinking everything's OK.

Stricture: turn up warning levels and heed the messages.

--
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 14 '07 #2
"wmaple" <wm****@126.comschrieb im Newsbeitrag
news:f2**********@news.cn99.com...
The code as follows:

#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}

int main()
{
int a;

a = fun( fun( 9, 8 ), fun( 7, 8 ) );
printf( "%d\n", a );

return 0;
}

It is obvious that the value of fun( 9, 8 ) is 9. But, the definition of
fun
Huh? It is obvious that fun(7,8) returns 8. fun(9,8) returns whatever
garbage is fiund on the stack, which may by pure luck be 9
has no logic to get this result.
Bye, Jojo
May 14 '07 #3
wmaple wrote:
#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}

int main()
{
int a;

a = fun( fun( 9, 8 ), fun( 7, 8 ) );
printf( "%d\n", a );

return 0;
}
Hence:

gcc -Wall t.c
t.c: In function 'fun':
t.c:6: warning: control reaches end of non-void function

--
Paul
May 14 '07 #4
Joachim Schmitz <no************@hp.comwrote:
int fun( int x, int y)
{
if ( x < y ) return y;
}
Huh? It is obvious that fun(7,8) returns 8. fun(9,8) returns whatever
garbage is fiund on the stack, which may by pure luck be 9
<pedantry>There might not be a stack, so one cannot say precisely
where the garbage comes from. The DS9K, for example, delivers the
garbage that Sarah Cynthia Sylvia Stout did not take out.</pedantry>

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
May 14 '07 #5
In article <f2**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@faeroes.freeshell.orgwrote:
>Joachim Schmitz <no************@hp.comwrote:
int fun( int x, int y)
{
if ( x < y ) return y;
}
>Huh? It is obvious that fun(7,8) returns 8. fun(9,8) returns whatever
garbage is fiund on the stack, which may by pure luck be 9

<pedantry>There might not be a stack, so one cannot say precisely
where the garbage comes from. The DS9K, for example, delivers the
garbage that Sarah Cynthia Sylvia Stout did not take out.</pedantry>
We don't even need to invoke the DS9k for this one. Several (most?) real
implementations return values in registers, not on the stack, so the
return value won't be whatever garbage happens to be found on the stack.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I like "fun" risks, rather than "lazy" risks.
(If I'm going to kill myself, I want to have a good time on the way.)
--Graham Reed in the scary devil monastery
May 14 '07 #6
Chris Dollin <chris.dol...@hp.comwrote:
wmaple wrote:
The code as follows:

#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}

If `x >= y`, then the result of calling this function is
an unspecified value and any use of it yields undefined
behaviour.
<nitpick>
An unspecified value would not result in undefined behaviour.
The result is not unspecified, not even indeterminate. Rather,
no value is returned. In itself, that is legitimate under all
C standards. However, as you say, if the caller attempts to
use the return value when no such value is returned, then the
behaviour is undefined.
</nitpick>

--
Peter

May 14 '07 #7
On Mon, 14 May 2007 17:16:49 +0200, Paul Uiterlinden
<pu*****@notaimvalley.nlwrote:
>wmaple wrote:
>#include <stdio.h>

int fun( int x, int y)
{
if ( x < y ) return y;
}

int main()
{
int a;

a = fun( fun( 9, 8 ), fun( 7, 8 ) );
printf( "%d\n", a );

return 0;
}

Hence:

gcc -Wall t.c
t.c: In function 'fun':
t.c:6: warning: control reaches end of non-void function
And a few others:

VC++ 6.0:
fun.c(6) : warning C4715: 'fun' : not all control paths return a value

VS 2005:
fun.c(6) : warning C4715: 'fun' : not all control paths return a value

TI Code Composer Studio:
"fun.c", line 6: warning: missing return statement at end of non-void
function "fun"

PC-lint:
fun.c(6) : Warning 533: function 'fun(int, int)' should return a value
(see line 3)

Metrowerks CodeWarrior:
Warning: return value expected
fun.c line 7 }

If there are any compilers that do not issue a warning (especially
with the warning level at its highest), I'd be pleasantly surprised.
--
jay
May 15 '07 #8
Joachim Schmitz wrote:
"wmaple" <wm****@126.comschrieb im Newsbeitrag
.... snip ...
>
>It is obvious that the value of fun( 9, 8 ) is 9. But, the
definition of fun

Huh? It is obvious that fun(7,8) returns 8. fun(9,8) returns
whatever garbage is fiund on the stack, which may by pure luck be
9
No. That case is totally undefined. There may not even be any
such thing as a stack.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
3
by: jonny.longrigg | last post by:
Hi I'm having some trouble with a function I've written in Python: def myFunction(l1,l2,result=): index=0 for i in l1: result.append() if type(i)==list: myFunction(i,l2,result)
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
4
by: Samuel R. Neff | last post by:
I'm writing an xslt in vs.net 2003 and in order to get intellisense on the html content I added the default namespace declaration xmlns="http://schemas.microsoft.com/intellisense/ie5". However,...
4
by: Steven D'Aprano | last post by:
I'm having problems passing a default value to the maxsplit argument of str.split. I'm trying to write a function which acts as a wrapper to split, something like this: def mysplit(S, sep=None,...
7
by: tlyczko | last post by:
I have a char(11) for SSN, and I would like to default it to 123-45-6789 so I can avoid having nulls in this column, and so I can easily find the rows in which I need to have a 'correct' SSN...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
3
by: cj2 | last post by:
In the code below I want when this web service is first published for the boolean Running.running to be True. It seems to default to false. How can I make this default to True? Once the web...
3
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.