473,387 Members | 1,517 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,387 software developers and data experts.

functions with no side-effects

Consider the following illustrative program:

#include <stdio.h>

double f(double x)
{
return x*x;
}

double g(double x)
{
puts("hello world");
return x*x;
}

int main(void)
{
double s = 0.0;
int i;

for (i=0; i<10000; i++)
s += f(2.0); /* f(2.0) is always 4.0 */

for (i=0; i<10000; i++)
s += g(2.0); /* g(2.0) is always 4.0 */

printf("s = %g\n", s);
return 0;
}

Does standard C provide a mechanism whereby one can reassure the
compiler that the function f() produces no side-effects while the
function g() has side-effects?

If such a mechanism existed, then an optimizing compiler might replace
f(2.0) by 4.0 to avoid 10000 function calls, but it wouldn't replace
g(2.0) by 4.0.

Something in the back of my mind tells me that such a mechanism exists
but I may be (a) just imagining it; (b) thinking of a non-standard
compiler; or (c) a language other than C.

I would appreciate it if someone would set me straight.

--
Rouben Rostamian <ro*******@umbc.edu>
Nov 13 '05 #1
9 6854
ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
Does standard C provide a mechanism whereby one can reassure the
compiler that the function f() produces no side-effects while the
function g() has side-effects?
No. There is a GNU C extension for that, but it's non-standard.

The `inline' keyword in C99 may help, however.
If such a mechanism existed, then an optimizing compiler might replace
f(2.0) by 4.0 to avoid 10000 function calls, but it wouldn't replace
g(2.0) by 4.0.


In the code you presented, the compiler can easily tell that f()
has no side effects, because it has already seen f() when it
compiles the call to it.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #2
Rouben Rostamian wrote:
Does standard C provide a mechanism whereby one can reassure the
compiler that the function f() produces no side-effects while the
function g() has side-effects?


There's isn't a standard mechanism for doing this, although some
compilers have extensions. GCC, for example, supports so-called
"function attributes" which allow you to supply this information to
the compiler:

http://gcc.gnu.org/onlinedocs/gcc-3....n%20Attributes

(see "const" and "pure" in particular). In trivial cases, such as the
ones in your example, an optimizing compiler should be able to work
such things out by itself, but it's not always possible:

int every(int predicate(const void *),
const void *const*vals,
size_t nvals)
{
for (size_t i = 0; i < nvals; i++) {
if (!predicate(vals[i])) {
return 0;
}
}
return 1;
}

"every" itself doesn't have any side effects, but the function called
through "predicate" might; it can't be determined at compile-time in
general. If you have some way of telling the compiler that
"predicate" has no side effects then it could, in theory, do all sorts
of interesting optimizations, including memoizing both the "predicate"
and "every" functions, avoiding the overhead of calling them multiple
times with the same arguments. I doubt that any C compilers actually
do that sort of thing (unless the argument values are known at
compile-time), but it's by no means unknown in functional languages
(where side-effect free functions are more common).

Jeremy.
Nov 13 '05 #3
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> schrieb im Newsbeitrag
news:bm************@ID-114079.news.uni-berlin.de...
http://gcc.gnu.org/onlinedocs/gcc-3....s.html#Functio
n%20Attributes
(see "const" and "pure" in particular). In trivial cases, such as the
ones in your example, an optimizing compiler should be able to work
such things out by itself, but it's not always possible:

int every(int predicate(const void *),
const void *const*vals,
size_t nvals)
{
for (size_t i = 0; i < nvals; i++) {
if (!predicate(vals[i])) {
return 0;
}
}
return 1;
}

"every" itself doesn't have any side effects, but the function called
through "predicate" might; it can't be determined at compile-time in
general. If you have some way of telling the compiler that
"predicate" has no side effects then it could, in theory, do all sorts
of interesting optimizations, including memoizing both the "predicate"
and "every" functions, avoiding the overhead of calling them multiple
times with the same arguments. I doubt that any C compilers actually
do that sort of thing (unless the argument values are known at
compile-time), but it's by no means unknown in functional languages
(where side-effect free functions are more common).

That should be no problem. If one could apply such attributes to function
pointer too,
then the compiler would know wheather side effects are possible or not:

int every(int predicate(pure const void *), /* note "pure" in the
declaration of the function pointer */
const void *const*vals,
size_t nvals)
{
for (size_t i = 0; i < nvals; i++) {
if (!predicate(vals[i])) {
return 0;
}
}
return 1;
}

if the function pointer is declared without pure one can pass every function
he likes, and the compiler must assume that
every() has side effects. But if you declare the function pointer as pure
one can only pass pure functions to every() and the compiler
can assume that every() doesn't have any side effects.

i don't know wheather or not that is the case, if not consider this as a
proposal for future compilers.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #4
In <87************@pfaff.stanford.edu> Ben Pfaff <bl*@cs.stanford.edu> writes:
ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
Does standard C provide a mechanism whereby one can reassure the
compiler that the function f() produces no side-effects while the
function g() has side-effects?


No. There is a GNU C extension for that, but it's non-standard.

The `inline' keyword in C99 may help, however.


