使用goto会导致内存泄漏吗?
我有一个程序,需要打破一大堆嵌套的 for 循环。 到目前为止,大多数人告诉我的方法是在代码中使用丑陋的 goto。
现在,如果我在循环中创建一堆本地堆栈(我认为这就是它们的名称,如果不是,我的意思只是常规变量而不使用新命令)变量,并且我的程序命中触发 goto 的 if 语句,由于我的程序不正确地退出许多循环并且没有清理局部变量,我会遇到内存泄漏吗?
I have a program in which i need to break out of a large bunch of nested for loops. So far, the way most people have been telling me to do it is to use an ugly goto in my code.
Now, if i create a bunch of local stack (i think that's what they are called, if not, i mean just regular variables without using the new command) variables inside my loops and my program hits that one if statement that triggers the goto, will i encounter a memory leak due to my program exiting many loops improperly and not cleaning up the local variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不,您不会导致内存泄漏。使用
goto
并不是“不正确地退出循环”。 从代码结构的角度来看,通常不推荐。除此之外,当您离开循环时,局部变量将超出范围并在此过程中从堆栈中弹出(即清理)。
No, you will not cause a memory leak. Using a
goto
is not "exiting loops improperly." It's just not generally recommended from a code-structure point-of-view.That aside, when you leave the loop, the local variables will go out of scope and be popped off of the stack (i.e. cleaned up) in the process.
堆栈变量(汽车,而不是汽车人)不像通过 new() 或 malloc() 分配的变量那样“泄漏”。
至于 goto 的“丑陋”,这只是教条主义。 读高德纳 (Knuth) 的书,他和迪杰斯特拉 (Dijkstra) 一样才华横溢。 http://pplab.snu.ac.kr/courses/ adv_pl05/papers/p261-knuth.pdf 避免基于面食的编程,但小心使用不会退化为意大利面。
Dijkstra 不喜欢它们,因为用 goto 做的大部分事情都可以用其他结构化编程技术来完成,并且使用更少的代码,因此使其他结构化编程技术不易出错。
明白 goto 不应该是你的第一个解决方案,并且不要不遗余力地使用它们,但如果它有意义,不要屈服于教条主义的暴民。 Break 语句只是一个变相的 goto,专为严格遵守“你不得使用 goto”戒律而没有意义的情况而设计。
Stack variables (autos, not autobots) aren't "leaky" like variables allocated via new() or malloc().
As far as the "uglyness" of gotos that's just dogmatic. Read Knuth, he was just as brilliant as Dijkstra. http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf Avoid pasta based programming, but careful use won't degrade into spaghetti.
Dijkstra didn't like them BECAUSE most of what you can do with gotos can be done with other structured programming techniques and uses less code therefore making the other structured less error prone.
Understand that gotos shouldn't be your first solution, and don't go out of your way to use them, but if it makes sense don't submit to dogmatic lench mobs. The break statement is a just a goto in disguise designed for cases where strict adhearance to the "Thou shalt not use gotos" commandment didn't make sense.
堆栈变量在您进入函数时定义(并分配),并在您离开函数时隐式消除(因为整个调用堆栈记录都被弹出)。 函数内部的任何弹跳都不可能对一直分配的内存造成任何破坏。 无论代码采用什么执行路径,当控制权返回到调用函数时,堆栈记录都会弹出,并且内存将被释放。
Stack variables are defined (and allocated) the moment you enter the function, and are implicitly eliminated the moment you leave the function (since the entire call stack record is popped away). No amount of bouncing around inside the function can possibly cause any havoc with memory that's been allocated the whole time. Regardless of what execution path you take through the code, the stack record will pop when control returns to the calling function, and the memory will be freed.
不可以。您只能泄漏动态分配的内存。
No. You can only leak memory that is dynamically allocated.
其他答案是正确的......但是,如果您必须以不同的方式嵌套循环,我会质疑将它们放在那里的设计。 将逻辑拆分为单独的函数将是解决此类问题的更好方法。
比利3
The other answers are true.... however, if you have to nest loops that differently, I'd question the design that put them there. Splitting up that logic into separate functions would be a better way to solve such a problem.
Billy3
Goto 并不总是不好,但就您而言,您可能不应该使用 goto。
请参阅此处和良好使用 goto 的示例="https://stackoverflow.com/questions/788903/valid-use-of-goto-for-error-management-in-c">此处。
如果您转到超出范围的标签,则堆栈上的对象将被释放。
示例:
这将打印:
Goto is not always bad, but in your case you probably shouldn't be using goto.
See examples of good use of goto here and here.
If you goto a label that is outside of scope your object on the stack will be freed.
Example:
This will print:
没有。 局部变量不需要单独清理。 当堆栈弹出时,所有局部变量将随之消失。
Nope. Local variables do not need to be individually cleaned up. When the stack pops, all the local variables will go away right along with it.
不,如果您使用 goto 语句中断循环,循环中的任何自动变量都不会导致编程泄漏。
No, any automatic variables in your loops will not cause programming leaks if you break out of your loops with a goto statement.
不,你不会的。
但是,请确保正确释放所有外部资源。 例如,如果您打开一个文件,则可以跳到通常关闭该文件的位置。
No you will not.
However, make sure that any external resources are properly released. For example, if you opened a file it would be possible to jump past where it would normally be closed.
向后goto会泄漏资源吗? 或者下面的代码有任何其他潜在的问题吗?
重新执行:
Will backward goto leak resources ? Or any other potential problems with below code ?
Reexecute: