简体   繁体   English

在 C++ 中返回局部变量

[英]Returning a local variable in C++

I'm encountering a C++ problem (or lack there-of) because this code works, but I don't know why.我遇到了 C++ 问题(或缺乏),因为此代码有效,但我不知道为什么。

For context, my professor has given us a header file, and a main function, the program generates a Fibonnaci Sequence.对于上下文,我的教授给了我们一个头文件和一个主函数,程序生成一个斐波那契数列。

I am using an iterator to iterate the sequence using ++a and a++.我正在使用迭代器使用 ++a 和 a++ 迭代序列。

Here is my implementation of a++.这是我对a++的实现。

FibonacciIterator FibonacciIterator::operator++(int) //i++, increment i, return old i.
{
    FibonacciIterator old = *this; //create old object to return

    //increment actual fibonacci sequence
    long nextTemp = fCurrent + fPrevious;
    fPrevious = fCurrent;
    fCurrent = nextTemp;

    fCurrentN++;  //increment count
    return old;
}

Now, I create a value 'old', by using the dereference operator of the pointer 'this'.现在,我通过使用指针“this”的解引用运算符创建了一个值“old”。 I do some logic to the current iterator, and return the old iterator.我对当前的迭代器做一些逻辑,并返回旧的迭代器。

This all works, and using the following do-while loop:这一切都有效,并使用以下 do-while 循环:

FibonacciIterator lIterator2 = lIterator.begin();
do
{
    cout << *lIterator2++ << endl;
} while (lIterator2 != lIterator2.end());

everything works.一切正常。 This do-while loop is written by the professor, we are not meant to change it.这个 do-while 循环是教授写的,我们不打算改变它。

My question is, why does this code work?我的问题是,为什么这段代码有效? To my understanding, when we create a local variable in a method, that variable is encased within the methods stack frame.据我了解,当我们在方法中创建局部变量时,该变量被封装在方法堆栈框架中。 When we exit the stack frame, should we return a local variable that was created in that stack frame, we might get our value.当我们退出堆栈帧时,如果我们返回在该堆栈帧中创建的局部变量,我们可能会得到我们的值。 We also might not.我们也可能不会。 My understanding is this is because the memory location in which this variable was created is now "up for grabs" by any program on the computer that might need it.我的理解是这是因为创建这个变量的内存位置现在可以被计算机上可能需要它的任何程序“抢夺”。 So IF we get the value we desired, its because nothing has overwritten it yet.所以如果我们得到了我们想要的值,那是因为还没有任何东西覆盖它。 IF we don't, its because something has overwritten it.如果我们不这样做,那是因为有些东西已经覆盖了它。

So why is it that this code works, 100% of the time?那么为什么这段代码在 100% 的情况下都能正常工作呢? Why do I not SOMETIMES see old become garbage, and crash my program with an unhandled exception?为什么有时我不会看到旧的变成垃圾,并因未处理的异常而使我的程序崩溃? My only guess is that because 'FibonacciIterator' is a user made class, it is AUTOMATICALLY allocated memory on the heap, thus we don't run in to this problem.我唯一的猜测是因为 'FibonacciIterator' 是一个用户创建的类,它在堆上自动分配内存,因此我们不会遇到这个问题。 My only guess would be that this problem only exists using types such as int, long, double, etc.我唯一的猜测是这个问题只存在于使用 int、long、double 等类型时。

However, I am 99% sure that my guess is wrong, and I want to understand what is going on here.但是,我 99% 确定我的猜测是错误的,我想了解这里发生了什么。 I want to understand this because for one, I enjoy C++, but I don't enjoy not knowing why something works.我想理解这一点,因为一方面,我喜欢 C++,但我不喜欢不知道某些东西为什么有效。

Thank you!谢谢!

You can return a local object - it will be copied.您可以返回一个本地对象 - 它将被复制。 You shouldn't return a pointer to a local object.您不应该返回指向本地对象的指针。 As you correctly point out, the pointer will point to junk.正如您正确指出的那样,指针将指向垃圾。

In your case you've got a copy and so it is ok (with all the caveats that the copy must be "safe")在你的情况下,你有一个副本,所以没问题(所有的警告都是副本必须是“安全的”)

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

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