What is stack unwinding? Searched through but couldn’t find enlightening answer!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Stack unwinding is usually talked about in connection with exception handling. Here’s an example:
Here memory allocated for
pleakwill be lost if an exception is thrown, while memory allocated toswill be properly released bystd::stringdestructor in any case. The objects allocated on the stack are “unwound” when the scope is exited (here the scope is of the functionfunc.) 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++.
Now that allows us to provide exception safety guarantees.