简体   繁体   English

这个静态变量赋值给本地函数中的指针有什么问题?

[英]What is the issue with this static variable assignment to a pointer in local function?

int* func(int *ptr)
{
  static int a = 5;
  ptr = &a;
  return ptr;
}

Someone asked me this question in an interview. 有人在接受采访时问我这个问题。 Now the point is, the variable 'a' is static, so, unlike ordinary variables which are declared, which loses it's value (popped from stack) once the function returns, this variable retains it's value as it is a static one. 现在重点是,变量'a'是静态的,因此,与声明的普通变量不同,一旦函数返回,它会丢失它的值(从堆栈中弹出),这个变量保留它的值,因为它是静态变量。

Then i didn't understand, what's the issue with this piece of code? 然后我不明白,这段代码有什么问题?

There is no point in having ptr as a parameter. ptr作为参数没有意义。 The passed value is not used. 不使用传递的值。 You could change this to 你可以改成这个

int* func()
{
  static int a = 5;
  return &a;
}

The value (address you're pointing to) of ptr that you are passing in is NOT changing outside the function as you are changing a locally scoped copy of ptr 您传入的ptr的值(您指向的地址)不会在函数外部更改,因为您正在更改ptr的本地范围副本

It needs to be passed in by reference or pointer to pointer for it to change. 它需要通过引用或指针传入,以便更改它。

int* func(int **ptr)
{
  static int a = 5;
  *ptr = &a;
  return *ptr;
}

There is no issue. 没有问题。 a is static so it exists through the lifetime of the execution. a是静态的,因此它在执行的生命周期中存在。 It is syntactically invisible outside func . 这是外界语法隐形func You are just returning the address of some "hidden" global variable. 您只是返回一些“隐藏”全局变量的地址。

I'm bringing back your attention for local static variables. 我正在引起你对本地静态变量的关注。 All time the variables will be remain same from last call. 所有时间变量将与上次调用保持相同。 So, if you increase the variable a=a+1 then next time the value is remain 6. Here come how it happens. 所以,如果你增加变量a=a+1那么下次值保持为6.这就是它发生的方式。 Each local declare new space in memory in each call. 每个本地在每次调用时在内存中声明新空间。 But in static variable you are telling to use same memory for every time. 但是在静态变量中,你告诉每次使用相同的内存。 So, pointer will remain same. 因此,指针将保持不变。 That why you are getting same address. 这就是为什么你得到相同的地址。 This is not unexpected. 这并不意外。

There isn't an issue, unless the caller doesn't understand what the function does. 没有问题,除非调用者不理解该函数的作用。 This is quite possible, given that people may assume the function modifies the value pointed to by the parameter. 这很可能,因为人们可能会认为函数修改了参数指向的值。

For instance, if somebody writes code like 例如,如果有人写代码,如

int foo = 0;
func(&foo);
assert(foo == 5);

They will have an issue. 他们会遇到问题。

The interviewer may have been looking for a solution like: 面试官可能一直在寻找以下解决方案:

int *func(int *ptr) {
    static int a = 5;
    *ptr = a; //modify value being pointed to by argument
    return ptr;
}

There is a thread-safety issue: returning a non-constant pointer to a makes it possible that some thread will modify the value while another thread is reading it. 存在一个线程安全问题:返回一个非常量指针指向a使得某个线程可能会在另一个线程正在读取它时修改该值。 Data races have undefined behavior. 数据争用具有未定义的行为。

I'll also toss in that returning a raw pointer is horrible practice, since I haven't seen it in the other answers. 我还会抛弃返回原始指针是一种可怕的做法,因为我没有在其他答案中看到它。 int& func() should be preferred. int& func()应该是首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM