public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* No effect of -fshort-enums..is it a bug
@ 2005-09-21 12:17 Gaurav Gautam, Noida
  2005-09-21 12:39 ` Daniel Jacobowitz
  0 siblings, 1 reply; 22+ messages in thread
From: Gaurav Gautam, Noida @ 2005-09-21 12:17 UTC (permalink / raw)
  To: gcc, gcc-help

Hi,

I have compiled a testcase

int main()
{
        enum aa {
        a = 0, b =127  , c
        };
 
        printf("size = %d  %d %d\n", sizeof(a),sizeof(b), sizeof(c));
        printf("value= %d  %d %d\n", a,b,c);
        return 0;
}

On gcc (GCC) 4.1.0 20050915 (experimental) with the following option -fshort-enums.

The option -fshort-enums has no effect and the output is same as it is without this option.


I also tried the same tc on gcc (GCC) 3.3.1 (SuSE Linux). But in this case the output changed with and without this option.

I am using an SuSe linux -- X86_AMD64 machine.

I think it's a bug. Can anybody please confirm?

Thanks
Gaurav

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 12:17 No effect of -fshort-enums..is it a bug Gaurav Gautam, Noida
@ 2005-09-21 12:39 ` Daniel Jacobowitz
  0 siblings, 0 replies; 22+ messages in thread
From: Daniel Jacobowitz @ 2005-09-21 12:39 UTC (permalink / raw)
  To: Gaurav Gautam, Noida; +Cc: gcc, gcc-help

On Wed, Sep 21, 2005 at 05:46:58PM +0530, Gaurav Gautam, Noida wrote:
> int main()
> {
>         enum aa {
>         a = 0, b =127  , c
>         };
>  
>         printf("size = %d  %d %d\n", sizeof(a),sizeof(b), sizeof(c));
>         printf("value= %d  %d %d\n", a,b,c);
>         return 0;
> }

> The option -fshort-enums has no effect and the output is same as it is without this option.

It's not a bug.  Add sizeof(enum aa) to your printf; _that_ will be
affected by -fshort-enums.  The type of the enumerators remains int.

-- 
Daniel Jacobowitz
CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-23 14:06 Gaurav Gautam, Noida
@ 2005-09-23 14:21 ` John Love-Jensen
  0 siblings, 0 replies; 22+ messages in thread
From: John Love-Jensen @ 2005-09-23 14:21 UTC (permalink / raw)
  To: Gaurav Gautam, Noida, gcc, MSX to GCC

Hi Gaurav,

You could do this...
q = 4294967295U

Or you could use -std=iso9899:1999 (perhaps with -pedantic) for the compiler
to produce an error.  Assuming you are using GCC 4.x.

Or if you *want* to allow that, you could do this...
-std=gnu99

I'm guessing as to which version of GCC you are using, and what command line
options you used to compile with, and your platform's architecture.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: No effect of -fshort-enums..is it a bug
@ 2005-09-23 14:06 Gaurav Gautam, Noida
  2005-09-23 14:21 ` John Love-Jensen
  0 siblings, 1 reply; 22+ messages in thread
From: Gaurav Gautam, Noida @ 2005-09-23 14:06 UTC (permalink / raw)
  To: Dave Korn, John Love-Jensen, gcc, MSX to GCC


> 
> > Hi Gaurav,
> >
> >> Please confirm which of the two outputs is correct and why is there
a
> > difference in the output of two versions of compiler?
> >
> > Both outputs are "correct".
> >
> 
> 
>   No, the standard is entirely unambiguous:
> 
> --------------------------------------------------------
> 6.4.4.3 Enumeration constants
> Syntax
> 1 enumeration-constant:
> identifier
> Semantics
> 2 An identifier declared as an enumeration constant has type int.
> --------------------------------------------------------
> 
>   The enumeration constants denoted by the identifiers a, b, and c are
> therefore of type int and must have size 4 (on a standard 32-bit
system),
> regardless of the size of the enumerated type aa.

Thank you all for your replies. But I have another question to ask
Consider another example

#include <stdio.h>
int main()
{
  enum ff {
        p = 0, q = 4294967295, r              
        };
        printf("size = %d  %d %d\n", sizeof(enum ff),sizeof(q),
sizeof(r));
        printf("value= %d  %d %d\n", p,q,r);
}

