Returning a struct from a function - strange behavior

L

lovecreatesbeauty

the behavior is undefined. But when you assign the result of
make_person() to the object p, the expression p.name *is* an lvalue,
the conversion does occur, and everything works.

It's me but not the op, remember :)
Your best bet, if you're actually
trying to get some work done, is to avoid the issue and use an
explicit temporary.

This piece of code of mine already applied your suggestion, didn't it?

int main(void)
{
Person p;

p = make_person();
printf("%s\n", p.name);

/* printf("%s\n", make_person().name); */

return 0;
}
 
J

jacob navia

James said:
That's not my logic. My logic was that the low priority you've attached
to standard conformance in the past

[snip]

"the low priority I have attached to standard conformance"
has led me to work something like 5 years to implement C99.

A continuous work that led me to implement the whole C library,
all the syntax changes necessary, all the tests, correct all the
bug reports, and work year after year to implement standard
C.

And after all this effort I have to hear from somebody

"... the low priority you attach to standard conformance"
 
J

jacob navia

No, it doesn't. It *mostly* conforms, if you like, but until you
implement *all* of the requirements, not just the "important" ones, it
does not (fully) conform.

I do not care about your opinion.

That is blatantly untrue. There are obscure C89 constructs that it
miscompiles because it interprets them as containing C99 constructs.
Also...

Ditto. If you use C99 constructs in your code it will accept
them. Pedants notwithstanding.

That's not a "problem", it's a *problem*. C89 *requires* diagnostics in
certain circumstances. If your compiler doesn't emit those diagnostics,
then it does *not* fully conform. You can say it *mostly* conforms if
you like, but please stop lying to people.


Sure, I am lying etc...

Ditto: I do not care about your opinion.
 
J

jameskuyper

jacob said:
James said:
That's not my logic. My logic was that the low priority you've attached
to standard conformance in the past

[snip]

"the low priority I have attached to standard conformance"
has led me to work something like 5 years to implement C99.

A continuous work that led me to implement the whole C library,
all the syntax changes necessary, all the tests, correct all the
bug reports, and work year after year to implement standard
C.

And after all this effort I have to hear from somebody

"... the low priority you attach to standard conformance"

You haven't finished the work yet. You've finished work on numerous
extensions before finishing work on the base language. That implies
that you gave ALL of those extensions a higher priority than full
conformance; by my standards, that counts as a low priority. I'm not
criticizing your priorities; they're not my priorities, so I would
have no use for your compiler, but it's presumably considered a useful
compiler by those who share your priorities. If that market's as big
as you think, you should have a profitable product, and I see nothing
wrong with you collecting those profits. However, please don't deny
that you have prioritized your extensions over full conformance.

I notice that you did not address the truth (or lack thereof) of my
guesses as to why you implemented this particular feature.
 
J

jameskuyper

jacob said:
I do not care about your opinion.

That it doesn't conform isn't a matter of opinion, it's a matter of
fact. It's an opinion whether or not that lack of conformance is
important, and you're entitled to the opinion that it's unimportant.
That opinion will make it impossible for you to sell or even give away
your product to anyone like me who actively relies upon that
particular aspect of conformance, but then I'm obviously not an
example of the target audience for your product.

....
Sure, I am lying etc...

Ditto: I do not care about your opinion.

Nonetheless, a paying customer who actually needs C90 conformance
could sue you for misrepresentation if you claimed it, and because of
the absence of these mandatory diagnostics, such a customer would have
a valid case.
I'm thinking in terms of American law; I'm not sure if this would be
case in France. The complaint would still be valid, but I don't know
how easy it would be to file a lawsuit based upon it.
 
V

vippstar

jacob navia wrote:

[ about jacob claiming conformance ]
Nonetheless, a paying customer who actually needs C90 conformance
could sue you for misrepresentation if you claimed it, and because of
the absence of these mandatory diagnostics, such a customer would have
a valid case.
I'm thinking in terms of American law; I'm not sure if this would be
case in France. The complaint would still be valid, but I don't know
how easy it would be to file a lawsuit based upon it.

I don't think such case would have any luck. Jacob could just claim
it's a bug he wasn't aware of. Else everyone would be able to sue any
buggy software and get money.
 
A

Antoninus Twink

I don't think such case would have any luck. Jacob could just claim
it's a bug he wasn't aware of.

Hardly necessary - the courts would probably be a lot less legalistic
then the average clc Heathfield-wannabe. The law has the notion of a
"reasonable person", which is a foreign concept to most clc'ers.
 
K

Keith Thompson

jacob navia wrote:

[ about jacob claiming conformance ]
Nonetheless, a paying customer who actually needs C90 conformance
could sue you for misrepresentation if you claimed it, and because of
the absence of these mandatory diagnostics, such a customer would have
a valid case.
I'm thinking in terms of American law; I'm not sure if this would be
case in France. The complaint would still be valid, but I don't know
how easy it would be to file a lawsuit based upon it.

