Passing pointer to local variable to function: is it safe?

后端 未结 6 2119
余生分开走
余生分开走 2020-12-15 20:15

For example:

void func1(){
    int i = 123;
    func2(&i);
}
void func2(int *a){
    *a = 456;
}

When func1 calling

相关标签:
6条回答
  • 2020-12-15 20:53

    As stated in most of the answers before, it is perfectly safe to pass the pointer to func2() in your special case.

    In a real world piece of software however, I consider this harmful as you don't have control on what func2() is doing with your variable. func2() may create an alias to its parameter to use it asynchronously at a later point in time. And at that time, the local variable int i may be gone when this alias is used later.

    So from my point of view passing a pointer to a local (automatic) variable is extremely dangerous and should be avoided.

    You may do so if you declare the variable in func1() as static int i;

    In that case, it is ensured, that the memory for i won't get recycled and overwritten. However you will need to setup some Mutex locking for access control to this memory in a concurrent environment.

    To illustrate this problem here is some code I just stumbled in yesterday while doing software testing at my customer. And yes, it crashes...

    void func1()
    {
      // Data structure for NVMemory calls
      valueObj_t NVMemObj;
    
      // a data buffer for eeprom write
      UINT8 DataBuff[25];
      // [..]
      /* Assign the data pointer to NV Memory object */
      NVMemObj.record = &DataBuff[0];
      // [..]
      // Write parameter to EEPROM.
      (void)SetObject_ASync(para1, para2, para3, &NVMemObj);
      return;
    }
    
    void SetObject_ASync(para1, para2, para3, valueObj_t *MemoryRef)
    {
      //[..]
      ASyncQueue.CommandArray[ASyncQueue.NextFreeEntry].BufferPtr  = MemoryRef->record;
      //[..]
      return;
    }
    

    In this case, the data in the DataBuff is long gone when the pointer in ASyncQueue.CommandArray[ASyncQueue.NextFreeEntry].BufferPtr is used to store the data to the EEPROM.

    To fix this code, it is at least necessary to declare static UINT8 DataBuff[25]; Additionally, it shall be considered to also declare static valueObj_t NVMemObjas we don't know what the called function is doing with that pointer.

    To put it briefly: TL;DR

    Even though it is legal in the C-language I consider it as harmful to pass pointers to automatic variables in a function call. You never know (and often you don't want to know) what exactly the called function does with the passed values. When the called function establishes an alias, you get in big trouble.

    Just my 2 cents.

    0 讨论(0)
  • 2020-12-15 20:55

    Yes it is safe to pass a pointer to a local variable but you can't return a pointer to an automatic local variable from a function.

    0 讨论(0)
  • 2020-12-15 20:59

    The scope of i is func1 and it outlives the call to func2. So it is perfectly safe.

    0 讨论(0)
  • 2020-12-15 21:03

    Is this safe for the rules of C?

    What you are doing is safe as the local variable is still valid and within the scope. Accessing the local varaible outside of its scope is undefined behvaior but this is totally fine

    0 讨论(0)
  • 2020-12-15 21:05

    In your case, you can safely use &i till the time i is valid.

    Now, as we can see i has a lifetime till the end of func1(). As, the func2() is being called from the func1() and the func1() has not yet finished execution, so, i is still valid.

    That's why , usually passing the address of a local variable to another function is usually allowed (the variable's lifetime is not over) but, returning the address of a local variable (immediately after return, local variables of the function cease to exist) is not allowed.

    TL;DR :You can safely use &i as the argument of func2() as shown here.

    0 讨论(0)
  • 2020-12-15 21:10

    Yes, your code is safe.

    As long as the object's lifetime is not over, it's safe to pass local variables like you do.

    0 讨论(0)
提交回复
热议问题