Its output is with default options on gcc :
size = 8  8 8
value= 0  -1 0

Enumerator are supposed to be int and must have size 4. why are they
being treated as long here and taking 8 bytes? Size of int on my machine
is 4.

Also the compiler doesn't even gives a warning when the tc is compiled.
> 
> 
> > (Neither output is compliant to the standard, of course, as -fshort-
> enums
> > is a deviation from the standard.)
> 
>   Nope, the standard is entirely unambiguous:
> 
> --------------------------------------------------------
> 6.7.2.2 Enumeration specifiers
> 
> 4 Each enumerated type shall be compatible with an integer type. The
> choice
> of type is
> implementation-defined,97) but shall be capable of representing the
values
> of all the
> members of the enumeration. The enumerated type is incomplete until
after
> the } that
> terminates the list of enumerator declarations.
> --------------------------------------------------------
> 
>   The choice of what integer type to use to store a value of an
enumerated
> type is implementation-defined, and if a char is big enough to
represent
> all
> the values, the implementation is at liberty to use a char.
> 
> 
> 
>     cheers,
>       DaveK
> --
> Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 15:42 ` Andreas Schwab
@ 2005-09-23  9:16   ` Bernhard R. Link
  0 siblings, 0 replies; 22+ messages in thread
From: Bernhard R. Link @ 2005-09-23  9:16 UTC (permalink / raw)
  To: gcc, gcc-help

* Andreas Schwab <schwab@suse.de> [050921 17:46]:
> "Gaurav Gautam, Noida" <gauravga@noida.hcltech.com> writes:
> > Does -fshort-enum guides the size of enumeration type or the size of
> > enumerator constant ?
> An enumerator constant is not an object, thus it has no size of its own.
> Since the enumerator constants are of type int, not the enum type,
> -fshort-enum should not have any effect on their type.

I think some warning for this case would be nice. While the value is
looks clearly definied, I think almost all cases where this is actually
used will be errors. (The same with sizeof(void), which most likely
only happens with p=malloc(sizeof(*p)) from macros).
Is there already a warning against that (I cannot find any in the info
page) or is there any chance to get such a thing implemented?

Hochachtungsvoll,
	Bernhard R. Link

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 18:31       ` Daniel Jacobowitz
@ 2005-09-22 19:03         ` Paul Brook
  0 siblings, 0 replies; 22+ messages in thread
From: Paul Brook @ 2005-09-22 19:03 UTC (permalink / raw)
  To: gcc
  Cc: Daniel Jacobowitz, Robert Dewar, Dave Korn,
	'John Love-Jensen', 'Gaurav Gautam, Noida',
	'MSX to GCC'

On Thursday 22 September 2005 19:31, Daniel Jacobowitz wrote:
> On Thu, Sep 22, 2005 at 12:50:39PM -0400, Robert Dewar wrote:
> > of course, but the behavior of a compiler with a special implementation
> > dependent switch is not specified by the standard! Switches can do any
> > amount of violence to the standard you like, the only requirement is
> > that there be a defined set of switches which gives standard defined
> > semantics.
>
> Except that the point I've been trying to make for the last day is that
> -fshort-enums does no damage to the standard.  At least for C99.

It's also worth noting that -fshort-enums is the default on some targets, as 
required by the platform ABI.

