Side Effect--What's This

What is a side effect in C?

My question is that does the C standard explicitly describe a meaning of side effects?

The sentence in the C standard you quote (C 1999 5.1.2.3 2, and the same in C 2018) explicitly describes the meaning of side effects. These are further explained below.

Modifying An Object

Modifying an object is understood to include the things that update the stored bytes that represent the object. I believe a complete list of them is:

  • Simple assignment (=) and compound assignment (*=, /= %=, +=, -=, <<=, >>=, &=, ^=, and |=).
  • The increment and decrement operators (++ and --), both prefix and postfix.
  • Initialization of an object included in its definition.
  • Library routines that are specified to change objects, such as memcpy.

Accessing a Volatile Object

“Access” is defined in C 2018 3.1 as “⟨execution-time action⟩ to read or modify the value of an object”. If x is a volatile int, then using the value of x in an expression accesses it (when the expression is evaluated), because it reads the value of x. You can follow this more specifically in that 6.3.2.1 2 tells us that the use of x in an expression results in the value of x being taken:

Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion.

So the x in the expression which is, by itself, just a designation of the object x, is converted to the value stored in x, which means that stored value is read from memory. That is an access of x.

Modifying a volatile object is the same as modifying any object, described above.

Modifying a File

Files are modified by way of the routines defined in clause 7.21 (“Input/output <stdio.h>”).

side-effects in functional programming (racket)

There are various possible definitions, but a common and enlightening one is that a side effect is anything that makes the order of evaluation observable. That is, a program is pure (free of side effects) if you can simplify any of its subexpressions, in any order, merely by substituting definitions, without changing the outcome of the program. In particular, you can always replace a variable with its definition, without evaluating that first.

This implies that I/O, mutable state and exceptions are side effects, as one would expect. Strictly speaking, it even implies that non-termination is an effect -- and while that may sound strange at first, it is exactly what you want in the case of dependently-typed languages, for example.

What is side effect in java streams API

Terminal operations, such as Stream.forEach or IntStream.sum, may traverse the stream to produce a result or a side-effect

Please refer the below link to know more about side-effect.

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

How to perform side-effect inside of an epic in redux-observable?

Definitely not an anti-pattern outright to have an epic which is "read-only"--but I want to caution that it's often a sign a person is doing something less-than-idiomatic, but this is not one of those cases.

There are likely many ways of accomplishing this in Rx. Here are two:

do + ignoreElements

I think this is the most clear of what it does. do is only ever used to perform a side effect (often logging) for next/error/complete without affecting the stream. We then use ignoreElements to prevent the SOME_ACTION from being dispatched again (which would cause infinite recursion). See the rxjs docs for more info on these operators

const someEpic = action$ =>
action$.ofType('SOME_ACTION')
.do(() => sendAnalytics())
.ignoreElements();

Anonymous Observable

This is a somewhat more "lightweight" solution since it doesn't use any operators other than ofType. I would caution against using this one though until you and your teammates have a solid grasp of RxJS and particularly creating custom/anonymous Observables. i.e. don't write code you don't understand.

const someEpic = action$ =>
new Observable(() =>
action$.ofType('SOME_ACTION')
.subscribe(() => sendAnalytics()) // subscription is returned so it can be cleaned up
);

This use case would be a great addition to the Recipes section of the docs, if someone ever has the time to PR it.


Btw, this is mostly an RxJS question where the stream just happens to be actions. You might find you get answers and better support long-term if you search or phrase your questions in the context of RxJS instead of redux-observable. Nearly all redux-observable questions are actually RxJS questions, which is great cause the knowledge is transferable!

What are expressions with side effects and why should they be not passed to a macro?

The classic example is a macro to calculate the maximum of two value:

#define MAX(a, b) ((a) > (b) ? (a) : (b))

Now lets "call" the macro like this:

int x = 5;
int y = 7;
int z = MAX(x++, y++);

Now if MAX was a normal function, we would expect that x and y would be incremented once, right? However because it's a macro the "call" is replaced like this:

int z = ((x++) > (y++) ? (x++) : (y++));

As you see, the variable y will be incremented twice, once in the condition and once as the end-result of the ternary operator.

This is the result of an expression with side-effects (the post-increment expression) and a macro expansion.


On a related note, there are also other dangers with macros. For example lets take this simple macro:

#define MUL_BY_TWO(x)  (x * 2)

Looks simple right? But now what if we use it like this:

int result = MUL_BY_TWO(a + b);

That will expand like

int result = (a + b * 2);

And as you hopefully knows multiplication have higher precedence than addition, so the expression a + b * 2 is equivalent to a + (b * 2), probably not what was intended by the macro writer. That is why arguments to macros should be put inside their own parentheses:

#define MUL_BY_TWO(x)  ((x) * 2)

Then the expansion will be

int result = ((a + b) * 2);

which is probably correct.

Why evaluation of an expression may generate side effects?

A side effect is any change in the system that is observable to the outside world.

Printing a number is clearly a visible change (also, internally you affect stdout state etc...)


Another important notion that can be helpful is the notion of pure function. It has two main characteristics:

  • A pure function is deterministic. This means, that given the same input, the function will always return the same output. ...

  • A pure function will not cause side effects. A side effect is any change in the system that is observable to the outside world.

Typical examples of functions violating these properties are:

static int n=0;
int foo_1(int m) // not deterministic, without side effect
{
return m+n;
}
int foo_2(int m) // with side effect, but deterministic
{
++n;
return m;
}
int foo_3(int m) // with side effect, not deterministic
{
++n;
return m+n;
}
int foo_4(int m) // without side effect + deterministic = pure function
{
return 2*m;
}

Side effects in C

You can use an assignment expression as a value:

double d = 3.5;

int x, y;

printf("%d", x = d); // Prints "3".

y = (x = d) * 5; // Sets y to 15.

double z = x = d; // Sets z to 3 (not 3.5).

The value produced by x = d, is its main effect. The changing of the value of x is a side effect.



Related Topics



Leave a reply



Submit