Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of a typedef with arrays of unsigned char

Reply
Thread Tools

size of a typedef with arrays of unsigned char

 
 
Francois Grieu
Guest
Posts: n/a
 
      12-19-2006
Hello,

is the size of a type, defined using typedef as a collection of
arrays of unsigned char, the sum of the size of the arrays ?

After simplifying my question, it probably is equivalent to:
does the following program terminates ?

int main(void) {
typedef struct t { unsigned char f[1]; } t;
for(;1!=sizeof t;
return 0; }


TIA,

Francois Grieu
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-19-2006
Francois Grieu wrote:
>
> Hello,
>
> is the size of a type, defined using typedef as a collection of
> arrays of unsigned char, the sum of the size of the arrays ?
>
> After simplifying my question, it probably is equivalent to:
> does the following program terminates ?


The code example depends on a property of structures
called padding bytes.
sizeof t could be equal to 1,
or sizeof t could be larger than 1.

> int main(void) {
> typedef struct t { unsigned char f[1]; } t;
> for(;1!=sizeof t;
> return 0; }


--
pete
 
Reply With Quote
 
 
 
 
iamgodk
Guest
Posts: n/a
 
      12-19-2006
in fact, the size is affected by the different compiler.

"This is by no means guaranteed"
refer to Martin Ambuhl <(E-Mail Removed)>


ÓÚ Tue, 19 Dec 2006 09:42:21 +0100£¬Francois Grieuдµ½£º

> Hello,
>
> is the size of a type, defined using typedef as a collection of
> arrays of unsigned char, the sum of the size of the arrays ?
>
> After simplifying my question, it probably is equivalent to:
> does the following program terminates ?
>
> int main(void) {
> typedef struct t { unsigned char f[1]; } t;
> for(;1!=sizeof t;
> return 0; }
>
>
> TIA,
>
> Francois Grieu


 
Reply With Quote
 
iamgodk
Guest
Posts: n/a
 
      12-19-2006
ÓÚ Tue, 19 Dec 2006 08:55:20 +0000£¬peteдµ½£º

> Francois Grieu wrote:
>>
>> Hello,
>>
>> is the size of a type, defined using typedef as a collection of
>> arrays of unsigned char, the sum of the size of the arrays ?
>>
>> After simplifying my question, it probably is equivalent to:
>> does the following program terminates ?

>
> The code example depends on a property of structures
> called padding bytes.
> sizeof t could be equal to 1,
> or sizeof t could be larger than 1.
>
>> int main(void) {
>> typedef struct t { unsigned char f[1]; } t;
>> for(;1!=sizeof t;
>> return 0; }

sometimes the number of the padding bytes can be set by compiler parameter

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      12-19-2006
Francois Grieu <(E-Mail Removed)> wrote:

> is the size of a type, defined using typedef as a collection of
> arrays of unsigned char, the sum of the size of the arrays ?


Not necessarily.

> After simplifying my question, it probably is equivalent to:
> does the following program terminates ?
>
> int main(void) {
> typedef struct t { unsigned char f[1]; } t;
> for(;1!=sizeof t;
> return 0; }


What horrible style. Whitespace is nearly free, you know.

But no, that may, and often will, but need not, terminate.

Richard
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      12-19-2006

Francois Grieu wrote:
> Hello,
>
> is the size of a type, defined using typedef as a collection of
> arrays of unsigned char, the sum of the size of the arrays ?


The reference to typedef is irrelevant. You are actually interested in
the size of structs.

The answer to your question is no. This may be so in some cases, but in
general, padding may be added to make the struct "conveniently" sized.

For example :-

struct s {
int a;
char b[2];
long c;
};

On a system with 8-bit chars, 32-bit ints and 64-bit longs, the
structure could have sizeof = 16, as 2 bytes padding would be inserted
(after the char array b) to align the long on an 8-byte boundary.

On the machines I most commonly work with, all structs are aligned at
8-byte boundarys, so even a struct containing a single "char" would
have sizeof = 8.

 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      12-19-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Francois Grieu wrote:


> > is the size of a type, defined using typedef as a collection of
> > arrays of unsigned char, the sum of the size of the arrays ?

>
> The reference to typedef is irrelevant. You are actually interested in
> the size of structs.


Right.


> The answer to your question is no.


OK, thanks.

Now, if I can't use a struct for the purpose of naming fields
in a record consisting of arbitrary byte-sized chunks, does the
following work for this purpose (in particular, is is certain
that sizeof(vMyRec) is 7*15), and can anyone suggest a better
idiom ?


enum
{
myRecSize = 15,
fField2 = myRecSize-7, // fField2, 7 bytes,
fField1 = fField2-5, // fField1, 5 bytes,
fField0 = fField1-3, // fField0, 3 bytes,
myRecSizeOK = 1/(fField0==0) // check that things add up
};
typedef unsigned char tMyRec[myRecSize];

tMyRec vMyRec[7]; // memory image of file consisting of 7 records

#include <stdio.h>
int main(void)
{
FILE *f;
if ((f = fopen("testfile","rb"))!=NULL
&& fread(vMyRec,sizeof(*vMyRec),sizeof(vMyRec)/sizeof(*vMyRec),f)==
sizeof(vMyRec)/sizeof(*vMyRec))
printf("Fourth byte of second field of seventh record is 0x%02X\n",
vMyRec[6][fField1+3]);
return 0;
}


TIA,

Francois Grieu
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      12-21-2006

Francois Grieu wrote:
> In article <(E-Mail Removed). com>,
> (E-Mail Removed) wrote:
>
> > Francois Grieu wrote:

>
> > > is the size of a type, defined using typedef as a collection of
> > > arrays of unsigned char, the sum of the size of the arrays ?

> >
> > The reference to typedef is irrelevant. You are actually interested in
> > the size of structs.

>
> Right.
>
>
> > The answer to your question is no.

>
> OK, thanks.
>
> Now, if I can't use a struct for the purpose of naming fields
> in a record consisting of arbitrary byte-sized chunks, does the
> following work for this purpose (in particular, is is certain
> that sizeof(vMyRec) is 7*15), and can anyone suggest a better
> idiom ?

FAQ 2.11 is what you want, I think - http://c-faq.com/struct/io.html

 
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
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments