2

Is this allowed?

goto inside;
{
inside:
int a[n];
}

A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

What about the inverse?

{
goto outside;
int a[n];
}
outside: ;

and

{
int a[n];
goto outside;
}
outside: ;

Are they the same thing?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sabrina
  • 1,460
  • 11
  • 23
  • your quote seems to answer your question – M.M Feb 21 '17 at 04:14
  • So you mean first code OK , second code OK third code NO and fourth code: OK; – Sabrina Feb 21 '17 at 04:16
  • 1
    1. OK, 2. OK, 3. OK, 4. OK . part 4 is a completely separate question to the rest, you should have posted as a different question – M.M Feb 21 '17 at 04:18
  • for the last part I am not sure if it is an important question that's why I included it inside this one it is like a check. – Sabrina Feb 21 '17 at 04:19

2 Answers2

5

The first one is not allowed, but the reason is that you can't apply a label to a declaration.

The label comes before the scope of the VLA, and if there were executable code (formally, a statement; even an empty one would do) to label, then you'd be OK:

goto inside;
{
    inside:;
    int a[n];   // Scope of a starts here!
}

Both the goto outside fragments are fine.


There was a discussion about the scope of identifiers in comments, and when the array a[n] comes into scope. The standards (both C99 and C11 — in the same section number) say:

6.2.1 Scopes of identifiers

¶7 Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

It was in §6.1.2.1 in the C90 standard, but the wording was the same.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This is what I thought too the scope of VLA starts from the declaration and not from starting of block. – Sabrina Feb 21 '17 at 04:24
3

Using goto to exit a scope that contains a VLA is perfectly fine. It's no different than exiting that scope using return or break.

The issue with using goto to enter a scope with a VLA is that a VLA declaration is treated as if it contains some code which calls alloca() to reserve stack space for the array. If you use goto to skip the declaration, you've also skipped that alloca() call, so there may not be any space reserved for the array.

Note that, alloca() aside, this isn't much different from skipping over a variable initialization (e.g, int n = 1). VLAs just behave as though they always have an initializer.