I don't think such case would have any luck. Jacob could just claim
it's a bug he wasn't aware of. Else everyone would be able to sue any
buggy software and get money.

He'd have to claim that somebody else has been posting to Usenet under
his name. He's publicly acknowledged here that lcc-win doesn't
produce diagnostics that are required for C90 conformance. I don't
think he's actually acknowledged that those diagnostics are required,
but that's a matter of fact that can be inferred by reading the
standard.
 
H

Harald van Dijk

I *think* that the behavior is undefined in both C90 and C99 (but
becomes well defined in C201X).

No, support for non-lvalue arrays was added in C99.

It's been pointed out to me in e-mail that the original code passes the
array to printf which then attempts to access it after the next
sequence point (which would be the one right before the call), so it
actually *is* undefined behavior in C99, despite the support for
non-lvalue arrays. [...]

Where does C99 say that the array may be accessed *before* the next
sequence point?

6.2.4p1:
"An object has a storage duration that determines its lifetime. There are
three storage durations: static, automatic, and allocated. [...]"

There are specific exceptions for accessing temporary objects after the
next sequence point. There is no such exception for accessing them before,
so you are allowed to access them until the end of their lifetime, which
is later for all three possible storage durations.
 
K

Keith Thompson

Keith Thompson said:
Where does C99 say that the array may be accessed *before* the next
sequence point?

I got an e-mail response to this:

| 6.5.2.2 P5: "If an attempt is made to modify the result of a function
| call or to access it after the next sequence point, the behavior
| is undefined."
|
| Presumably, this sentence was added to C99 specifically to address
| the case of an function returning a struct/union containing an
| array. Unfortunately, the lifetime isn't quite long enough to
| cover the case in question in this thread, and the existence of
| the necessary temporary object is implied but not stated; hence
| the clarification in C1x.
|
| [Unfortunately, I am currently unable to post to Usenet; hence
| the email.]
|
| Philip Lantz

As Philip says, the real problem in C99 is that it assumes the
existence of this object, but doesn't actually say that it exists.
 
J

jacob navia

jacob said:
James said:
jacob navia wrote:
Obviously if I have something correct it can ONLY be a coincidence,
since I am unable to produce anything correct since I am a jerk
by definition.

Quite a clear logic isn't it?
That's not my logic. My logic was that the low priority you've attached
to standard conformance in the past
[snip]

"the low priority I have attached to standard conformance"
has led me to work something like 5 years to implement C99.

A continuous work that led me to implement the whole C library,
all the syntax changes necessary, all the tests, correct all the
bug reports, and work year after year to implement standard
C.

And after all this effort I have to hear from somebody

"... the low priority you attach to standard conformance"

You haven't finished the work yet.

Lcc-win is the work of just two people, Friedrich and me.
Compared to the huge teams of gcc, not to speak about the
commercial compilers, it is the smallest team that has
ever undertaken such a huge project.

We have finished implementing the full set of core features,
and the ones that are missing are changes in the preprocessor
that I started some years ago but I haven'et got the time to
finish yet.

You've finished work on numerous
extensions before finishing work on the base language. That implies
that you gave ALL of those extensions a higher priority than full
conformance; by my standards, that counts as a low priority.

You do not give a dam about my work if it is not to denigrate it,
together with your friends in this group. Now you are telling me what I
should and should not do in my free time. You have never helped
the project with a single contribution besides your eternal
complaing about "full conformance" etc.

The fact that I do not reject // comments is taken as a serious bug,
obviously other features like 105 digits precision in the 32 bit edition
are just "extensions that are worth nothing", since for you things like
usability, the existence of a debugger, speed of compilation, etc etc
are just nothing.
 
D

DiAvOl

I think that the last 20-30 posts (or more) has nothing to do with the
subject of this thread, maybe creating a new subject for lcc-win32 and
post there would be a better idea

Just a thought
 
V

vippstar

I think that the last 20-30 posts (or more) has nothing to do with the
subject of this thread, maybe creating a new subject for lcc-win32 and
post there would be a better idea

Just a thought


Actually that's just how usenet works, it's not a web forum.
Many of the interesting discussions here in comp.lang.c don't have
anything to do with the subject line, or the OP question.
 
J

jacob navia

DiAvOl said:
I think that the last 20-30 posts (or more) has nothing to do with the
subject of this thread, maybe creating a new subject for lcc-win32 and
post there would be a better idea

Just a thought

When I said that lcc-win compiles and executes that
code correctly they could not stand that.

Denigrating lcc-win is the only thing they all agree with.

Why?

It is the only compiler that is not a C++ compiler that
happens to compile C.

It has several enhancements and innovations that make
this people scream like mad dogs:
o Operator overloading
o generic functions
o default arguments


The prototype figure for them is CB Falconer.

I agree with you. It would be better if I just ignore
those. Normally I stopped posting here.
 
J

jameskuyper

jacob said:
Lcc-win is the work of just two people, Friedrich and me.

