473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Copying a struct to a larger struct?

I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?

Here's the small program I wrote to test this:

#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};

struct simplestruct foostruct;
struct extendedstruct barstruct;

/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");

/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));

/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;

/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}

Thanks in advance for any advice.
--
Karl Garrison
he********@yahoo.com

Nov 15 '05 #1
21 2394


he********@yahoo.com wrote:
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};


A lower maintainence, less error prone way to do this is
struct simplestruct { ... };
struct extendedstruct { struct simplestruct s; ... };

Nov 15 '05 #2
he********@yahoo.com wrote:
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?

Here's the small program I wrote to test this:

#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};

struct simplestruct foostruct;
struct extendedstruct barstruct;

/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");

/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));

/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;

/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}

Thanks in advance for any advice.


It is possible that sizeof foostruct == sizeof barstruct;
take another example:

struct simplestruct
{
int i;
char str[7];
};

struct extendedstruct
{
int i;
char str[7];
unsigned char j;
};

On typical implementations with 8bit char and 32bit or 16bit int and
minimal padding, it is now probable that sizeof foostruct == sizeof
barstruct, so your padding in simplestruct overwrites your useful
information in extendedstruct.
So, either you memcpy() (offsetof(struct simplestruct, str) + sizeof
foostruct.str) bytes (the offsetof macro comes from <stddef.h>) or
you follow the hint given in another reply, namely
struct extendedstruct
{
struct simplestruct mySimple;
unsigned char j;
}
Then, you do not even need memcpy():
barstruct.mySimple = foostruct;
suffices.
(In addition, you can abuse the address of barstruct as a
struct simplestruct*, if necessary -- but I have not said that ;-))
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
On 27 Jul 2005 12:32:08 -0700, "he********@yahoo.com"
<kg*******@pobox.com> wrote:
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?


There is no guarantee that the padding in the common part of the two
structs is the same. It is also possible for the shorter struct to
have padding at the end but in the longer struct there could be an
actual member at the same offset.
<<Remove the del for email>>
Nov 15 '05 #4
tedu wrote:
A lower maintainence [...]


to maintain, yes, but maintenance ;-)
Nov 15 '05 #5
"he********@yahoo.com" wrote:

I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has
extra members at the end). I wrote a small program to test this,
and it seems to work fine, but are there any cases where doing
something like this could cause any problems?


Why do snaky things in the first place? Just write down what you
want:

struct smaller {
....
} smallguy;

struct bigger {
struct smaller small;
....
} bigguy;

....

bigguy.small = smallguy;

and I find it hard to imagine what could go wrong.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #6
CBFalconer wrote
(in article <42***************@yahoo.com>):
Why do snaky things in the first place?


you are trying to implement an adder?

<duck>
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #7
"Barry Schwarz" <sc******@deloz.net> wrote in message
news:g7********************************@4ax.com...
On 27 Jul 2005 12:32:08 -0700, "he********@yahoo.com" .... There is no guarantee that the padding in the common part of the two
structs is the same.


If my memory serves me, the standard gurantees that. But, I'd prefer to turn
that common part into a type used inside both structs. memcpy() will be able
to do bad things silently...

Alex
Nov 15 '05 #8
On Fri, 29 Jul 2005 00:56:03 +0400, Alexei A. Frounze wrote:
"Barry Schwarz" <sc******@deloz.net> wrote in message
news:g7********************************@4ax.com...
On 27 Jul 2005 12:32:08 -0700, "he********@yahoo.com" ...
There is no guarantee that the padding in the common part of the two
structs is the same.


If my memory serves me, the standard gurantees that.


Perhaps you aren't using error correction on your memory. :)

Barry is right, the standard doesn't guarantee it. The alignment of
members within a struct is "implementation-defined" (I can't imagine why
in practice a compiler would not pad them identically, but it isn't
prohibited from padding differently).

The only guarantees about member positions in structs are that no hole
will occur at the beginning and that members will occupy increasing
storage addresses.

Padding also may be added at the end to ensure correct alignment for
arrays of the struct (but this is again at the implementation's discretion
- i.e. not of predictable / guaranteed / repeatable size).
But, I'd prefer to turn
that common part into a type used inside both structs. memcpy() will be able
to do bad things silently...


memcpy() may do bad things if the common part is not padded identically,
as well as if the smaller struct has padding at the end in space at which
the larger struct has a member.

You're right that your preferred solution is safe though.

Nov 15 '05 #9
Netocrat <ne******@dodo.com.au> writes:
On Fri, 29 Jul 2005 00:56:03 +0400, Alexei A. Frounze wrote:
"Barry Schwarz" <sc******@deloz.net> wrote in message
news:g7********************************@4ax.com...
On 27 Jul 2005 12:32:08 -0700, "he********@yahoo.com" ...
There is no guarantee that the padding in the common part of the two
structs is the same.


If my memory serves me, the standard gurantees that.


Perhaps you aren't using error correction on your memory. :)

Barry is right, the standard doesn't guarantee it. The alignment of
members within a struct is "implementation-defined" (I can't imagine why
in practice a compiler would not pad them identically, but it isn't
prohibited from padding differently).

The only guarantees about member positions in structs are that no hole
will occur at the beginning and that members will occupy increasing
storage addresses.


But there is an additional guarantee. C99 6.5.2.3p5 says:

One special guarantee is made in order to simplify the use of
unions: if a union contains several structures that share a common
initial sequence (see below), and if the union object currently
contains one of these structures, it is permitted to inspect the
common initial part of any of them anywhere that a declaration of
the complete type of the union is visible. Two structures share a
common initial sequence if corresponding members have compatible
types (and, for bit-fields, the same widths) for a sequence of one
or more initial members.

There's no explicit guarantee *unless* the two structures are members
of the same union, but it's hard to imagine an implementation other
than the DS9K meeting this requirement for union members without
meeting it for all structures with common initial sequences.
Padding also may be added at the end to ensure correct alignment for
arrays of the struct (but this is again at the implementation's discretion
- i.e. not of predictable / guaranteed / repeatable size).
But, I'd prefer to turn
that common part into a type used inside both structs. memcpy() will be able
to do bad things silently...


memcpy() may do bad things if the common part is not padded identically,
as well as if the smaller struct has padding at the end in space at which
the larger struct has a member.


Yes, that can be a problem even given the additional guarantee above.
For example, given:

struct foo {
int x;
char y;
}

struct foobar {
int x;
char y;
char z;
}

it's effectively guaranteed that the x and y members will have the
same size and offset in both structures, but the z member of struct
foobar could very well occupy some of the padding of struct foo.
You're right that your preferred solution is safe though.


Agreed.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
....
But there is an additional guarantee. C99 6.5.2.3p5 says:

One special guarantee is made in order to simplify the use of
unions: if a union contains several structures that share a common
initial sequence (see below), and if the union object currently
contains one of these structures, it is permitted to inspect the
common initial part of any of them anywhere that a declaration of
the complete type of the union is visible. Two structures share a
common initial sequence if corresponding members have compatible
types (and, for bit-fields, the same widths) for a sequence of one
or more initial members.

There's no explicit guarantee *unless* the two structures are members
of the same union, but it's hard to imagine an implementation other
than the DS9K meeting this requirement for union members without
meeting it for all structures with common initial sequences.
That was what I was referring to. It's really hard to imagine it would work
with unions only.
I think this would fail only if the compiler is such broken that it would
even treat diffrently the same structure/type definition from a .h header
file included by several different .c files. Say the files are: h.h, a.c and
b.c. Apparently, the compiler must not remember how it compiled a.c when
compiling b.c. But if it not just didn't remember but treated the contents
of h.h. differently in a.c and b.c, it would be useless for compiling any
code...
Yes, that can be a problem even given the additional guarantee above.
For example, given:

struct foo {
int x;
char y;
}

struct foobar {
int x;
char y;
char z;
}

it's effectively guaranteed that the x and y members will have the
same size and offset in both structures, but the z member of struct
foobar could very well occupy some of the padding of struct foo.


That's fine with z. But still, isn't what you're saying about x and y in
both structures enough to memcpy between them? Maybe it should be (I'm
writing pseudo code here):
memcpy (&foobar, &foo, offsetof(foo.y)+1)
instead of:
memcpy (&foobar, &foo, sizeof(foo))
?

