zoukankan      html  css  js  c++  java
  • C++:Stack Unwinding(栈辗转开解)

    节选:http://stackoverflow.com/questions/2331316/what-is-stack-unwinding

    1. Stack unwinding is usually talked about in connection with exception handling. Here's an example:

    void func( int x )
    
    {
    
        char* pleak = new char[1024]; // might be lost => memory leak
    
        std::string s( "hello world" ); // will be properly destructed
    
        if ( x ) throw std::runtime_error( "boom" );// will only get here if x != 0
    
        
    
        delete [] pleak; 
    
    }
    
    int main()
    
    {
    
        try
    
        {
    
            func( 10 );
    
        }
    
        catch ( const std::exception& e )
    
        {
    
            return 1;
    
        }
    
        return 0;
    
    }

    Here memory allocated for pleak will be lost if exception is thrown, while memory allocated to s will be properly released by std::string destructor in any case. The objects allocated on the stack are "unwound" when the scope is exited (here the scope is of the function func.) This is done by the compiler inserting calls to destructors of automatic (stack) variables.

    Now this is a very powerful concept leading to the technique called RAII, that is Resource Acquisition Is Initialization, that helps us manage resources like memory, database connections, open file descriptors, etc. in C++.

    2.All this relates to C++:

    Definition: As you create objects statically (on the stack as opposed to allocating them in the heap memory) and perform function calls, they are "stacked up".

    When a scope (anything delimited by { and }) is exited (by using return XXX;, reaching the end of the scope or throwing an exception) everything within that scope is destroyed (destructors are called for everything). This process of destroying local objects and calling destructors is called stack unwinding. (Exiting a code block using goto will not unwind the stack which is one of the reasons you should never use goto in C++).

    You have the following issues related to stack unwinding:

    1. avoiding memory leaks (anything dynamically allocated that is not managed by a local object and cleaned up in the destructor will be leaked) - see RAII referred to by Nikolai, and the documentation for boost::scoped_ptr or this example of using boost::mutex::scoped_lock.

    2. program consistency: the C++ specifications state that you should never throw an exception before any existing exception has been handled. This means that the stack unwinding process should never throw an exception (either use only code guaranteed not to throw in destructors, or surround everything in destructors with try { and } catch(...) {}).

    If any destructor throws an exception during stack unwinding you end up in the land of undefined behavior which could cause your program to treminate unexpectedly (most common behavior) or the universe to end (theoretically possible but has not been observed in practice yet).

  • 相关阅读:
    BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
    BZOJ_1492_[NOI2007]货币兑换Cash_CDQ分治+斜率优化
    BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS
    BZOJ_2726_[SDOI2012]任务安排_斜率优化+二分
    BZOJ_1406_[AHOI2007]密码箱_枚举+数学
    哈希表(Hash table)
    算法分析方法之平摊分析(Amotized Analysis)
    数据库视图功能的使用
    不基于比较的排序算法:Counting-sort和Radix-sort
    QuickSort(快速排序)的JAVA实现
  • 原文地址:https://www.cnblogs.com/qinfengxiaoyue/p/3084790.html
Copyright © 2011-2022 走看看