Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > padding and enums

Reply
Thread Tools

padding and enums

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      04-04-2008
Hi all,

arrays are guaranteed to be contiguous with no padding before or
after any array member , but what about enums ..???
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> arrays are guaranteed to be contiguous with no padding before or
> after any array member , but what about enums ..???


Um, what about them?

An enumerated type is compatible with some implementation-defined
integral type. That type may have padding bits. Is that what you're
asking?

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      04-04-2008
On Apr 4, 9:38*pm, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > *arrays are guaranteed to be contiguous with no padding before or
> > after any array member , but what about enums ..???

>
> Um, what about them?
>
> An enumerated type is compatible with some implementation-defined
> integral type. *That type may have padding bits. *Is that what you're
> asking?
>


I just wanted to clear the doubt that, whether the concept structure
padding also applies to enums ..???
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-04-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:
>On Apr 4, 9:38=A0pm, Keith Thompson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) writes:
>> > =A0arrays are guaranteed to be contiguous with no padding before or
>> > after any array member , but what about enums ..???


>> Um, what about them?


>> An enumerated type is compatible with some implementation-defined
>> integral type. =A0That type may have padding bits. =A0Is that what you're
>> asking?


> I just wanted to clear the doubt that, whether the concept structure
>padding also applies to enums ..???


Your original wording about arrays is not completely correct.
It is possible for there to be padding after the final array member in
a structure. For example:

struct foo { char bar[3]; int baz };

then there could be padding after bar[2] and before baz.


An enumeration type is some integral type (implementation-defined which),
so the same rules apply to it as apply to other integral types.
--
"He wove a great web of knowledge, linking everything together,
and sat modestly at a switchboard at the center, eager to help."
-- Walter Kerr
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-04-2008
(E-Mail Removed) wrote:

> Hi all,
>
> arrays are guaranteed to be contiguous with no padding before or
> after any array member , but what about enums ..???


Enumerations are not an aggregate. The concept of padding means nothing
in that context. It seems like you think that declaring an enum means
making some data structure with all those in some sort of sequence. It
doesn't. It creates a type, which is in reality some sort of alias for
one of the integral types, with some predefined aliased values.




Brian
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed)>,
> <(E-Mail Removed)> wrote:
>>On Apr 4, 9:38=A0pm, Keith Thompson <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) writes:
>>> > arrays are guaranteed to be contiguous with no padding before or
>>> > after any array member , but what about enums ..???

>
>>> Um, what about them?

>
>>> An enumerated type is compatible with some implementation-defined
>>> integral type. =A0That type may have padding bits. =A0Is that what you're
>>> asking?

>
>> I just wanted to clear the doubt that, whether the concept structure
>>padding also applies to enums ..???

>
> Your original wording about arrays is not completely correct.
> It is possible for there to be padding after the final array member in
> a structure. For example:
>
> struct foo { char bar[3]; int baz };
>
> then there could be padding after bar[2] and before baz.


But that padding isn't part of the array. In effect, there can be
padding after the array, but not after an element of an array (a
debatable distinction, but I think that's the best way to look at it).

[...]

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-04-2008
(E-Mail Removed) wrote:
> I just wanted to clear the doubt that, whether the concept structure
> padding also applies to enums ..???


"Structure padding" is something that is inserted between small pieces
(members) of a larger composite object. Such composite objects are
referred as "aggregates" in C. Structures are aggregates, they can have
padding between their members. Arrays are aggregates, although they
cannot have any array-specific padding between their elements.

Enums are not aggregates. They don't consist of smaller pieces. There's
nowhere in enum you can insert that padding to. So I don't see how could
anyone even start applying the concept of "struct padding" to enums.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-04-2008
(E-Mail Removed) wrote:
>
> arrays are guaranteed to be contiguous with no padding before or
> after any array member , but what about enums ..???


No they are not.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-05-2008
CBFalconer <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>> arrays are guaranteed to be contiguous with no padding before or
>> after any array member , but what about enums ..???

>
> No they are not.


What are not what?

If you're disputing the statement that arrays are guaranteed to be
contiguous, I'm afraid you're mistaken; there can be no padding before
or after any array element. (Depending on the context, there can be
padding before or after the whole array, just as for any object.)

If you're trying to answer the "what about enums" question, I'd like
to know how you've managed to interpret the question so it has a
meaningful yes or no answer.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-05-2008
Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:
>> (E-Mail Removed) wrote:
>>
>>> arrays are guaranteed to be contiguous with no padding before
>>> or after any array member , but what about enums ..???

>>
>> No they are not.

>
> What are not what?
>
> If you're disputing the statement that arrays are guaranteed to
> be contiguous, I'm afraid you're mistaken; there can be no
> padding before or after any array element. (Depending on the
> context, there can be padding before or after the whole array,
> just as for any object.)


If, for example, the basic element is an array of 3 bytes, but
requires the alignment of 4, the individual element will be padded
with an extra byte. Something like:

typedef struct elem {
int i;
char c;
} elem;

and, to me, each element of:

elem array[MAX};

will be padded accordingly. This is why such arrays cannot be
compared for equality with simple code.

Yes, I was overly terse AGAIN. Sorry.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
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
enums and bitfileds and signs Jason Kraftcheck C++ 2 11-16-2007 10:19 AM
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
getEnclosingClass and enums ogmios01@gmail.com Java 0 02-27-2006 08:24 PM
SQL Storage, Enums, Roles and Clean Coding !!! Mr Newbie ASP .Net 10 11-22-2005 11:53 AM
enums in C and C++ Simon Elliott C++ 13 11-01-2004 10:26 AM



Advertisments