I wasn't talking about how fast you finished your tasks; I was talking
about the order in which you finished them. Having only two people
does not prevent you from working on full conformance first, and
extensions later. That was your choice, and it indicates where your
priorities lie.
You do not give a dam about my work if it is not to denigrate it,

Note: that's 'damn', not 'dam'. That is NOT a criticism, my French is
far worse than your English; I just thought you should know. Swearing
like that looses a lot of it's intended impact when you misspell the
words.

I don't want to denigrate your work; I'd be happy to have legitimate
reason to praise it.
together with your friends in this group. Now you are telling me what I
should and should not do in my free time.

I said nothing of the kind. I said very specifically that you have the
right to assign whatever priorities you want to your tasks. I'm just
saying that you clearly didn't assigned a high priority to full
conformance if you've finished a whole lot of extensions first.
... You have never helped
the project with a single contribution besides your eternal
complaing about "full conformance" etc.

You're right - it's your project, not mine, I have no interest in
contributing. For precisely that reason, if you had never described it
as conforming, I'd have never had reason or interest in pointing out
it's non-conformance.
The fact that I do not reject // comments is taken as a serious bug,

Yes, it is. I am contractually obligated to deliver code that will
compile on any fully conforming C90 compiler that also supports POSIX.
If I were to use your compiler to develop my code, and accidentally
made use of C99 extensions such as // comments, I wouldn't get any
error messages. If I delivered that code to our client and our client
couldn't get it to compile on their C90 compiler, we would at the very
least be seriously embarrassed; I won't claim that we would lose our
contract, or even that I would be fired over the issue. However, I do
work for a NASA contractor. Budget cuts, corresponding layoffs, and
recompeted contracts are the norm, not the exception. When they decide
whether to renew our contract or give it to someone else, when they
decide who to layoff, I'd prefer that they not have an easily
avoidable compilation failure of delivered code to influence their
decisions.
obviously other features like 105 digits precision in the 32 bit edition
are just "extensions that are worth nothing", ...

For me, that's a completely useless extension. For other people, with
different needs, it might be pretty important.
... since for you things like
usability, the existence of a debugger, speed of compilation, etc etc
are just nothing.

I never said that they were nothing. They're all important issues. But
for me, full conformance is a top priority, those other things are
less important. The existence of fully conforming C90 compilers with a
good debugger, acceptable usability and acceptable speed for the
platforms of interest to me makes your compiler correspondingly less
interesting. The fact that those compilers almost fully conform to C99
gives me something to look forward to should our next contract allow
use of C99. lcc-win32 could beat them out if your linux version
achieves full C99 conformance before gcc's does.
 
D

DiAvOl

Actually that's just how usenet works, it's not a web forum.
Many of the interesting discussions here in comp.lang.c don't have
anything to do with the subject line, or the OP question.

so you find this discussion interesting? what exactly is interesting
about it?

I am relatively new here and all I see is some people shooting another
(jacob navia in this case)
(Also I saw many interesting replies from some interesting programmers
of course)

For me lcc-win32 is another tool I can use if I want to (to test some
code probably), if I don't like it I won't use it

Anyway thanks for (the few) interesting replies, this is my last post
for this subject I guess
 
C

CBFalconer

Nate said:
.... snip ...

You are correct in that it needs to allocate this space even if
the return value is going to be ignored. But again, this is
already the case. As I understand it, the only difference
between the current state of affairs and what's proposed for C1x
is how long the compiler has to keep the return value intact
before it can write over it.

Are you proposing to do away entirely with the ability to return
structs from C functions? Given the problems that arise, one
might argue that adding them to C was ill-considered, but I think
it's too late to take them back now.

Don't think about the complex returned values. They already need
special handling (after all, they were even forbidden to be
returned in early C). Consider the return value from, say,
strlen. This is of type size_t, which will be some form of
integer. It is normally returned in a register, and is a value.
What if you write:

&strlen(thing);

in your code somewhere. What are you taking the address of? Why?
Why did you want to force saving all such returns in a memory
location (and possibly letting the optimizer take them all out
again)? If you really want this you can write:

thinglen = strlen(thing);
use(&thinglen);
 
C

CBFalconer

Harald said:
Keith Thompson wrote:
.... snip ...
Where does C99 say that the array may be accessed *before* the
next sequence point?

6.2.4p1:
"An object has a storage duration that determines its lifetime.
There are three storage durations: static, automatic, and
allocated. [...]"

There are specific exceptions for accessing temporary objects
after the next sequence point. There is no such exception for
accessing them before, so you are allowed to access them until
the end of their lifetime, which is later for all three possible
storage durations.

And why do you think that a functions returned value is a C object,
if you haven't stored it in such an object?
 
C

CBFalconer

Nick said:
sorry? If its UB it can anything it likes. There may be no
requirement to produce an error message (a diagnostic) but
there can't be anything wrong with producing a diagnostic. So
why is (this version) of gcc buggy?

What bug? Failure to emit a diagnostic when no diagnostic is
required cannot be considered a bug.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top