Alex
Nov 15 '05 #11
Alexei A. Frounze wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
...
But there is an additional guarantee. C99 6.5.2.3p5 says:

One special guarantee is made in order to simplify the use of
unions: if a union contains several structures that share a common
initial sequence (see below), and if the union object currently
contains one of these structures, it is permitted to inspect the
common initial part of any of them anywhere that a declaration of
the complete type of the union is visible. Two structures share a
common initial sequence if corresponding members have compatible
types (and, for bit-fields, the same widths) for a sequence of one
or more initial members.

There's no explicit guarantee *unless* the two structures are members
of the same union, but it's hard to imagine an implementation other
than the DS9K meeting this requirement for union members without
meeting it for all structures with common initial sequences.

That was what I was referring to. It's really hard to imagine it would work
with unions only.
I think this would fail only if the compiler is such broken that it would
even treat diffrently the same structure/type definition from a .h header
file included by several different .c files. Say the files are: h.h, a.c and
b.c. Apparently, the compiler must not remember how it compiled a.c when
compiling b.c. But if it not just didn't remember but treated the contents
of h.h. differently in a.c and b.c, it would be useless for compiling any
code...

Yes, that can be a problem even given the additional guarantee above.
For example, given:

struct foo {
int x;
char y;
}

struct foobar {
int x;
char y;
char z;
}

it's effectively guaranteed that the x and y members will have the
same size and offset in both structures, but the z member of struct
foobar could very well occupy some of the padding of struct foo.


That's fine with z. But still, isn't what you're saying about x and y in
both structures enough to memcpy between them? Maybe it should be (I'm
writing pseudo code here):
memcpy (&foobar, &foo, offsetof(foo.y)+1)

offsetof(struct foo, y)
see my reply to the OP.
instead of:
memcpy (&foobar, &foo, sizeof(foo))
?


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #12
On Fri, 29 Jul 2005 09:13:05 +0400, Alexei A. Frounze
<al*****@chat.ru> wrote:
I think this would fail only if the compiler is such broken that it would
even treat diffrently the same structure/type definition from a .h header
file included by several different .c files. Say the files are: h.h, a.c and
b.c. Apparently, the compiler must not remember how it compiled a.c when
compiling b.c. But if it not just didn't remember but treated the contents
of h.h. differently in a.c and b.c, it would be useless for compiling any
code...


No, that's not necessarily broken. In fact it can happen easily if the
compiler is invoked with different options for each file (easy to do in
some build systems, or if some of the code is a separate library).

However, provided you haven't done something silly like that (which will
break all structures passed between the translation units) you are
right. If something is in a header file in one translation unit then it
must be compiled the same in all other translation units otherwise the
system is broken.
Yes, that can be a problem even given the additional guarantee above.
For example, given:

struct foo {
int x;
char y;
}

struct foobar {
int x;
char y;
char z;
}

it's effectively guaranteed that the x and y members will have the
same size and offset in both structures, but the z member of struct
foobar could very well occupy some of the padding of struct foo.


That's fine with z. But still, isn't what you're saying about x and y in
both structures enough to memcpy between them? Maybe it should be (I'm
writing pseudo code here):
memcpy (&foobar, &foo, offsetof(foo.y)+1)
instead of:
memcpy (&foobar, &foo, sizeof(foo))
?


Yes, you could, although the +1 should be +sizeof(foo.y) to be general
(in your case y is a char, but if it were anything else then it could be
a different size). I would prefer (in C99 supporting compilers) to use
an extra variable, preferably zero length:

struct Common
{
int x;
char y;
/*...*/
char endOfCommon[];
};

struct A
{
int x;
char y;
/*...*/
};

etc. And then do
...
memcpy(&a, &b, offsetof(struct Common, endOfCommon));

If your compiler won't take that syntax then make endOfCommon a char (it
won't matter anyway since you are just using it as a marker and won't
actually declare a variable of type struct Common so it won't take up
any real space). That is effectively the "struct hack", you would just
be doing it with declared variables rather than the more common malloc'd
memory. I would bury the memcpy inside a macro or function to avoit
people mistyping it, COPY_COMMON(a, b) for instance. I would also be
tempted to put the common declarations into a macro so that I always got
those right, so it would come out something like:

