Reverse Lexical Order

monarch_dodra monarchdodra at gmail.com
Tue Jul 16 04:52:07 PDT 2013


On Tuesday, 16 July 2013 at 01:13:15 UTC, H. S. Teoh wrote:
> On Mon, Jul 15, 2013 at 08:59:44PM -0400, Jonathan M Davis 
> wrote:
>> On Monday, July 15, 2013 14:48:08 Manfred Nowak wrote:
>> > Jonathan M Davis wrote:
>> > > gotos in such a context seem like a bit
>> > > of a nightmare to me though.
>> > 
>> > I did realize this nightmare. Therefore the assurance in the 
>> > docs
>> > is probably true only in the absence within the scope of at 
>> > least
>> > gotos to targets within the scope.
>> 
>> Well, I'd have to study exactly how goto works to say how 
>> exactly it
>> would interact with stuff like try-catch. I've pretty much 
>> only used
>> goto with case statements and loops and haven't spent the time 
>> trying
>> to sort out all of its idiosyncracies. I guess that I should 
>> add that
>> to be my todo list.
> [...]
>
> My understanding is that goto translates directly to a jump in 
> assembly,
> so jumping in/out of blocks with declarations or stuff that 
> needs
> cleanups should immediately raise red flags. Of course, the 
> compiler may
> do something intelligent by inserting implicit stack pointer 
> adjustments
> and/or cleanup blocks, but I wouldn't count on it unless the 
> language
> spec explicitly requires so.
>
>
> T

That's wrong. That's "longjmp", which does a raw ASM jump.

C's goto correctly handles stack decrement if the jump would make 
you leave a scope (destroying a variable), and make a variable 
disappear. goto's aren't allowed to jump over declarations 
(unless they "skip" a scope entirelly). Some implementations 
allow it, but I have no idea how they non-POD type construction 
:S but stack pointer is always correctly handled.

With C++'s destructors, goto has also been "improved" to 
guarantee that the destructor is called when a goto would make a 
variable leave a scope. In C++, I don't think it is possible to 
"leak" a stack variable's destructor, short of doing a longjmp.

Long story short, in C, goto always perfectly safe (AFAIK). In 
C++, it depends on what the compiler will allow you to to do, 
depending on its "features". If it respects "strict ANSI", 
though, then you shouldn't be able to go wrong.

--------

Related: I use goto fairly often, but I've yet to find a use-case 
for it outside of loop "improvement", or goto end cleanup. In 
both these cases, scope blocks should not be a problem.


More information about the Digitalmars-d-learn mailing list