goto skips declaration of variable
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 19 13:32:58 PDT 2014
On Monday, 18 August 2014 at 13:51:14 UTC, nrgyzer wrote:
> Hi all,
>
> I've the following code snipped:
>
> import std.bigint;
> void main(string[] args)
> {
> BigInt i = "12345";
> if (args.length > 1)
> {
> goto Exit;
> }
> i = BigInt("67890");
> Exit:
> return;
> }
For what it's worth, whenever you have "goto-end" style code,
place all your code in a proper block, in such a way that all
your variable declarations are in that block, and all your gotos
break out of this block. This way, a goto will *never* cross a
declaration, so coding is easy. The only variables you place at
the top or the ones that could need cleanup.
void main(string[] args)
{
//Declarations that need cleanup:
void* p;
//Code
{
BigInt i = "12345"; //Local variable
if (args.length > 1)
{
goto Exit; //Breaks out of block
}
i = BigInt("67890");
BigInt j = "54321"; //Local variable
}
//End
Exit:
CleanUp(p);
return;
}
More information about the Digitalmars-d-learn
mailing list