#define COMMON int x; char y; /*...*/

struct COMMON_s
{
COMMON
char end_of_COMMON;
};

#define COPY_COMMON(a,b) \
memcpy(a, b, offsetof(struct COMMON_s, end_of_COMMON))

/*...*/

struct A
{
COMMON
int astuff;
};

struct B
{
COMMON
void *bstuff;
};

/*...*/

void funct(struct A *aPar)
{
struct B bbb;
COPY_COMMON(&b, a);
/*...*/
}

Yes, it might fail on a DS9000 which was deliberately trying to break
it, but I can't believe that any real compiler would do it (not least
because so much real world code would break, anyone using such a
compiler would throw it back on Quality of Implementation grounds, the
DS9000 is not a useful production compiler).

Chris C
Nov 15 '05 #13
On Fri, 29 Jul 2005 08:45:05 +0100, Chris Croughton wrote:
On Fri, 29 Jul 2005 09:13:05 +0400, Alexei A. Frounze
<al*****@chat.ru> wrote:
I think this would fail only if the compiler is such broken that it
would even treat diffrently the same structure/type definition from a
.h header file included by several different .c files. Say the files
are: h.h, a.c and b.c. Apparently, the compiler must not remember how
it compiled a.c when compiling b.c. But if it not just didn't remember
but treated the contents of h.h. differently in a.c and b.c, it would
be useless for compiling any code...
No, that's not necessarily broken. In fact it can happen easily if the
compiler is invoked with different options for each file (easy to do in
some build systems, or if some of the code is a separate library).


Only if those options don't include invoking standards-compliant mode or
we're being really pedantic...

As Keith pointed out the standard provides a virtual guarantee that the
common members must be padded in the same way - it's not an actual
guarantee but a compiler writer would have to go out of their way to meet
the requirements for structs with common initial members to be padded
identically when part of a union but not in the general case.
However, provided you haven't done something silly like that (which will
break all structures passed between the translation units) you are
right. If something is in a header file in one translation unit then it
must be compiled the same in all other translation units otherwise the
system is broken.
Yes, that can be a problem even given the additional guarantee above.
For example, given:

struct foo {
int x;
char y;
}

struct foobar {
int x;
char y;
char z;
}

it's effectively guaranteed that the x and y members will have the
same size and offset in both structures, but the z member of struct
foobar could very well occupy some of the padding of struct foo.


