size of a typedef with arrays of unsigned char

F

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
 
P

pete

Francois said:
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; }
 
I

iamgodk

in fact, the size is affected by the different compiler.

"This is by no means guaranteed"
refer to Martin Ambuhl <[email protected]>
:)

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

iamgodk

ÓÚ Tue, 19 Dec 2006 08:55:20 +0000£¬peteдµ½£º
Francois said:
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
 
R

Richard Bos

Francois Grieu said:
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
 
M

mark_bluemel

Francois said:
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.
 
F

Francois Grieu

Francois Grieu wrote:

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
 
M

mark_bluemel

Francois said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top