Paul

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 16:51     ` Robert Dewar
@ 2005-09-22 18:31       ` Daniel Jacobowitz
  2005-09-22 19:03         ` Paul Brook
  0 siblings, 1 reply; 22+ messages in thread
From: Daniel Jacobowitz @ 2005-09-22 18:31 UTC (permalink / raw)
  To: Robert Dewar
  Cc: Dave Korn, 'John Love-Jensen',
	'Gaurav Gautam, Noida', gcc, 'MSX to GCC'

On Thu, Sep 22, 2005 at 12:50:39PM -0400, Robert Dewar wrote:
> of course, but the behavior of a compiler with a special implementation
> dependent switch is not specified by the standard! Switches can do any
> amount of violence to the standard you like, the only requirement is
> that there be a defined set of switches which gives standard defined
> semantics.

Except that the point I've been trying to make for the last day is that
-fshort-enums does no damage to the standard.  At least for C99.

-- 
Daniel Jacobowitz
CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 16:20   ` Dave Korn
  2005-09-22 16:28     ` John Love-Jensen
@ 2005-09-22 16:51     ` Robert Dewar
  2005-09-22 18:31       ` Daniel Jacobowitz
  1 sibling, 1 reply; 22+ messages in thread
From: Robert Dewar @ 2005-09-22 16:51 UTC (permalink / raw)
  To: Dave Korn
  Cc: 'John Love-Jensen', 'Gaurav Gautam, Noida',
	gcc, 'MSX to GCC'

Dave Korn wrote:
>
>>>Please confirm which of the two outputs is correct and why is there a
>>
>>difference in the output of two versions of compiler?
>>
>>Both outputs are "correct".

>   No, the standard is entirely unambiguous:

of course, but the behavior of a compiler with a special implementation
dependent switch is not specified by the standard! Switches can do any
amount of violence to the standard you like, the only requirement is
that there be a defined set of switches which gives standard defined
semantics.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 16:28     ` John Love-Jensen
@ 2005-09-22 16:41       ` Gabriel Dos Reis
  0 siblings, 0 replies; 22+ messages in thread
From: Gabriel Dos Reis @ 2005-09-22 16:41 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: 'Gaurav Gautam, Noida', gcc, MSX to GCC

John Love-Jensen <eljay@adobe.com> writes:

| Hi Dave, Daniel, and Gaurav,
| 
| For C99, I stand corrected.
| 
| For C89 or C++98, I think my statement is applicable. 

No, for C++98 your statement is even more incorrect because
enumerators (the constants) are NOT of type int -- and that has been
so for looon time now that I'm surprise.  In fact, I wish the
enumeration underlying type were not int by default.  But I suspect
that for interoperability with C, it was decided -fshort-enum wasn't
the default (I wish it was).

-- Gaby

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 16:26   ` Gabriel Dos Reis
@ 2005-09-22 16:33     ` John Love-Jensen
  0 siblings, 0 replies; 22+ messages in thread
From: John Love-Jensen @ 2005-09-22 16:33 UTC (permalink / raw)
  To: Gaurav Gautam, Noida, gcc, MSX to GCC

Hi Gaby, Dave, Daniel, and Gaurav,

>This is incorrect and misleading.

I concur.  I retract my previous statement, and direct
seekers-of-clarification to the previous posts that answered this issue.  My
apologies for my confusion.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 16:20   ` Dave Korn
@ 2005-09-22 16:28     ` John Love-Jensen
  2005-09-22 16:41       ` Gabriel Dos Reis
  2005-09-22 16:51     ` Robert Dewar
  1 sibling, 1 reply; 22+ messages in thread
From: John Love-Jensen @ 2005-09-22 16:28 UTC (permalink / raw)
  To: 'Gaurav Gautam, Noida', gcc, MSX to GCC

Hi Dave, Daniel, and Gaurav,

For C99, I stand corrected.

For C89 or C++98, I think my statement is applicable.  (But until I
double-check by reading those standards, take that with a grain of salt.)

For all three, having enum be an int (signed or unsigned) is legit of
course.

For all three, having enum be a long is (at best) a compiler extension, at
worst (-pedantic ?) not supported.  If I'm reading the specs correctly.
NOTE:  not germane to Gaurav's question, just talking out loud.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 15:36 ` John Love-Jensen
  2005-09-22 16:15   ` Daniel Jacobowitz
  2005-09-22 16:20   ` Dave Korn
@ 2005-09-22 16:26   ` Gabriel Dos Reis
  2005-09-22 16:33     ` John Love-Jensen
  2 siblings, 1 reply; 22+ messages in thread
From: Gabriel Dos Reis @ 2005-09-22 16:26 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: Gaurav Gautam, Noida, gcc, MSX to GCC

John Love-Jensen <eljay@adobe.com> writes:

| Hi Gaurav,
| 
| >Please confirm which of the two outputs is correct and why is there a
| difference in the output of two versions of compiler?
| 
| Both outputs are "correct".
| 
| (Neither output is compliant to the standard, of course, as -fshort-enums is
| a deviation from the standard.)

