Will using goto cause memory leaks?

前端 未结 10 2062
迷失自我
迷失自我 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:11

    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:

    #include 
    using namespace std;
    
    class A
    {
    public:
      ~A() 
      {
         cout<<"A destructor"<

    This will print:

    Inside scope
    A destructor
    After l label

提交回复
热议问题