sizeof unpadded struct size

G

George Orwell

Peter said:
int padded = sizeof (struct abc);
int unpadded = pp_struct_abc(pp_as_type_size, +);

printf(" padded: %d bytes\n", padded);
printf("unpadded: %d bytes\n", unpadded);
printf(" padding: %d bytes\n", padded - unpadded);

Please fix your code.

The sizeof operator returns a size_t.

You've been on this newsgroup for many years, so surely
you're aware by now of the *grave* importance that
Keith Thompson & Co. place on proper sizeof typing. Or
is it important for only newcomers to the group, and not
the clique's foot soldiers?

Only the other day, in the thread "size of a struct without
'sizeof'", Keith Thompson & Co. reminded us that, even though
the structure presented by a poster had very few members,
there could be problems because, I quote:

"... but even this will fail if the struct's size is greater
than INT_MAX, which could be as low as 32767. It would be
better to use a `size_t' variable."
-- Eric Sosman

"It's possible (if unlikely) that sizeof(struct whatever)
could exceed INT_MAX."
-- Keith Thompson

You've been warned, Peter! Now get fixing!

Yours,
Han from China

Il mittente di questo messaggio|The sender address of this
non corrisponde ad un utente |message is not related to a real
reale ma all'indirizzo fittizio|person but to a fake address of an
di un sistema anonimizzatore |anonymous system
Per maggiori informazioni |For more info
https://www.mixmaster.it
 
P

Peter Nilsson

George Orwell said:
Please fix your code.

If it is broken, then it is broken in the struct definition
which you snipped.

"... but even this will fail if the struct's size is
greater than INT_MAX, which could be as low as 32767.
 It would be better to use a `size_t' variable."
    -- Eric Sosman

"It's possible (if unlikely) that sizeof(struct
whatever) could exceed INT_MAX."
    -- Keith Thompson

You've been warned, Peter!

I've issued the warning myself. But I'm only responsible
for my own education. I'm not responsible for others.
Now get fixing!

There's nothing to fix. Rather, it's not possible to
fix the true problem.

Under C90, implementations are only required to support
one object of 32767 bytes in size. [And then, only in a
specific program of the implementation's choosing.] If
you create a struct larger than 32767 bytes, then you
have already crimped its portability.

You can't control how much padding an implementation will
add in a struct. Given the sample struct had two ints and
char, I have to gample on QoI that I have not exceeded the
size limit of the implementation.

True, not using size_t adds another gamble, but sometimes
I just roll the dice.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top