What is side effect in C?

落花浮王杯 提交于 2021-01-29 14:11:59

问题


Wikipedia says that:

In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation.

But how can we access a variable outside its local environment, can anyone explain this situation, side effect, main effect and sequence point comprehensibly?


回答1:


A function is (should be) a black box, in which the return value, or the value of a variable passed by reference, should be the only thing that may change depending upon the input parameters.

Any other observable change that the function produces outside these cases, is a side-effect. The most well-known example may be the printf() function which, besides returning the number of written characters, changes the contents of the standard output, which means altering some memory buffer associated with a pipe, a file, or the screen, for instance, and which doesn't belong to the local environment of the function.




回答2:


There is nothing called "main effect" in the C language.

The formal definition of side effect in the C language (C17 5.1.2.3/2) is rather:

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.

Plainly, a side effect is:

  • accessing a volatile variable, or
  • writing to any variable,
  • or writing to a file

(Where stdout is likely to regard as a file for the given system.)

This is relevant when determining if/how a compiler is allowed to optimize code (C17 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).

But also when it comes to determine if an expression has well-defined behavior or not, which is where sequence points come in. Sequence points is however a big topic of its own, explained for example in several answers here: Why are these constructs using pre and post-increment undefined behavior?




回答3:


A State is the attribute of a program which tells about the behavior of the program/function/variable.

for example, observe the state of "i" (variable):

int i = 0; // here state of i is only dependent on the value its holding. lets denote the time when i is having a value 0 as State A.
i++; // i = 1, that means its no longer 0 i.e. state changed to state B
i++ // i = 2 . State C
i--; // i = 1, state B
i += 0; // state B

Side effects: In general, when someone talks about side-effects in a function (regardless of the language), they are talking about changes to the state of the program outside of changes to the function parameters and the object itself.

The way I like to visualize side effect:

----------------------
            \ <= side effect
           ----------------

A function with no side effects in C (where we disregard the possibility of a function being a member function) might look like this:

int stateOfP(int a, char *p)
{
  *p = 0;
   return a+1;
}

In this case, the program has modified a location in memory pointed to by p, but as p is a memory location pointed to in an argument, we don’t count that as a side effect.

A function without side effects is a good thing, for a few reasons.

First, the absence of side effects makes it easier for the compiler to optimize use of the function.

Second, side effects make it much more difficult to prove the correctness of a program.

Finally, when using multiple threads, particularly in a C program, side effects can have undetermined outcomes. For example, if two threads modify an ordinary global variable in a C program without some special locking mechanism, the program’s outcome is undefined.

What does it look like when a function has side effects? Something like this:

int a = 0;
void stateChange(int p)
{
  a++; // here the function is having side effects as 'a' is not its attribute
  return;
}


来源:https://stackoverflow.com/questions/62148339/what-is-side-effect-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!