C: Passing an automatic variable by reference

I came across a piece of code that now works fine, but, in my opinion, its behavior is undefined and may lead to an error in the future.

Pseudocode:

void OpertateLoad(int load_id)
{
   int value = 0;

   /* code to calculate value */

   SetLoadRequest(load_id,&value);

   /*some processing  not involving value**/

}

void SetLoadRequest(int load_id, int* value)
{
   /**some processing**/
   LoadsArray[load_id] = *value; 
   /**some processing**/ 
}

In my understanding, the C compiler does not guarantee where the variables will be stored Auto. It can be a stack / register (if available and suitable for processing).

I suspect that if the compiler decides to save valueto the general register, then the function SetLoadRequestmay refer to incorrect data.

Did I understand correctly, or am I overdoing it?

I use a compiler IARARMfor the processor ARM CORTEX M-4.

----------: EDIT: ----------

. , " , , , ".

: ? , ? '.

NO, - C, ? , undefined?

+4
4

C11 n1570:

6.2.4

6 , , , , , - . ( , .) ,    . .    , ,    ;    .

+1

. , value , , SetLoadRequest.

, . ( , ), . !

, , ( undefined, - ).

+7

?

!

. , , ( ).

+3

As I understand it, the area valueis the body OpertateLoad. However, it SetLoadRequestassigns the specified value, so the actual value is copied value. Undefined behavior applies.

+1
source

Source: https://habr.com/ru/post/1686190/


All Articles