I found an example where the function of copying content from one memory to another takes three arguments: the source memory ( param[out] ), the target ( param[in] ) and the number of bytes to copy ( param[in] ).

I do not quite understand the difference between in and out. What are these values ​​for?

  • You do not confuse anything? In theory, the memory into which they are copied must be OUT ... - Vladimir Martyanov
  • Vladimir Martyanov, found here habrahabr.ru/post/252101 . Yes, there target is in. - Arden
  • one
    IMHO there the author lured: "dest Source ...", "Copies content from source", but dest, judging by the name, is the target area, the receiver of the copied data. Below, the answer you deployed :-) - Vladimir Martyanov
  • one
    @ Vladimir Martianov, not IMHO, but absolutely exactly mixed up. - avp
  • @Arden notice, that post was fixed - Pavel Mayorov

1 answer 1

Since you apparently did not specifically indicate exactly where you took this documentation, my answer may be a little different from what will be written there, but ideologically it will be close.

The "classic functions" all arguments "in" - that is, incoming. One requirement is imposed on them - they must be readable and if the function suddenly changes inside them, the external code will not know about it. Usually such arguments can be passed directly (for example, just a number), as well as a variable / expression.

But sometimes inside the function you need to pass an argument that can be changed. Yes, classic functions usually return a result, but sometimes this is not enough. In this case, you just need to "out". A classic example is passing a pointer to a variable. A function inside itself can write a value there and the calling code will "see" it.

Moreover, if the in parameter must contain a meaningful value (except if it is not used internally), then out may well be non-initialized.

Some compilers by default consider parameters as in, and for out it is necessary to specify explicitly (Pascal, Delphi, C #). Moreover, the compiler can even check if the value is being returned. Of course, there are always workarounds and you can fool the compiler :).

Look at the classic look of the memory copy function.

 void * memcpy ( void * destination, const void * source, size_t num ); 

Initially, it’s not at all clear what’s what (for novice programmers). But if you say that destination is out, a source and num in, then the situation becomes a little clearer (although the names themselves hint as it were). In this case, as if there is a guarantee that what is transmitted from the source will not change (there, by the way, const hints again). And the fact that it will be stored at the destination address will change - in the same place out.

Summing up. In most cases (if it is with / with ++, an assembler), in / out is just a convention (and there are no such keywords). In other languages, the compiler can check a little (pascal, C # and they even have such words). It is possible that there are programming languages ​​that contain stricter rules and even completely refuse to compile if the declaration is at variance with the code. The gcc compiler has special keywords (these are extensions), which partially implement this as well. Plus, the compiler can believe you and make optimization.

  • Thanks for the clarification. - Arden
  • You can even add in-out , there are also (an obvious example - sorting) - avp
  • yes, but in-out is the same out that requires initialization. - KoVadim
  • Maybe. And you yourself in this situation (sorting array) that write? (I (if formally required) write in-out) - avp
  • in-out may be different. therefore, most likely, I will write down similar parameters separately. - KoVadim