How? What is preventing a C99 inline function from calling printf?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
Does standard C provide a mechanism whereby one can reassure
the compiler that the function f() produces no side-effects
while the function g() has side-effects?


No. There is a GNU C extension for that, but it's non-standard.

The `inline' keyword in C99 may help, however.


How? What is preventing a C99 inline function from calling printf?


I think you misconstrued Bens comments. If the function is
'inline' the compiler is generating its code within the piece that
calls it, so it can look at the generated sequence and see that
there are no side effects, without any help.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #6
In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
>
>> Does standard C provide a mechanism whereby one can reassure
>> the compiler that the function f() produces no side-effects
>> while the function g() has side-effects?
>
> No. There is a GNU C extension for that, but it's non-standard.
>
> The `inline' keyword in C99 may help, however.


How? What is preventing a C99 inline function from calling printf?


I think you misconstrued Bens comments. If the function is
'inline' the compiler is generating its code within the piece that
calls it, so it can look at the generated sequence and see that
there are no side effects, without any help.


It is not inline that magically does that, but the fact that the function
definition is visible to the compiler. You don't need inline in order to
make the function definition visible to the compiler.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
Dan Pop wrote:

In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
>
>> Does standard C provide a mechanism whereby one can reassure
>> the compiler that the function f() produces no side-effects
>> while the function g() has side-effects?
>
> No. There is a GNU C extension for that, but it's non-standard.
>
> The `inline' keyword in C99 may help, however.

How? What is preventing a C99 inline function from calling printf?


I think you misconstrued Bens comments. If the function is
'inline' the compiler is generating its code within the piece that
calls it, so it can look at the generated sequence and see that
there are no side effects, without any help.


It is not inline that magically does that, but the fact that the function
definition is visible to the compiler. You don't need inline in order to
make the function definition visible to the compiler.


In theory, yes. However, do you know of any compilers that go to
the trouble of characterizing each function in that manner? I
suspect they all limit their knowledge to whatever is carried in
the prototype.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #8
In message <3F***************@yahoo.com>
CBFalconer <cb********@yahoo.com> wrote:
Dan Pop wrote:

It is not inline that magically does that, but the fact that the function
definition is visible to the compiler. You don't need inline in order to
make the function definition visible to the compiler.


In theory, yes. However, do you know of any compilers that go to
the trouble of characterizing each function in that manner? I
suspect they all limit their knowledge to whatever is carried in
the prototype.


I've not seen one. However, the majority of "pure" functions in practice are
likely to be ones small enough for a real-world compiler to consider
auto-inlining, which would achieve a similar effect.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #9
In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:

In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
>Dan Pop wrote:
>> Ben Pfaff <bl*@cs.stanford.edu> writes:
>> > ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
>> >
>> >> Does standard C provide a mechanism whereby one can reassure
>> >> the compiler that the function f() produces no side-effects
>> >> while the function g() has side-effects?
>> >
>> > No. There is a GNU C extension for that, but it's non-standard.
>> >
>> > The `inline' keyword in C99 may help, however.
>>
>> How? What is preventing a C99 inline function from calling printf?
>
>I think you misconstrued Bens comments. If the function is
>'inline' the compiler is generating its code within the piece that
>calls it, so it can look at the generated sequence and see that
>there are no side effects, without any help.


It is not inline that magically does that, but the fact that the function
definition is visible to the compiler. You don't need inline in order to
make the function definition visible to the compiler.


In theory, yes. However, do you know of any compilers that go to
the trouble of characterizing each function in that manner? I
suspect they all limit their knowledge to whatever is carried in
the prototype.


They don't have to do it for inline functions, either. Why should a
compiler care whether an inline function has side effects or not? The
reasons, whatever they are, apply to both kinds of functions.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

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

Similar topics

1
by: The Plankmeister | last post by:
Hi... I'm writing a website builder. There are two 'halves' of it, codewise: The admin pages and the public pages. I intend to use sessions to hold information about validated users (to modify...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
15
by: Paul J. Ettl | last post by:
Two questions: I use var date1 = new Date(); to get todays date. But how can I get yesterdays date? Furthermore I use
13
by: yohji | last post by:
Hi,everyone! I am a beginner of C.When I studied C,I found a problem which isn't described in many C teaching books.It is like this: I found some of int or char* functions can be used as void...
2
by: Andre | last post by:
I've set up a CustomValidator control which has both a client side and server side function associated with it. My server side function never seems to be called as long as I have the...
2
by: anoop | last post by:
Hello, I created a Public class in .aspx.vb code behind file, now I want to know can I call that functions in the class in the Scripts either client side or server side in .aspx page. also I want...
4
by: qualitynice | last post by:
HELP :-)... I'm creating an embedded activex object in an asp.net page using the HtmlGenericControl class. I'm doing this because when I tried to embed it directly in the aspx page, it stopped...
8
by: Gary | last post by:
Hello, I am currently new to .NET, as you can probably tell by my last two or three threads!!! Anyway, I am using code behind as much as possible - and am curious about the way in which...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...

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.