Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Varibly modified type in switch statement

Reply
Thread Tools

Varibly modified type in switch statement

 
 
Zhang Yuan
Guest
Posts: n/a
 
      12-24-2012
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.

------
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      12-24-2012
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
 
Reply With Quote
 
 
 
 
Zhang Yuan
Guest
Posts: n/a
 
      12-24-2012

> 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.
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      12-25-2012
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
}
}

 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      12-27-2012
Bart van Ingen Schenau <(E-Mail Removed)> 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?
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      01-14-2013
On 12/27/2012 10:35, Tim Rentsch wrote:
> Bart van Ingen Schenau <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
date modified if statement Naji Perl Misc 30 10-31-2005 04:00 AM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
How to use enum that are not int type in the switch( ) statement in java? Bruce Sam Java 2 01-10-2005 08:20 AM



Advertisments