Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

padding and enums

 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-05-2008
CBFalconer wrote:
> Keith Thompson wrote:
>> CBFalconer <(E-Mail Removed)> writes:
>>> http://www.velocityreviews.com/forums/(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;


Yes, but this really has nothing much to do with the array itself. This
trailing padding exists as an integral part of the above struct, not as
something inserted additionally _between_ the elements of the array and
specific to the the array itself.

For arrays, the following relation must hold

sizeof array = sizeof element * number_of_elements

This eliminates any possibility of the array inserting any additional
padding between its elements. Any necessary padding should be already
present in the element itself (in terms of it size, as returned by
'sizeof').

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


I'm not really sure what exactly you mean here. But if you are saying
that arrays can't be compared by raw memory comparison because padding
areas might contain unpredictable bit patterns, then first of all this
issue actually already applies to structs. It already exists for
standalone structs, no arrays necessary. And it applies to arrays of
structs just as mere consequence of that, not because of some specific
property of arrays.

If array elements contain no padding of any kind (i.e. if they can be
compared by raw memory comparison), then whole arrays of such elements
can also be compared by raw memory comparison because, once again,
arrays are not allowed to introduce any additional padding between their
elements.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-05-2008
CBFalconer <(E-Mail Removed)> writes:
> 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.


First off, your element type isn't an array of 3 bytes. If it were,
it would have to be byte-aligned. For example:

typedef unsigned char tri_byte[3];
tri_byte arr[10];

Type tri_byte cannot require 4-byte alignment, and sizeof arr must be
30 in any conforming implementation. No padding is permitted within
either array type.

As for the example you actually used, I assume you intend
sizeof(int)== 2. If int requires word alignment, then sizeof(struct
elem) must be (at least) 4; padding is inserted within the structure
itself, probably after c (or possibly after i). This padding is
required because of the requirements on arrays, but it applies to all
objects of type struct elem.

Every object type must have a size that's a multiple of its required
alignment. Padding can exist within structures, but not between array
elements.

--
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
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      04-05-2008
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> wrote:

>>>> arrays are guaranteed to be contiguous with no padding before
>>>> or after any array member


Note the phrase "before or after".

>typedef struct elem {
> int i;
> char c;
>} elem;
>
>and, to me, each element of:
>
> elem array[MAX};
>
>will be padded accordingly.


As you say, each element will be padded. There will be no padding
*before or after* each element. The padding is *in* the elements.

-- Richard
--
:wq
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-05-2008
Richard Tobin wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>
>>>>> arrays are guaranteed to be contiguous with no padding before
>>>>> or after any array member

>
> Note the phrase "before or after".
>
>> typedef struct elem {
>> int i;
>> char c;
>> } elem;
>>
>> and, to me, each element of:
>>
>> elem array[MAX};
>>
>> will be padded accordingly.

>
> As you say, each element will be padded. There will be no padding
> *before or after* each element. The padding is *in* the elements.


Alright, I concede. However the array user has to allow for the
fact that there are padding items present.

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