delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/28/17:00:17

From: "Tim \"Zastai\" Van Holder" <zastai AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <u9hmjs4dknggq2jboelfo8o4pv0b0op60h AT 4ax DOT com>
Subject: Re: Size_Of in DJGPP...Bug?
Lines: 58
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Message-ID: <m1r65.110534$6t3.155967@afrodite.telenet-ops.be>
Date: Wed, 28 Jun 2000 17:58:42 GMT
NNTP-Posting-Host: 213.224.63.5
X-Complaints-To: abuse AT pandora DOT be
X-Trace: afrodite.telenet-ops.be 962215122 213.224.63.5 (Wed, 28 Jun 2000 19:58:42 MET DST)
NNTP-Posting-Date: Wed, 28 Jun 2000 19:58:42 MET DST
Organization: Pandora-- Met vlotte tred op Internet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Radical NetSurfer <radsmail AT juno DOT com> wrote in message
news:u9hmjs4dknggq2jboelfo8o4pv0b0op60h AT 4ax DOT com...
> // printf("The size_of a Enum   is %ld\n", sizeof(enum));
>
> QUESTION:
> Why is not proper to use 'enum' in Size_of ??
> Normally, this is defined as 2-Bytes (=to short or int),
> but in DJGPP, its a SYNTAX error....
>

This seems exctly right to me. 'enum' isn't a type, so the sizeof operator
can't legally be applied to it.
'enum foo', 'enum bar' and 'enum foobar' ARE type, and you can apply sizeof
to them. Note that in theory, the compiler is allowed to decide the size of
an enum by looking at the range of values it contains; an enum containing
just values between -127 and 128 (or between 0 and 255) could be put in
single byte, while an enum using -12 and 2375 would require 2 bytes, and so
on. in practice, usually an int is used, as shown below:

/* sizeof-enum-test.c */
#include <stdio.h>

enum foo {
  value1 = 0, value2 = 1, value3 = 200
}; /* Needs just 1 byte */
enum bar {
  value1 = 0, value3 = 200, value3 = 1300
}; /* Needs 2 bytes */
enum foobar {
  value1 = -17, value2 = 1300, value3 = 5000
}; /* Needs > 2 bytes */

int
main(void)
{
  printf("Note: Using gcc %s\n", __VERSION__);
  printf("enum foo,    needing  1 byte  takes up %lu bytes.\n",
sizeof(foo));
  printf("enum bar,    needing  2 bytes takes up %lu bytes.\n",
sizeof(bar));
  printf("enum foobar, needing >2 bytes takes up %lu bytes.\n",
sizeof(foobar));
  return 0;
}

----------- Output follows:
Note: Using gcc 2.95.2 19991024 (release)
enum foo,    needing  1 byte  takes up 4 bytes.
enum bar,    needing  2 bytes takes up 4 bytes.
enum foobar, needing >2 bytes takes up 4 bytes.
-----------

NOTE: Optimizations (including -Os) do NOT affect this -- gcc always uses
int-sized storage for enums.

Zastai


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019