Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Varibly modified type in switch statement (http://www.velocityreviews.com/forums/t955792-varibly-modified-type-in-switch-statement.html)

Zhang Yuan 12-24-2012 11:27 AM

Varibly modified type in switch statement
 
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.

------

Bart van Ingen Schenau 12-24-2012 11:58 AM

Re: Varibly modified type in switch statement
 
On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:

> 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

Zhang Yuan 12-24-2012 12:24 PM

Re: Varibly modified type in switch statement
 

> 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.

Philip Lantz 12-25-2012 03:05 AM

Re: Varibly modified type in switch statement
 
Bart van Ingen Schenau wrote:
> Zhang Yuan wrote:
> > 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
}
}


Tim Rentsch 12-27-2012 03:35 PM

Re: Varibly modified type in switch statement
 
Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> writes:

> On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:
>
>> 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?

Shao Miller 01-14-2013 07:52 AM

Re: Varibly modified type in switch statement
 
On 12/27/2012 10:35, Tim Rentsch wrote:
> Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> writes:
>
>> On Mon, 24 Dec 2012 03:27:46 -0800, Zhang Yuan wrote:
>>
>>> 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.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter


All times are GMT. The time now is 01:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.