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

后端 未结 6 2145
余生分开走
余生分开走 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 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.

提交回复
热议问题