Copying from one struct to another, simple assignment?

R

Richard Bos

Eric Sosman said:
Keith said:
[...]
If we're not going to allow structs containing arrays to be compared
meaningfully, it should be a constraint violation.

Everything not compulsory is forbidden?

Trollings will continue until topicality improves.

Richard
 
C

CBFalconer

Eric said:
Keith said:
[...]
If we're not going to allow structs containing arrays to be
compared meaningfully, it should be a constraint violation.

Everything not compulsory is forbidden?

This is going to revolutionize society. :)
 
J

JosephKK

JosephKK said:
[...]
The real problem is that even if copying is done using (possibly an
internal equivalent of) memcpy(), it is quite possible to start out with
two different structs with different padding, and then assign identical
values to the members of those structs, potentially leaving only the
padding different. Any reasonable programmer would expect those two
structs to compare equal; and that requires that the comparison operator
ignores padding, and therefore must know where it is.
Of course, it's easily possible to write an implementation which works
around this by carefully keeping all padding bytes at, say, zero, at all
times. Requiring this of all implementations, though, defeats the
purpose of leaving the value of padding undefined.

Arrgh. Does the standard require struct assignment to work across
implementations?

The first time I read that sentence, I thought you were talking about
assigning a struct in one implementation to a struct in another
implementation; of course the standard doesn't require that. But to
answer the question I think you were asking, yes, the standard
requires each implementation to support struct assignment.

Sorry, perhaps "assignment between implementations" carries my intent
better?
Or maybe i am confusing a "C" attribute with an OS based
implementation requirement. Unless i am mistaken (again) static and
dynamic libraries can pass structures in the function arguments.
I thought that was fairly clear from the discussion so far, but ...

The C standard *could* have required equality comparison to work for
structs. The most straightforward way to define it would be that two
structs are equal if their corresponding members are equal; the rule
would be applied recursively for members that are structs. This
breaks down if any of the members are unions; probably comparison of
unions, or of structs containing unions, would be disallowed.
Agreed.

I can think of at least a couple of reasons assignment is required but
equality comparison isn't.

First, assignment is much simpler to implement; all you need is the
equivalent of memcpy(). Comparison would have to ignore any padding
bytes, would have to deal specially with bit fields, and might require
specialized code if pointer and/or floating-point comparison isn't
just bitwise comparison. If you compare two bitwise identical
structs, but they both contain a floating-point member whose current
value is a NaN, are they equal? At the time when struct assignment
was added to the language, struct comparison would have been a
substantial burden on compilers.

Possibly, if they have the same NaNs in the same places.
Second, struct comparison just isn't all that useful. When was the
last time you really needed to compare two structs for equality? And
even if you did, would the obvious member-by-member comparison really
do what you want? I think it's more common to need to ignore certain
members depending on the values of other members, or to do a deep
comparison that dereferences pointers (and determines whether to
compare just a single element or an array of some size that has to be
computed).

I have come across cases of struct comparison in tree balancing
algorithms and in map reduction algorithms. (But those were specific
structs and specific purposes, not a general case.)
.
 
K

Keith Thompson

JosephKK said:
Sorry, perhaps "assignment between implementations" carries my intent
better?

That depends on what your intent is, and I'm afraid I'm now more
confused about that than I was before.
Or maybe i am confusing a "C" attribute with an OS based
implementation requirement. Unless i am mistaken (again) static and
dynamic libraries can pass structures in the function arguments.

The standard doesn't mention static and dynamic libraries, and says
nothing about binary interoperability between different
implementations. You *might* be able to pass a struct argument from a
function compiled with one implementation to a function compiled with
another implementation, but that's entirely a result of cooperation
between the two implementations. Vendors for a given platform
probably have an interest in interoperability, but that's outside the
scope of the C standard.

[snip]
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top