Varibly modified type in switch statement

Z

Zhang Yuan

When I read c standard ISO/IEC 9899:201x
I'm confused about the sentence below ,why we make the switch statement
within the scope of that variably modified type identifier.
Thanks in advance if any one can offer some code example or explanation.

------
If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.154)

154) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.

------
 
B

Bart van Ingen Schenau

When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
below ,why we make the switch statement within the scope of that
variably modified type identifier. Thanks in advance if any one can
offer some code example or explanation.

------
If a switch statement has an associated case or default label within
the scope of an identifier with a variably modified type, the entire
switch statement shall be within the scope of that identifier.154)

154) That is, the declaration either precedes the switch statement,
or it follows the last case or default label associated with the
switch that is in the block containing the declaration.

------

The paragraph makes constructs like this illegal:

void foo()
{
int n = bar();
switch(baz())
{
case 1:
char s[n]; // Variably modified type
// do something
break;
default:
// do something else
}
}

The reason for making this invalid is because 's' is also in scope in the
default branch of the switch statement, but the control flow did not pass
through the declaration, so the program did not have the chance to
calculate how much room to reserve for `s`.
To make this legal, you would have to change it to

void foo()
{
int n = bar();
char s[n]; // Variably modified type
switch(baz())
{
case 1:
// do something
break;
default:
// do something else
}
}

Bart v Ingen Schenau
 
Z

Zhang Yuan

The reason for making this invalid is because 's' is also in scope in the

default branch of the switch statement, but the control flow did not pass

through the declaration, so the program did not have the chance to

calculate how much room to reserve for `s`.

To make this legal, you would have to change it to


Thanks,really appreciate your explicit explanation.
 
P

Philip Lantz

Bart said:
Zhang said:
When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
below ,why we make the switch statement within the scope of that
variably modified type identifier. Thanks in advance if any one can
offer some code example or explanation.

------
If a switch statement has an associated case or default label within
the scope of an identifier with a variably modified type, the entire
switch statement shall be within the scope of that identifier.154)

154) That is, the declaration either precedes the switch statement,
or it follows the last case or default label associated with the
switch that is in the block containing the declaration.

------

The paragraph makes constructs like this illegal:

void foo()
{
int n = bar();
switch(baz())
{
case 1:
char s[n]; // Variably modified type
// do something
break;
default:
// do something else
}
}

The reason for making this invalid is because 's' is also in scope in the
default branch of the switch statement, but the control flow did not pass
through the declaration, so the program did not have the chance to
calculate how much room to reserve for `s`.
To make this legal, you would have to change it to

void foo()
{
int n = bar();
char s[n]; // Variably modified type
switch(baz())
{
case 1:
// do something
break;
default:
// do something else
}
}

Or (my preference):

void foo()
{
int n = bar();
switch(baz())
{
case 1: {
char s[n]; // Variably modified type
// do something
break;
}
default:
// do something else
}
}
 
T

Tim Rentsch

Bart van Ingen Schenau said:
When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
below ,why we make the switch statement within the scope of that
variably modified type identifier. Thanks in advance if any one can
offer some code example or explanation.

------
If a switch statement has an associated case or default label within
the scope of an identifier with a variably modified type, the entire
switch statement shall be within the scope of that identifier.154)

154) That is, the declaration either precedes the switch statement,
or it follows the last case or default label associated with the
switch that is in the block containing the declaration.

------

The paragraph makes constructs like this illegal:

void foo()
{
int n = bar();
switch(baz())
{
case 1:
char s[n]; // Variably modified type
// do something
break;
default:
// do something else
}
}

[snip elaboration]

Actually this example requires a diagnostic regardless of 6.8.4.2 p2,
because it has a syntax error. (Only statements, not declarations,
are allowed after labels, including 'case' labels.)

However, this got me thinking about the ramifications of how variably
modified types are defined. Consider the following:

void
foo(){
int n = bar();
typedef int VLA[n];
switch( bas() ){
case 1: ;
VLA *p = 0;
/* ... etc 1 ... */ ;
break;

default: /* ... etc 2 ... */ ;
}
}

Question: does this function definition have a constraint violation
or not? Clearly the type VLA is a variable length array type, and so
a variably modified type. The type of p is derived from a variably
modified type, and so is also a variably modified type. The switch()
statement has a 'default' label in the scope of 'p', but that scope
does not cover the entire switch() statement. So 6.8.4.2 p2 is
violated, or in other words a constraint violation and a mandatory
diagnostic, yes? Does the Standard mean to disallow this function
definition? Or have I misunderstood something somewhere?
 
S

Shao Miller

Bart van Ingen Schenau said:
When I read c standard ISO/IEC 9899:201x I'm confused about the sentence
below ,why we make the switch statement within the scope of that
variably modified type identifier. Thanks in advance if any one can
offer some code example or explanation.

------
If a switch statement has an associated case or default label within
the scope of an identifier with a variably modified type, the entire
switch statement shall be within the scope of that identifier.154)

154) That is, the declaration either precedes the switch statement,
or it follows the last case or default label associated with the
switch that is in the block containing the declaration.

------

The paragraph makes constructs like this illegal:

void foo()
{
int n = bar();
switch(baz())
{
case 1:
char s[n]; // Variably modified type
// do something
break;
default:
// do something else
}
}

[snip elaboration]

Actually this example requires a diagnostic regardless of 6.8.4.2 p2,
because it has a syntax error. (Only statements, not declarations,
are allowed after labels, including 'case' labels.)

However, this got me thinking about the ramifications of how variably
modified types are defined. Consider the following:

void
foo(){
int n = bar();
typedef int VLA[n];
switch( bas() ){
case 1: ;
VLA *p = 0;
/* ... etc 1 ... */ ;
break;

default: /* ... etc 2 ... */ ;
}
}

Question: does this function definition have a constraint violation
or not? Clearly the type VLA is a variable length array type, and so
a variably modified type. The type of p is derived from a variably
modified type, and so is also a variably modified type. The switch()
statement has a 'default' label in the scope of 'p', but that scope
does not cover the entire switch() statement. So 6.8.4.2 p2 is
violated, or in other words a constraint violation and a mandatory
diagnostic, yes? Does the Standard mean to disallow this function
definition? Or have I misunderstood something somewhere?

It looks like a constraint violation to me, too.

Just as an observation: If it wasn't, and if 'default' was reached and
had the expression 'sizeof *p' somewhere, the value of 'n' at the time
of the 'typedef' would've effectively been smuggled down without the
declaration of 'p' having been reached.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top