How is destroying local variables when a block is exited normally called in C++?
Asked Answered
T

6

9

C++ automagically calls destructors of all local variables in the block in reverse order regardless of whether the block is exited normally (control falls through) or an exception is thrown.

Looks like the term stack unwinding only applies to the latter. How is the former process (the normal exit of the block) called concerning destroying local variables?

Tripper answered 9/4, 2010 at 5:52 Comment(2)
It really isn't called anything. Everywhere in the standard, it says things like "...automatic variables are destroyed at the end of the block". It never gives that process a name.Anyhow
@GMan - Save the Unicorns: I guess you could add this as an answer. Mentioning what The Standard says is a strong argument.Tripper
O
5

An object is automatically destructed when it "goes out of scope". This could be referred to as "automatic storage reclamation", but that actually refers to garbage collection (there are several papers with that phrase in their name that use the term to mean garbage collection). When it is used to ensure proper pairing of open/close, lock/unlock, or other forms of resource acquisition with their appropriate release, then it is known as the design pattern of Resource Acquisition is Initialization (RAII), which is somewhat ironic given that the main aspect of RAII is not the resource initialization or acquisition, but rather its destruction.

Organography answered 9/4, 2010 at 5:58 Comment(0)
S
3

Stack unwinding happens in both these cases, it's just that under normal execution the stack is unwound only to the context of the calling method (or block) when the executing method returns (or the block is exited). Local variables are allocated on the stack, so they are cleaned up in reverse order of allocation, and it's this process that is called unwinding. It's no different than processing any other type of data that you'd store in a LIFO structure - e.g. undo, redo.

When an exception is thrown the handler will unwind the stack through through zero or more methods until it finds one that can catch the exception, or until it reaches the top of the stack, at which point the unhandled exception handler will be called.

It seems to be convention to only use the term stack unwinding in the case of exception handling, but it's the same process occurring in each of these cases. The specific case where the stack unwinds due to a method exiting is called returning, there doesn't seem to be any convention for naming what happens when a scoped block of code is exited.

Sunwise answered 9/4, 2010 at 6:37 Comment(0)
O
2

The local variable is destroyed when it goes out of scope. Perhaps the process is called like "going out of scope"?

Odelsting answered 9/4, 2010 at 5:58 Comment(0)
T
1

I'm not sure there is a name for this. Stack variables are so automatic that no one worries about them, ever, not even enough to give a name for this automatic cleanup process.

Call it "going out of scope", I guess.

Treen answered 9/4, 2010 at 5:58 Comment(0)
H
1

I've always heard it spoken as "going out of scope" or more precisely "an auto variable going out of scope".

Hibiscus answered 9/4, 2010 at 5:59 Comment(0)
S
1

If what you're asking is how the method call is actually implemented in machine code, I would say it would depend on the calling convention used

Suds answered 9/4, 2010 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.