This is incorrect and misleading.

-- Gaby

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: No effect of -fshort-enums..is it a bug
  2005-09-22 15:36 ` John Love-Jensen
  2005-09-22 16:15   ` Daniel Jacobowitz
@ 2005-09-22 16:20   ` Dave Korn
  2005-09-22 16:28     ` John Love-Jensen
  2005-09-22 16:51     ` Robert Dewar
  2005-09-22 16:26   ` Gabriel Dos Reis
  2 siblings, 2 replies; 22+ messages in thread
From: Dave Korn @ 2005-09-22 16:20 UTC (permalink / raw)
  To: 'John Love-Jensen', 'Gaurav Gautam, Noida',
	gcc, 'MSX to GCC'

----Original Message----
>From: John Love-Jensen
>Sent: 22 September 2005 16:34

> Hi Gaurav,
> 
>> Please confirm which of the two outputs is correct and why is there a
> difference in the output of two versions of compiler?
> 
> Both outputs are "correct".
> 


  No, the standard is entirely unambiguous:

--------------------------------------------------------
6.4.4.3 Enumeration constants
Syntax
1 enumeration-constant:
identifier
Semantics
2 An identifier declared as an enumeration constant has type int.
--------------------------------------------------------

  The enumeration constants denoted by the identifiers a, b, and c are
therefore of type int and must have size 4 (on a standard 32-bit system),
regardless of the size of the enumerated type aa.


> (Neither output is compliant to the standard, of course, as -fshort-enums
> is a deviation from the standard.)

  Nope, the standard is entirely unambiguous:

--------------------------------------------------------
6.7.2.2 Enumeration specifiers

4 Each enumerated type shall be compatible with an integer type. The choice
of type is
implementation-defined,97) but shall be capable of representing the values
of all the
members of the enumeration. The enumerated type is incomplete until after
the } that
terminates the list of enumerator declarations.
--------------------------------------------------------

  The choice of what integer type to use to store a value of an enumerated
type is implementation-defined, and if a char is big enough to represent all
the values, the implementation is at liberty to use a char.



    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 15:36 ` John Love-Jensen
@ 2005-09-22 16:15   ` Daniel Jacobowitz
  2005-09-22 16:20   ` Dave Korn
  2005-09-22 16:26   ` Gabriel Dos Reis
  2 siblings, 0 replies; 22+ messages in thread
From: Daniel Jacobowitz @ 2005-09-22 16:15 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: Gaurav Gautam, Noida, gcc, MSX to GCC

On Thu, Sep 22, 2005 at 10:34:19AM -0500, John Love-Jensen wrote:
> Hi Gaurav,
> 
> >Please confirm which of the two outputs is correct and why is there a
> difference in the output of two versions of compiler?
> 
> Both outputs are "correct".
> 
> (Neither output is compliant to the standard, of course, as -fshort-enums is
> a deviation from the standard.)

That's incorrect, please read my response again.  The current output is
C99-conformant.

(It is surprising, though, on many non-embedded targets.  For instance
there are considerable difficulties using this option on GNU/Linux.)

-- 
Daniel Jacobowitz
CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 13:40 Gaurav Gautam, Noida
  2005-09-22 15:32 ` Neil Booth
@ 2005-09-22 15:36 ` John Love-Jensen
  2005-09-22 16:15   ` Daniel Jacobowitz
                     ` (2 more replies)
  1 sibling, 3 replies; 22+ messages in thread
From: John Love-Jensen @ 2005-09-22 15:36 UTC (permalink / raw)
  To: Gaurav Gautam, Noida, gcc, MSX to GCC

Hi Gaurav,

>Please confirm which of the two outputs is correct and why is there a
difference in the output of two versions of compiler?

Both outputs are "correct".

(Neither output is compliant to the standard, of course, as -fshort-enums is
a deviation from the standard.)

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-22 13:40 Gaurav Gautam, Noida
@ 2005-09-22 15:32 ` Neil Booth
  2005-09-22 15:36 ` John Love-Jensen
  1 sibling, 0 replies; 22+ messages in thread
From: Neil Booth @ 2005-09-22 15:32 UTC (permalink / raw)
  To: Gaurav Gautam, Noida; +Cc: gcc, gcc-help

