473,943 Members | 4,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this return UB ?

Is this return UB ?
int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc

some thoughts ?

Thanks !
Joe
WB2JQT

----------------------
I tought my wife to drive a stick.
She learned to drive the broom all on her own.
Jul 26 '07 #1
12 1380
dada said:
Is this return UB ?
int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc
That's fine, until you hit INT_MAX. If you don't plan to get that high,
no problem.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 26 '07 #2
Richard Heathfield wrote:
dada said:

>>Is this return UB ?
int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc


That's fine, until you hit INT_MAX. If you don't plan to get that high,
no problem.
Thanks Richard.
I think there is more logic in the actual function for wrap around,
but the gist of the question was about what was returned.

Joe
Jul 26 '07 #3
dada <jb*****@stny.r r.comwrote:
Richard Heathfield wrote:
dada said:
>Is this return UB ?

int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc
That's fine, until you hit INT_MAX. If you don't plan to get that high,
no problem.

I think there is more logic in the actual function for wrap around,
but the gist of the question was about what was returned.
Sorry, but actually you didn't ask a question (or perhaps there
is but my English isn't up to it). If the question is what the
function returns on the very first call then the answer is 0.
And the return value the second time you call it is 1 (the post-
fix ++ operator is only applied after the value of the expres-
sion has been evaluated).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jul 27 '07 #4
Jens Thoms Toerring said:
dada <jb*****@stny.r r.comwrote:
>Richard Heathfield wrote:
dada said:

Is this return UB ?

int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc

That's fine, until you hit INT_MAX. If you don't plan to get that
high, no problem.

I think there is more logic in the actual function for wrap around,
but the gist of the question was about what was returned.

Sorry, but actually you didn't ask a question (or perhaps there
is but my English isn't up to it).
Well, actually he did - he asked if the return statement invoked "UB" -
i.e. undefined behaviour (see above). It doesn't, as long as overflow
doesn't occur. He also pointed out what he wanted. My answer ("That's
fine..." - see above) covered both his explicit question and his
implicit request. Sorry if I didn't make this clearer.
If the question is what the
function returns on the very first call then the answer is 0.
And the return value the second time you call it is 1 (the post-
fix ++ operator is only applied after the value of the expres-
sion has been evaluated).
Er, kindasortakinda , yes. Actually, there's nothing to stop the compiler
doing something like:

INC I
MOV R1, I
DEC R1
RET

(other than the bounds of common sense, of course).

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 27 '07 #5
declare
static int var = 0;
as global variable not auto variable.....

and
return ( var++ )
now each calling will increment it

for eg.

#include<stdio. h>
static int x = 0;
int ret( void );

int main( void )
{
int i;
for ( i = 0; i < 100; i++ )
{
ret( );
printf( "%d\n", x );
}
_getch( );
return ( 0 );
}

int ret( void )
{
return( x ++ );
}

Jul 27 '07 #6
@$|-|. DUBEY said:
declare
static int var = 0;
as global variable
Why? What benefit does it confer that overcomes the disadvantage of its
being defined at file scope?
not auto variable.....
He didn't define it as an auto object. Here's his code again:

int someFunt ()
{
static int i = 0 ;

return i++ ;
}

i has static storage duration, not automatic storage duration.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 27 '07 #7
Richard Heathfield <rj*@see.sig.in validwrites:
Jens Thoms Toerring said:
>dada <jb*****@stny.r r.comwrote:
>>Richard Heathfield wrote:
dada said:

Is this return UB ?

int someFunt ()
{
static int i = 0 ;

return i++ ;
}

WHat we want is the first time to return 0, next 1, etc

That's fine, until you hit INT_MAX. If you don't plan to get that
high, no problem.

I think there is more logic in the actual function for wrap around,
but the gist of the question was about what was returned.

Sorry, but actually you didn't ask a question (or perhaps there
is but my English isn't up to it).

Well, actually he did - he asked if the return statement invoked "UB" -
i.e. undefined behaviour (see above). It doesn't, as long as overflow
doesn't occur. He also pointed out what he wanted. My answer ("That's
fine..." - see above) covered both his explicit question and his
implicit request. Sorry if I didn't make this clearer.
>If the question is what the
function returns on the very first call then the answer is 0.
And the return value the second time you call it is 1 (the post-
fix ++ operator is only applied after the value of the expres-
sion has been evaluated).

Er, kindasortakinda , yes. Actually, there's nothing to stop the compiler
doing something like:

INC I
MOV R1, I
DEC R1
RET

(other than the bounds of common sense, of course).
That is totally platform dependant and therefore ... you know.

As far as C goes, he was right. The ++ is applied to "i" after the value
of I is evaluated and handed to the return statement. There is no need
whatsoever to obfuscate this.

To the OP. You code does not exhibit "UB" until overflow. And then its
platform specific I believe.
Jul 27 '07 #8
On Fri, 27 Jul 2007 07:31:22 -0700, "@$|-|. DUBEY"
<du***********@ gmail.comwrote in comp.lang.c:
declare
static int var = 0;
as global variable not auto variable.....
No, there is no gain and some loss to changing his already correct
definition of a static object at function scope. While it is
permissible to initialize a static scalar object with 0, it is
unnecessary and redundant.
and
return ( var++ )
now each calling will increment it

for eg.

#include<stdio. h>
static int x = 0;
int ret( void );

int main( void )
{
int i;
for ( i = 0; i < 100; i++ )
{
ret( );
printf( "%d\n", x );
}
_getch( );
There is no such function as "_getch" in C.
return ( 0 );
"return" is a statement, not a function. Remove the parentheses.
}

int ret( void )
{
return( x ++ );
Again, remove the parentheses. They haven't been required for 18
years.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 28 '07 #9
On Jul 28, 3:05 pm, Jack Klein <jackkl...@spam cop.netwrote:
While it is permissible to initialize a static scalar
object with 0, it is unnecessary and redundant.
I think it helps the code readability.
Jul 29 '07 #10

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

Similar topics

3
4539
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong this time? This is the code the retrieves the values: if (($hasRegistered || $hasPreRegistered) && !empty($uplinenumber)) { // CHECK TO SEE IF UPLINE NUMBER IS A VALID NUMBER $regNumberGenerator = new RegNumberGenerator($uplinenumber,...
20
2372
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
4166
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and additon operator (which returns another Point object, and which my question is about) are as follows: Point::Point(int x, int y) : _x(x), _y(y) { }
2
2358
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last line is very long. I'm wondering if there is any way to suppress it. I can only think of typedef. But I'm not sure whether I can use typedef for the return type. Would you please help me? Please don't be daunted by the length of the code.
2
4686
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info Center: To return a result set from a procedure to the originating application, use the WITH RETURN TO CLIENT clause. When WITH RETURN TO CLIENT is specified on a result set, no nested procedures can access the result set.
15
6751
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int i = 0,j; j = sample(0);
10
19179
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using System; using System.Runtime.InteropServices; //
12
3820
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. 1. The Deserialization goes like: #Region " Load "
3
4562
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
6
2422
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class Card { // instance variables //suits private int suit; private int spades;
0
9970
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11131
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10665
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9865
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7391
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6310
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4912
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.