Will using goto cause memory leaks?

前端 未结 10 2052
迷失自我
迷失自我 2021-01-17 11:33

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 co

相关标签:
10条回答
  • 2021-01-17 12:22

    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.

    0 讨论(0)
  • 2021-01-17 12:24

    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

    0 讨论(0)
  • 2021-01-17 12:25

    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.

    0 讨论(0)
  • 2021-01-17 12:26

    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.

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