Gaurav Gautam, Noida wrote:-

>  #include <stdio.h>
>  int main()
>  {
>    enum aa {
>    a = 0, b =127  , c
>    };
> printf("size = %d  %d %d\n", sizeof(enum aa),sizeof(b),sizeof(c));
> printf("value= %d  %d %d\n", a,b,c);
> return 0;
> )
>  
>  The output is
>  size = 1  1 1
>  value= 0  127 128
>  when  gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums.
>  
>  And
>  
>  size = 1  4 4
>  value= 0  127 128
>  when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums.
>  
> Please confirm which of the two outputs is correct and why is there a
> difference in the output of two versions of compiler?

4.1.0 is correct, therefore 3.3.1 was buggy.

Neil.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: No effect of -fshort-enums..is it a bug
@ 2005-09-22 13:40 Gaurav Gautam, Noida
  2005-09-22 15:32 ` Neil Booth
  2005-09-22 15:36 ` John Love-Jensen
  0 siblings, 2 replies; 22+ messages in thread
From: Gaurav Gautam, Noida @ 2005-09-22 13:40 UTC (permalink / raw)
  To: Gaurav Gautam, Noida, gcc, gcc-help

Thanks for the reply, but I did not get the answer to my question.
My question is:
In the below mentioned program

 #include <stdio.h>
 int main()
 {
   enum aa {
   a = 0, b =127  , c
   };
printf("size = %d  %d %d\n", sizeof(enum aa),sizeof(b),sizeof(c));
printf("value= %d  %d %d\n", a,b,c);
return 0;
)
 
 The output is
 size = 1  1 1
 value= 0  127 128
 when  gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums.
 
 And
 
 size = 1  4 4
 value= 0  127 128
 when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums.
 
Please confirm which of the two outputs is correct and why is there a
difference in the output of two versions of compiler?

Thanks
Gaurav gautam


> -----Original Message-----
> From: Gaurav Gautam, Noida
> Sent: Wednesday, September 21, 2005 7:04 PM
> To: 'gcc@gcc.gnu.org'; 'gcc-help@gcc.gnu.org'
> Cc: 'Daniel Jacobowitz'
> Subject: RE: No effect of -fshort-enums..is it a bug
> 
> Thanks for the reply,
> 
> But why is there a difference in the output of same tc, with an old
gcc
> compiler and a new version of compiler.
> 
> Was there a bug in the earlier gcc.
> 
> I have a doubt.
> 
> Gcc manual says that
> 
> "-fshort-enums
>     Allocate to an enum type only as many bytes as it needs for the
> declared range of possible values. Specifically, the enum type will be
> equivalent to the smallest integer type which has enough room."
> 
> Does -fshort-enum guides the size of enumeration type or the size of
> enumerator constant ?
> 
> After modifying the tc as
> 
> #include <stdio.h>
> int main()
> {
>         enum aa {
>         a = 0, b =127  , c
>         };
> 
>         printf("size = %d  %d %d\n", sizeof(enum aa),sizeof(b),
> sizeof(c));
>         printf("value= %d  %d %d\n", a,b,c);
> 	  return 0;
> )
> 
> The output is
> 
> size = 1  1 1
> value= 0  127 128
> when  gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums.
> 
> 
> And
> 
> size = 1  4 4
> value= 0  127 128
> 
> when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums.
> 
> Which of the two output is standard confirming.?
> 
> 
> > -----Original Message-----
> > From: Daniel Jacobowitz [mailto:drow@false.org]
> > Sent: Wednesday, September 21, 2005 6:10 PM
> > To: Gaurav Gautam, Noida
> > Cc: gcc@gcc.gnu.org; gcc-help@gcc.gnu.org
> > Subject: Re: No effect of -fshort-enums..is it a bug
> >
> > On Wed, Sep 21, 2005 at 05:46:58PM +0530, Gaurav Gautam, Noida
wrote:
> > > int main()
> > > {
> > >         enum aa {
> > >         a = 0, b =127  , c
> > >         };
> > >
> > >         printf("size = %d  %d %d\n", sizeof(a),sizeof(b),
sizeof(c));
> > >         printf("value= %d  %d %d\n", a,b,c);
> > >         return 0;
> > > }
> >
> > > The option -fshort-enums has no effect and the output is same as
it is
> > without this option.
> >
> > It's not a bug.  Add sizeof(enum aa) to your printf; _that_ will be
> > affected by -fshort-enums.  The type of the enumerators remains int.
> >
> > --
> > Daniel Jacobowitz
> > CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 14:55   ` Robert Dewar
@ 2005-09-21 17:26     ` Daniel Jacobowitz
  0 siblings, 0 replies; 22+ messages in thread