That's fine with z. But still, isn't what you're saying about x and y
in both structures enough to memcpy between them? Maybe it should be
(I'm writing pseudo code here):
memcpy (&foobar, &foo, offsetof(foo.y)+1)
instead of:
memcpy (&foobar, &foo, sizeof(foo))
?


Yes, you could, although the +1 should be +sizeof(foo.y) to be general
(in your case y is a char, but if it were anything else then it could be
a different size). I would prefer (in C99 supporting compilers) to use
an extra variable, preferably zero length:

struct Common
{
int x;
char y;
/*...*/
char endOfCommon[];
};

struct A
{
int x;
char y;
/*...*/
};

etc. And then do
...
memcpy(&a, &b, offsetof(struct Common, endOfCommon));


You're forgetting something here...

What if there's padding added before endOfCommon?

<snip rest of example>
Nov 15 '05 #14
On Fri, 29 Jul 2005 18:36:38 +1000, Netocrat
<ne******@dodo.com.au> wrote:
On Fri, 29 Jul 2005 08:45:05 +0100, Chris Croughton wrote:
On Fri, 29 Jul 2005 09:13:05 +0400, Alexei A. Frounze
<al*****@chat.ru> wrote:
I think this would fail only if the compiler is such broken that it
would even treat diffrently the same structure/type definition from a
.h header file included by several different .c files. Say the files
are: h.h, a.c and b.c. Apparently, the compiler must not remember how
it compiled a.c when compiling b.c. But if it not just didn't remember
but treated the contents of h.h. differently in a.c and b.c, it would
be useless for compiling any code...
No, that's not necessarily broken. In fact it can happen easily if the
compiler is invoked with different options for each file (easy to do in
some build systems, or if some of the code is a separate library).


Only if those options don't include invoking standards-compliant mode or
we're being really pedantic...


No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.
As Keith pointed out the standard provides a virtual guarantee that the
common members must be padded in the same way - it's not an actual
guarantee but a compiler writer would have to go out of their way to meet
the requirements for structs with common initial members to be padded
identically when part of a union but not in the general case.


And what about the case when they are accessed via a pointer to the
union? Yugh. I wouldn't want to try to writte such a compiler...
Yes, you could, although the +1 should be +sizeof(foo.y) to be general
(in your case y is a char, but if it were anything else then it could be
a different size). I would prefer (in C99 supporting compilers) to use
an extra variable, preferably zero length:

struct Common
{
int x;
char y;
/*...*/
char endOfCommon[];
};

struct A
{
int x;
char y;
/*...*/
};

etc. And then do
...
memcpy(&a, &b, offsetof(struct Common, endOfCommon));


You're forgetting something here...

What if there's padding added before endOfCommon?


Hmm, you mean if the compiler puts more padding before a char than
before an int, say? Is it allowed to do that, since a char is by
definition the smallest type? Is the padding allowed to be more than
that needed to satisfy the alignment? I think that comes back to the
Quality of Implementation argument, a compiler which adds more padding
than it needs won't last long in the wild. Think of it as evolution in
action...

Chris C
Nov 15 '05 #15


Chris Croughton wrote:

No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.


Saw that happen once, in a vendor-supplied header
no less. Paraphrasing from dim memory:

#pragma push(_struct_packing_mode)
#define _struct_packing_mode 1
struct something { ... };
#pragma pop(struct_packing_mode)

The effect was that `struct something' was laid out
consistently and correctly in all translation units,
but that other structs were laid out inconsistently
depending on whether their declarations came before
or after the header that contained this sequence.

--
Er*********@sun.com

Nov 15 '05 #16
On Fri, 29 Jul 2005 10:33:22 -0400, Eric Sosman
<er*********@sun.com> wrote:
Chris Croughton wrote:

No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.
Saw that happen once, in a vendor-supplied header
no less. Paraphrasing from dim memory:

#pragma push(_struct_packing_mode)
#define _struct_packing_mode 1
struct something { ... };
#pragma pop(struct_packing_mode)


Let me guess, a Richmond WA based company's media APIs? That's where
I've found similar...
The effect was that `struct something' was laid out
consistently and correctly in all translation units,
but that other structs were laid out inconsistently
depending on whether their declarations came before
or after the header that contained this sequence.


And inconsistent from ones which didn't include the header at all. Yup.
Easily done with implementation options. Borland's compilers did it a
lot, because you could (still can) turn 'command line' parameters on an
off within source files and it's very easy to forget to restore the
default in a header...

GCC's method of putting 'attributes' only on the specific object is a
lot more manageable in that way (still non-standard, but at least it
rarely breaks things outside the specific cases where it's used).

Chris C
Nov 15 '05 #17
Eric Sosman <er*********@sun.com> writes:
#pragma push(_struct_packing_mode)
#define _struct_packing_mode 1
struct something { ... };
#pragma pop(struct_packing_mode)

The effect was that `struct something' was laid out
consistently and correctly in all translation units,
but that other structs were laid out inconsistently
depending on whether their declarations came before
or after the header that contained this sequence.


I've seen this problem avoided by putting the needed prologue and
epilogue in separate header files and then #include'ing them
instead of putting them inline. If you screw up the spelling of
a header file name, you get an error message.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 15 '05 #18

In article <sl******************@ccserver.keris.net>, Chris Croughton <ch***@keristor.net> writes:
On Fri, 29 Jul 2005 18:36:38 +1000, Netocrat
<ne******@dodo.com.au> wrote:
On Fri, 29 Jul 2005 08:45:05 +0100, Chris Croughton wrote:
No, that's not necessarily broken. In fact it can happen easily if the
compiler is invoked with different options for each file (easy to do in
some build systems, or if some of the code is a separate library).


Only if those options don't include invoking standards-compliant mode or
we're being really pedantic...


No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.


Compiling two source files with two different sets of options is,
by definition, compiling them with two different implementations:

[#1] implementation
a particular set of software, running in a particular
translation environment under particular control options
(n869 3.10)

Since structure padding is implementation-defined, the situation you
invoke is moot. When compiling two source files using the same
implementation, common initial members of structure types *must* use
the same padding unless the implementation can determine that they
never appear together in a union. (If you did ever run into such a
perverse implementation, you could easily create such a union to
force this behavior, of course.)

When compiling under two different implementations, all bets are off;
the clause does not apply.

--
Michael Wojcik mi************@microfocus.com

The presence of those seeking the truth is infinitely preferable to
the presence of those who think they've found it. -- Terry Pratchett
Nov 15 '05 #19


Chris Croughton wrote:
On Fri, 29 Jul 2005 10:33:22 -0400, Eric Sosman
<er*********@sun.com> wrote:

Chris Croughton wrote:
No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.


Saw that happen once, in a vendor-supplied header
no less. Paraphrasing from dim memory:

#pragma push(_struct_packing_mode)
#define _struct_packing_mode 1
struct something { ... };
#pragma pop(struct_packing_mode)

Let me guess, a Richmond WA based company's media APIs? That's where
I've found similar...


No; the header that fouled up my project came from a
company based in Maynard MA.

That company no longer exists, and neither does the
company that bought their remnants -- and the company that
in turn bought the buyer has fired its CEO and is laying
off more than fourteen thousand other employees. The
consequences of Undefined Behavior truly can be dire ...

--
Er*********@sun.com

Nov 15 '05 #20
Eric Sosman <er*********@sun.com> writes:
No; the header that fouled up my project came from a
company based in Maynard MA.

That company no longer exists, and neither does the
company that bought their remnants -- and the company that
in turn bought the buyer has fired its CEO and is laying
off more than fourteen thousand other employees. The
consequences of Undefined Behavior truly can be dire ...


And yet, Stanford's computer science building still contains a
"Digital Equipment Corporation Library" on the 2nd floor. We use
it as a grad student lounge these days though.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 15 '05 #21
On Fri, 29 Jul 2005 14:56:15 -0400, Eric Sosman
<er*********@sun.com> wrote:
Chris Croughton wrote:
On Fri, 29 Jul 2005 10:33:22 -0400, Eric Sosman
<er*********@sun.com> wrote:
Chris Croughton wrote:

No, the options can be perfectly standards compliant but only if they
are used consistently. An option which says that structures are packed,
for instance, would be perfectly compliant unless you missed it off
somewhere. But that would cause all structures passed from one to the
other to fail.

Saw that happen once, in a vendor-supplied header
no less. Paraphrasing from dim memory:

#pragma push(_struct_packing_mode)
#define _struct_packing_mode 1
struct something { ... };
#pragma pop(struct_packing_mode)
Let me guess, a Richmond WA based company's media APIs? That's where
I've found similar...


No; the header that fouled up my project came from a
company based in Maynard MA.


Ah, that one. Another is in Cupertino CA (but using the OS from the other one
I mentioned)...
That company no longer exists, and neither does the
company that bought their remnants -- and the company that
in turn bought the buyer has fired its CEO and is laying
off more than fourteen thousand other employees. The
consequences of Undefined Behavior truly can be dire ...


Think of it as evolution in action...

Chris C
Nov 15 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Xavier Noria | last post by:
Given two structures of the same size but different type, does C99 guarantee that pointers to them can be casted one to each other, and that the order of the elements will be kind of respected? ...
5
by: Victor Bazarov | last post by:
Below you will find some code I wrote to see if I could wrap array copying (especially for multi-dimensional arrays) in a simple class and/or function. It seems to work fine for one-dimensioned...
6
by: Johs32 | last post by:
I have made a pcb pointer called current. I have then made another pointer (old_thread) that I initialize to point to the same area that current points to. If I make changes to current...
23
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about...
10
by: webfan | last post by:
In the code below, will the assignment z= y do bitwise copying? struct x { int a; int b; int c; }; main()
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.