By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

size of a typedef with arrays of unsigned char

P: n/a
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
Dec 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Dec 19 '06 #2

P: n/a
in fact, the size is affected by the different compiler.

"This is by no means guaranteed"
refer to Martin Ambuhl <ma*****@earthlink.net>
:)

ÓÚ 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
Dec 19 '06 #3

P: n/a
ÓÚ 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

Dec 19 '06 #4

P: n/a
Francois Grieu <fg****@gmail.comwrote:
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
Dec 19 '06 #5

P: n/a

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.

Dec 19 '06 #6

P: n/a
In article <11**********************@48g2000cwx.googlegroups. com>,
ma**********@pobox.com 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
Dec 19 '06 #7

P: n/a

Francois Grieu wrote:
In article <11**********************@48g2000cwx.googlegroups. com>,
ma**********@pobox.com 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

Dec 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.