From: Daniel Jacobowitz @ 2005-09-21 17:26 UTC (permalink / raw)
  To: Robert Dewar; +Cc: Gaurav Gautam, Noida, gcc, gcc-help

On Wed, Sep 21, 2005 at 10:54:56AM -0400, Robert Dewar wrote:
> Daniel Jacobowitz wrote:
> >
> >I'm not 100% sure what #3 means for enumerators whose value does not
> >fit in the range of "int", but it's pretty clear that the
> >implementation is not allowed to change the type of enumerators.
> 
> Of course an implementation can do whatever it likes in response
> to switches, compiler dependent declarations etc.

Yes, but -fshort-enums is not intended to interfere with C99
compliance.  And nowadays it doesn't.

-- 
Daniel Jacobowitz
CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 13:35 Gaurav Gautam, Noida
  2005-09-21 14:19 ` Daniel Jacobowitz
@ 2005-09-21 15:42 ` Andreas Schwab
  2005-09-23  9:16   ` Bernhard R. Link
  1 sibling, 1 reply; 22+ messages in thread
From: Andreas Schwab @ 2005-09-21 15:42 UTC (permalink / raw)
  To: Gaurav Gautam, Noida; +Cc: gcc, gcc-help, Daniel Jacobowitz

"Gaurav Gautam, Noida" <gauravga@noida.hcltech.com> writes:

> Does -fshort-enum guides the size of enumeration type or the size of
> enumerator constant ?

An enumerator constant is not an object, thus it has no size of its own.
Since the enumerator constants are of type int, not the enum type,
-fshort-enum should not have any effect on their type.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 14:19 ` Daniel Jacobowitz
@ 2005-09-21 14:55   ` Robert Dewar
  2005-09-21 17:26     ` Daniel Jacobowitz
  0 siblings, 1 reply; 22+ messages in thread
From: Robert Dewar @ 2005-09-21 14:55 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: Gaurav Gautam, Noida, gcc, gcc-help

Daniel Jacobowitz wrote:
>
> I'm not 100% sure what #3 means for enumerators whose value does not
> fit in the range of "int", but it's pretty clear that the
> implementation is not allowed to change the type of enumerators.

Of course an implementation can do whatever it likes in response
to switches, compiler dependent declarations etc.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: No effect of -fshort-enums..is it a bug
  2005-09-21 13:35 Gaurav Gautam, Noida
@ 2005-09-21 14:19 ` Daniel Jacobowitz
  2005-09-21 14:55   ` Robert Dewar
  2005-09-21 15:42 ` Andreas Schwab
  1 sibling, 1 reply; 22+ messages in thread
From: Daniel Jacobowitz @ 2005-09-21 14:19 UTC (permalink / raw)
  To: Gaurav Gautam, Noida; +Cc: gcc, gcc-help

On Wed, Sep 21, 2005 at 07:03:49PM +0530, Gaurav Gautam, Noida wrote:
> Thanks for the reply,
> 
> But why is there a difference in the output of same tc, with an old gcc
> compiler and a new version of compiler. 
> 
> Was there a bug in the earlier gcc.
> 
> I have a doubt.
> 
> Gcc manual says that
> 
> "-fshort-enums
>     Allocate to an enum type only as many bytes as it needs for the
> declared range of possible values. Specifically, the enum type will be
> equivalent to the smallest integer type which has enough room."
> 
> Does -fshort-enum guides the size of enumeration type or the size of
> enumerator constant ?

C99, section 6.7.2.2:

       [#3]  The  identifiers in an enumerator list are declared as
       constants that have type int and may  appear  wherever  such
       are  permitted.

...

       [#4] Each enumerated type shall be compatible with  char,  a
       signed  integer  type,  or  an  unsigned  integer type.  The
       choice  of  type  is  implementation-defined,  but  shall be
       capable of representing the values of all the members of the
       enumeration.

I'm not 100% sure what #3 means for enumerators whose value does not
fit in the range of "int", but it's pretty clear that the
implementation is not allowed to change the type of enumerators.


-- 
Daniel Jacobowitz
CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: No effect of -fshort-enums..is it a bug
@ 2005-09-21 13:35 Gaurav Gautam, Noida
  2005-09-21 14:19 ` Daniel Jacobowitz
  2005-09-21 15:42 ` Andreas Schwab
  0 siblings, 2 replies; 22+ messages in thread
From: Gaurav Gautam, Noida @ 2005-09-21 13:35 UTC (permalink / raw)
  To: gcc, gcc-help; +Cc: Daniel Jacobowitz

Thanks for the reply,

But why is there a difference in the output of same tc, with an old gcc
compiler and a new version of compiler. 

Was there a bug in the earlier gcc.

I have a doubt.

Gcc manual says that

"-fshort-enums
    Allocate to an enum type only as many bytes as it needs for the
declared range of possible values. Specifically, the enum type will be
equivalent to the smallest integer type which has enough room."

Does -fshort-enum guides the size of enumeration type or the size of
enumerator constant ?

After modifying the tc as 

#include <stdio.h>
int main()
{
        enum aa {
        a = 0, b =127  , c
        };

        printf("size = %d  %d %d\n", sizeof(enum aa),sizeof(b),
sizeof(c));
        printf("value= %d  %d %d\n", a,b,c);
	  return 0;
)

The output is 

size = 1  1 1
value= 0  127 128
when  gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums.


And 

size = 1  4 4
value= 0  127 128

when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums.

Which of the two output is standard confirming.?


> -----Original Message-----
> From: Daniel Jacobowitz [mailto:drow@false.org]
> Sent: Wednesday, September 21, 2005 6:10 PM
> To: Gaurav Gautam, Noida
> Cc: gcc@gcc.gnu.org; gcc-help@gcc.gnu.org
> Subject: Re: No effect of -fshort-enums..is it a bug
> 
> On Wed, Sep 21, 2005 at 05:46:58PM +0530, Gaurav Gautam, Noida wrote:
> > int main()
> > {
> >         enum aa {
> >         a = 0, b =127  , c
> >         };
> >
> >         printf("size = %d  %d %d\n", sizeof(a),sizeof(b),
sizeof(c));
> >         printf("value= %d  %d %d\n", a,b,c);
> >         return 0;
> > }
> 
> > The option -fshort-enums has no effect and the output is same as it
is
> without this option.
> 
> It's not a bug.  Add sizeof(enum aa) to your printf; _that_ will be
> affected by -fshort-enums.  The type of the enumerators remains int.
> 
> --
> Daniel Jacobowitz
> CodeSourcery, LLC

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2005-09-23 14:21 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-21 12:17 No effect of -fshort-enums..is it a bug Gaurav Gautam, Noida
2005-09-21 12:39 ` Daniel Jacobowitz
2005-09-21 13:35 Gaurav Gautam, Noida
2005-09-21 14:19 ` Daniel Jacobowitz
2005-09-21 14:55   ` Robert Dewar
2005-09-21 17:26     ` Daniel Jacobowitz
2005-09-21 15:42 ` Andreas Schwab
2005-09-23  9:16   ` Bernhard R. Link
2005-09-22 13:40 Gaurav Gautam, Noida
2005-09-22 15:32 ` Neil Booth
2005-09-22 15:36 ` John Love-Jensen
2005-09-22 16:15   ` Daniel Jacobowitz
2005-09-22 16:20   ` Dave Korn
2005-09-22 16:28     ` John Love-Jensen
2005-09-22 16:41       ` Gabriel Dos Reis
2005-09-22 16:51     ` Robert Dewar
2005-09-22 18:31       ` Daniel Jacobowitz
2005-09-22 19:03         ` Paul Brook
2005-09-22 16:26   ` Gabriel Dos Reis
2005-09-22 16:33     ` John Love-Jensen
2005-09-23 14:06 Gaurav Gautam, Noida
2005-09-23 14:21 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).