0

If a function modifies an object it received a pointer to, is that change visible in the caller after the function call?

unit a:

void foo(int *bar) { *bar = 42; }

unit b:

extern void foo(int *bar);

void baz(void) {
   int qux = 0;
   foo(&qux);
   // Is qux guaranteed to be 42 now?
}

I'm quite sure it is but I would like to have it backed up based on the C standard.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • 4
    I'm not sure why we need to dive into the standards for this. This is basic C and the answer is clearly yes. Maybe I'm missing something but what specific doubts do you have about this that we need to pore over the standards to check? – kaylum May 07 '20 at 08:01
  • It is guaranteed that if you access this variable after the function call it will have the updated value. Until then, the compiler may change the order of execution as long as it stays invisible to you. – Alex Lop. May 07 '20 at 08:03
  • 1
    I have no doubts about that. Still, even the most basic, obvious behavior should be defined by the C standard. – undur_gongor May 07 '20 at 08:19
  • @undur_gongor it not related to the standard, it's related to how pointers work in general. – Jabberwocky May 07 '20 at 08:23
  • I can understand him, better to always proof everything. The question is not that he don´t know that it will do so. OP wants to have the complete standard compliance, which is always a good intention. – RobertS supports Monica Cellio May 07 '20 at 08:29
  • @AlexLop. Where do you got this from? We have a conversation about that in the comments under my answer regarding a optimazation method where a compiler is be able to leave the value unchanged even after executing the function. – RobertS supports Monica Cellio May 07 '20 at 09:03
  • @RobertSsupportsMonicaCellio This is related to an “as if” rule https://stackoverflow.com/q/15718262 . If the function content is visible to the compiler, it may inline it and optimize its content including the execution order as long as it remains unnoticed. For example if the passed pointer points to a global variable, then such optimization is impossible because it may be visible by another thread. – Alex Lop. May 07 '20 at 09:11

1 Answers1

2

"I'm quite sure it is but I would like to have it backed up based on the C standard."

Although this is a pretty basic question (as you probably already know), here is the quote to provide the complete standard compliance you wanted:

ISO/IEC 9899:2018 (C18), "Function calls" 6.5.2.2/4 (emphasize mine):


"An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.97)

97) A function can change the values of its parameters, but these changes cannot affect the values of the arguments. On the other hand, it is possible to pass a pointer to an object, and the function can then change the value of the object pointed to. A parameter declared to have array or function type is adjusted to have a pointer type as described in 6.9.1."

Community
  • 1
  • 1
  • @undur_gongor Do you mean the [comment](https://stackoverflow.com/questions/61652552/is-modifying-an-object-pointed-to-by-a-parameter-from-inside-of-a-function-stand/61653088#comment109055206_61652552) by Alex Lop. under your question? – RobertS supports Monica Cellio May 07 '20 at 08:43
  • @undur_gongor I think what he means is different to what you think: `qux` might still have the value of `0` until the end of the execution of `baz`, but `qux` have to be `42` after `baz` returned. – RobertS supports Monica Cellio May 07 '20 at 08:47
  • Sorry for being unclear. A colleague is arguing the compiler could optimize in a way that after the call to baz the previous value of qux is still used (hence the question). Somehow this should be covered by sequence points. – undur_gongor May 07 '20 at 08:47
  • @undur_gongor Hmm, This might be possible, but I´m not sure. But if it is possible, it might violate the rules of the standard. I guess you should ask another separate and focused question to that (with the quotes from your colleague if and how it is possible and if it is standard-compliant. – RobertS supports Monica Cellio May 07 '20 at 09:00