padding in a statically initialized struct 
Author Message
 padding in a statically initialized struct

#include <stdio.h>

struct s {
  char c;
  int i;

Quote:
};

int main(void)
{
  struct s a, b;
  a.c = b.c = 'X';
  a.i = b.i = 42;
  printf("%d\n", memcmp(&a, &b, sizeof (struct s)));
  return 0;

Quote:
}

The above program is not guaranteed to print "0", because the structs
can contain random padding bytes and the int members can also contain
random unused bits.

If the structs are initialized statically, like this:

  static struct s a = { 'X', 42 };
  static struct s b = { 'X', 42 };

is memcmp(&a, &b, sizeof (struct s)) then guaranteed to return 0, or
can the compiler insert random padding anyway?

If the compiler can do that, can the following main() also produce
different output each time it is run?

static const struct s a = { 'X', 42 };
int main(void)
{
  fwrite(&a, sizeof a, 1, stdout);
  return 0;

Quote:
}

--



Sun, 02 Mar 2003 22:14:17 GMT  
 padding in a statically initialized struct

Quote:

>   static struct s a = { 'X', 42 };
>   static struct s b = { 'X', 42 };
> is memcmp(&a, &b, sizeof (struct s)) then guaranteed to return 0, or
> can the compiler insert random padding anyway?

The padding is indeterminate, so the memcmp can report unequal.

Quote:
> If the compiler can do that, can the following main() also produce
> different output each time it is run?
> static const struct s a = { 'X', 42 };
> int main(void)
> {
>   fwrite(&a, sizeof a, 1, stdout);
>   return 0;
> }

Sure; further note that you're writing random junk to a text
stream.
--



Mon, 03 Mar 2003 03:00:00 GMT  
 padding in a statically initialized struct


Quote:
>struct s {
>  char c;
>  int i;
>};

>If the structs are initialized statically, like this:

>  static struct s a = { 'X', 42 };
>  static struct s b = { 'X', 42 };

>is memcmp(&a, &b, sizeof (struct s)) then guaranteed to return 0, or
>can the compiler insert random padding anyway?

Random padding is permissible.

Quote:
>If the compiler can do that, can the following main() also produce
>different output each time it is run?

>static const struct s a = { 'X', 42 };
>int main(void)
>{
>  fwrite(&a, sizeof a, 1, stdout);
>  return 0;
>}

Yes, because you're writing the padding to stdout.  OTOH,

    fwrite( &a, 1, 1, stdout );

will always produce the same output, because the first element of a struct
is always at zero offset.

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list
--



Mon, 03 Mar 2003 03:00:00 GMT  
 padding in a statically initialized struct

wrote in comp.lang.c.moderated:

Quote:
> #include <stdio.h>

> struct s {
>   char c;
>   int i;
> };

> int main(void)
> {
>   struct s a, b;
>   a.c = b.c = 'X';
>   a.i = b.i = 42;
>   printf("%d\n", memcmp(&a, &b, sizeof (struct s)));
>   return 0;
> }

> The above program is not guaranteed to print "0", because the structs
> can contain random padding bytes and the int members can also contain
> random unused bits.

> If the structs are initialized statically, like this:

>   static struct s a = { 'X', 42 };
>   static struct s b = { 'X', 42 };

> is memcmp(&a, &b, sizeof (struct s)) then guaranteed to return 0, or
> can the compiler insert random padding anyway?

No, the compiler can still insert random padding, or merely leave the
value of any padding bytes uninitialized.

Quote:
> If the compiler can do that, can the following main() also produce
> different output each time it is run?

> static const struct s a = { 'X', 42 };
> int main(void)
> {
>   fwrite(&a, sizeof a, 1, stdout);
>   return 0;
> }

It can indeed produce a different output each time.  This is not only
true of structs but of any data value type that contains padding
bytes.  For example, some compilers for Intel x86 architecture return
12 for sizeof(long double) for better alignment even though only the
first 10 bytes are used to hold the Intel extended precision floating
point format.

Other than two unsigned chars, or arrays of unsigned char, there is no
guarantee that any two objects that compare equal via lvalues of the
appropriate types will be equal when compared by memcmp.  On the other
hand, two objects that are equal according to memcmp are required to
compare equal when tested via lvalues of the proper type.

Jack Klein
--
Home: http://jackklein.home.att.net
--



Mon, 03 Mar 2003 03:00:00 GMT  
 padding in a statically initialized struct

Quote:

> Other than two unsigned chars, or arrays of unsigned char, there is no
> guarantee that any two objects that compare equal via lvalues of the
> appropriate types will be equal when compared by memcmp.

Nonnegative signed chars are an exception because they have the
same representation as the corresponding unsigned char, right?

#include <string.h>
#include <stdlib.h>
int main(void)
{
  signed char a, b;
  a = 42;
  b = 42;
  return memcmp(&a, &b, 1)==0 ? 0 : EXIT_FAILURE;

Quote:
}

--



Sat, 08 Mar 2003 11:17:25 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Statically Initialized Two Dimensional Structure Problem

2. Initialize struct with struct

3. Padding in structs

4. padding a struct to a defined size

5. struct padding and sizeof

6. padding in structs..

7. padding in structs

8. sizeof(struct) and padding

9. Q: howto prevent struct padding

10. create a padded struct?

11. C++ 5.0 and sizeof (struct) / padding ?

12. preventing struct padding

 

 
Powered by phpBB® Forum Software