Questions tagged [side-effects]

A side-effect is a programming term that refers to intended changes in the program behavior, such as a variable changing value. It is in important term when discussing compiler optimization and expression evaluation.

A side-effect is a programming term that refers to intended changes in the program behavior, such as a variable changing value. It is in important term when discussing compiler optimization and expression evaluation.

Most notably, this term is often used in the C and C++ languages. One formal definition of the term can be found in ISO 9899:2011 (C11) 5.1.2.3 §2:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression in general includes both value computations and initiation of side effects.

C++ contains an identical definition, see for example C++11 1.9/12.

When a compiler optimizes code, it has to ensure that it removes no side effects (C11 5.1.2.3 §4):

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

Therefore the volatile qualifier is often used to prevent the compiler from optimizing cerain parts of the code, since any access (read/write) to a volatile object is considered a side effect.

Side effects is also an important term when discussing expression evaluation. For example, expressions that contain multiple side effects on the same variable, with no so-called "sequence point" in between, invokes undefined behavior, see Undefined behavior and sequence points.

461 questions
-1
votes
1 answer

bash: assignment AND condition test in nested statement?

What I'm looking for is a compact line of code that uses the value of an expression twice in one nested statement for an assignment to a variable and a condition test of the variable's new value against another value The questions are: Is this…
-1
votes
1 answer

Side effect in different programming language

I am studying the Side Effect for different programming language. For instance, in C++, if I define x = 1; int f(int y) { x = x + y; return x; } and execute f(1) the new value of x is equal to 2. In python, I can surely execute define the…
J.G
  • 167
  • 1
  • 1
  • 7
-1
votes
2 answers

Does this post-increment statement result in undefined behaviour?

When building a program using a newer version of GCC, I found a problem in the code. count[i] = count[i]++; This code worked with an older version of GCC (2.95), but doesn't work with a newer version (4.8). So I suspect this statement causes…
rcr
  • 13
  • 2
-1
votes
1 answer

Is a prefix or postfix operator in the global scope a side effect?

From Wikipedia: In computer science, a function or expression is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world. From You Don't Know JS There…
Gwater17
  • 2,018
  • 2
  • 19
  • 38
-1
votes
2 answers

How do you carry mutating data into callbacks within loops?

I constantly run into problems with this pattern with callbacks inside loops: while(input.notEnd()) { input.next(); checkInput(input, (success) => { if (success) { console.log(`Input ${input.combo} works!`); } …
TheEnvironmentalist
  • 2,694
  • 2
  • 19
  • 46
-1
votes
1 answer

Order of precedence in if statement C++

I'm trying to design a construct that can check for errors in input, so I wrote two boolean functions in a test program whose parameters are string references. I was hoping my if statement would evaluate the first function- changing part of word,…
Ben
  • 5,952
  • 4
  • 33
  • 44
-2
votes
2 answers

Unexpected side effect with java streams reduce operation

I'm observing a side effect on an underlying collection when calling 'reduce' on a Stream. This is so basic. I can't believe what I am seeing, but I can't find the error. Below is the code and the resulting output. Can anyone explain to me why one…
-2
votes
3 answers

Weird side effect using operator '->' in c

I get this weird side effect while using operator '->' in code I wrote in C. The pointer which I used -> on , is changed to have some garbage. More specifically: I have the following structs: typedef void* ListElement ; typedef struct List_t* List…
RanZilber
  • 1,840
  • 4
  • 31
  • 42
-2
votes
3 answers

external side effect in constructor

Look at this code: #include int main() { XFile file("./my_file.xxxx", "create"); XObject object("my_object"); // modify the object object.Write(); } Try to guess where object will be saved... yes, you guessed it. I…
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
-3
votes
3 answers

Boolean expression with integer increments gives unexpected results

#include int main() { int x, y, z; x=y=z=1; z = ++x || ++y && ++z; printf("%d, %d, %d", x, y,z); return 0; } I am getting output 2, 1, 1. I need and explanation why Y is 1 in output?
-3
votes
1 answer

How to create the effect on web page for selecting part of text by jquery

How can I create effect(by jquery) in web page when user select a part of text then focus paragraph and dark other part of page? How can create this effect? For example can see this link this image show the web page before select the text(top of…
1 2 3
30
31