online C programming test

  • Thread starter E. Robert Tisdale
  • Start date
R

Richard Heathfield

Peter said:
MSG said:
Yes, but others failed it many times as well, sometimes thanks to him [ERT],
e.g. when many C people stated that you could not return structs from
functions. I don't want to google, but it went something like this

struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;

return s;
}

*Many* (but not ERT) said you could not do this in C. So let's be fair
here.

OK, let's be fair. The code above *is* in fact illegal ;-)

Why?

$ cat foo.c
#include <stdio.h>

struct foo
{
int x;
int y;
int z;
};

int x, y, z;

/* Here's that code again, this time in some context. */

struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;

return s;
}

int main(void)
{
struct foo v;
x = 6;
y = 42;
z = 117;

v = f();

printf("%d\n%d\n%d\n", v.x, v.y, v.z);

return 0;
}

$ gcc -g -W -Wall -ansi -pedantic -o foo foo.c
$ ./foo
6
42
117

The fact that the code produces no diagnostics at a fairly severe warning
level, and gives the expected results, is of course not conclusive. But
neither is a simple assertion that the code is illegal!
 
C

Christopher Benson-Manica


It may be that he was confused (as I recently was) about returning
automatic variables. The code is legal, but I had to delete words I
just wrote asserting the opposite - if it hadn't been one of
unimpeachable correctness such as yourself who had posted that, I
might have been lured into yet another stupid post ;)
 
A

Arthur J. O'Dwyer

Peter said:
MSG said:
Yes, but others failed it many times as well, sometimes thanks to him
[ERT], e.g. when many C people stated that you could not return
structs from functions. I don't want to google, but it went something
like this

struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;
return s;
}

*Many* (but not ERT) said you could not do this in C. So let's be fair
here.

OK, let's be fair. The code above *is* in fact illegal ;-)

Why?
/* Here's that code again, this time in some context. */

Okay, *now* it's legal. ;-) [I can only assume that Peter was
referring to the fact that the OP had not defined 'struct foo'
anywhere in the translation unit under discussion.]

For reasons explained elsethread, I am firmly *against* returning
structs from functions, almost as firmly as I am against malloc
casting or using ALL_UPPER_CASE for anything other than macros --
but I have never claimed that the practice is illegal, and I don't
recall anyone else's doing so.

-Arthur
 
J

Jack Klein

Peter said:
Yes, but others failed it many times as well, sometimes thanks to him
[ERT], e.g. when many C people stated that you could not return
structs from functions. I don't want to google, but it went something
like this

struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;
return s;
}

*Many* (but not ERT) said you could not do this in C. So let's be fair
here.

OK, let's be fair. The code above *is* in fact illegal ;-)

Why?
/* Here's that code again, this time in some context. */

Okay, *now* it's legal. ;-) [I can only assume that Peter was
referring to the fact that the OP had not defined 'struct foo'
anywhere in the translation unit under discussion.]

For reasons explained elsethread, I am firmly *against* returning
structs from functions, almost as firmly as I am against malloc

Why, may I ask? I don't do it often, but have no qualms about doing
it when it is efficient.
 
A

Arthur J. O'Dwyer

Why, may I ask? I don't do it often, but have no qualms about doing
it when it is efficient.

<[email protected]>
and follow-ups give the 'elsethread' argument to which I referred.
Mostly, it's that returning structs is
- not transparent enough for my taste
- almost always easy to do another way, at least in my code
- not the way I first saw it done (K&R1 explicitly mentions that
you couldn't pass structs *to* functions in Ye Olde C, but I didn't
find any mention of *returning* structs, not that I looked very hard).
This is the real meat of my argument; I *said* it was religious! :)

-Arthur,
signing off
 
M

MSG

Richard Heathfield said:
Er, that's bizarre. Of course you can. The Standard even gives an example of
(a pointer to) a function that returns a struct.

Not saying it's a great idea, mind - but you can certainly do it.


Do you have a thread reference for that? A message ID or something?

I can't find it, given the limited time I have and the fact that I do
not recall any rare "keywords" from that thread. However, take a look
at this bit of ERT wisdom (pay attention to comments in asm code)

http://groups.google.com/[email protected]

which is not as good as what I wanted to find, but nevertheless.

I think ERT lacks some social skills, granted, who doesn't? But
branding him a troll is probably a fruit of groupthink.

MSG
 
M

Mike Wahler

E. Robert Tisdale said:
I agree.
But it would be helpful if someone had collect these examples
into a handy compendium.

Well then, go right ahead. Or are you asking others to
do your work for you?

I do indeed have a collection of snippets garnered from clc
for my own learning and use, but it's only specific to *my*
interests, and isn't really very 'general' or 'wide-scope'.

-Mike
 
R

Richard Heathfield

MSG said:
I can't find it,

Neither can anyone else, it seems.
given the limited time I have and the fact that I do
not recall any rare "keywords" from that thread. However, take a look
at this bit of ERT wisdom (pay attention to comments in asm code)

http://groups.google.com/[email protected]

which is not as good as what I wanted to find, but nevertheless.

Nevertheless, it is an article in which Mr Tisdale demonstrates how one may
post off-topic articles on assembly language instead of giving an
appropriate answer based on the semantics of the C language.
I think ERT lacks some social skills, granted, who doesn't? But
branding him a troll is probably a fruit of groupthink.

Do you? I think you just don't know him well enough. Of course, we must each
make up our own minds on these matters; but I think you're wasting your
time if you seek to persuade anyone who knows C that Mr Tisdale is worth
reading.
 
J

Joona I Palaste

Why, may I ask? I don't do it often, but have no qualms about doing
it when it is efficient.

I would also be surprised if someone was against malloc. Erm, wait a
minute here...
 
J

Jeremy Yallop

Richard said:
Er, that's bizarre. Of course you can. The Standard even gives an example of
(a pointer to) a function that returns a struct.

Further, a number of standard functions return structs (e.g. div()).

Jeremy.
 
C

CBFalconer

Richard said:
Peter Pichler wrote:



No, but there does seem to be some kind of underlying ethos of "I'm
doing this for free, so I don't have to be legible", which I find
appalling. I go to a lot of trouble to make my own code readable.
Whether I succeed is for others to judge for themselves, but at
least I try. I wish this were more common.

For the benefit of the general public, many of us have found that
writing for clarity makes things easier for the reader down the
line, and further examination of that theme reveals that that
reader is us. Even so, later re-reading often produces much
scratching of various body parts.
 
C

CBFalconer

Default said:
.... snip ...

Yes, it is true that Trollsdale is not a complete idiot, which
makes him dangerous. He can talk a good game, enough to suck
the newbies in.

This is no more dangerous than any other consumate idiot posting
here. However ERTs forte is revisionism, whereby he quotes
someone and edits that quote into meaninglessness or falsity.
This forces people to watch him closely, and deny those alleged
quotes. Idiocy is forgivable, the revisionism is not.
 
D

Dan Pop

Peter said:
Yes, but others failed it many times as well, sometimes thanks to him
[ERT], e.g. when many C people stated that you could not return
structs from functions. I don't want to google, but it went something
like this

struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;
return s;
}

*Many* (but not ERT) said you could not do this in C. So let's be fair
here.

OK, let's be fair. The code above *is* in fact illegal ;-)

Why?
/* Here's that code again, this time in some context. */

Okay, *now* it's legal. ;-) [I can only assume that Peter was
referring to the fact that the OP had not defined 'struct foo'
anywhere in the translation unit under discussion.]

There was NO translation unit under discussion, merely a code snippet
that was NOT supposed to be a complete translation unit. People seldom
post complete translation units.

Dan
 
P

Peter Pichler

Arthur J. O'Dwyer said:
/* Here's that code again, this time in some context. */

Okay, *now* it's legal. ;-) [I can only assume that Peter was
referring to the fact that the OP had not defined 'struct foo'
anywhere in the translation unit under discussion.]

Bingo! Plus missing declarations for x, y and z. And yes, I knew it was a
code fragment that would make sense in context, hence my smiley.
For reasons explained elsethread, I am firmly *against* returning
structs from functions, [...]

I am not strictly against, but I don't do it myself.

Peter
 
C

Chris Torek

Mostly, it's that returning structs is
- not transparent enough for my taste
- almost always easy to do another way, at least in my code
- not the way I first saw it done (K&R1 explicitly mentions that
you couldn't pass structs *to* functions in Ye Olde C, but I didn't
find any mention of *returning* structs, not that I looked very hard).
This is the real meat of my argument; I *said* it was religious! :)

It might be worth noting that the Portable C Compiler (one of
several common compilers, and perhaps even the most common,
implementing C at the time the K&R "white book" came out)
handled struct-value returns with non-reentrant code.

Specifically, it rewrote:

struct S f(...) { ... return val; }
...
result = f(...);

as:

struct S *f(...) {
static struct S retval;
...
retval = val;
return &retval;
}

result = *f(...);

(the ellipses here are neither K&R-1 C nor ANSI C, but rather
meant to imply "actual arguments do not matter").

The hidden-pointer-parameter method that gcc uses on the i386 is
"better", in my opinion, in that it is reentrant and can often
avoid some copying. It does, however, require that a declaration
of f() be in scope even if the caller intends to ignore the return
value. (Returning small structures in registers is even better
yet, of course, but often incompatible with vendor ABIs.)
 
M

MSG

Richard Heathfield said:
Neither can anyone else, it seems.

Calling me a liar, heh? Very smooth.

BTW, many == 3 in this case:
Tom wrote "not valid"
Jarno (agreed?)
Nick implied that returning automatically allocated structs was
undefined

Not bad for an "idiot troll"
Nevertheless, it is an article in which Mr Tisdale demonstrates how one may
post off-topic articles on assembly language instead of giving an
appropriate answer based on the semantics of the C language.


Do you? I think you just don't know him well enough.

Even better! Think of me as a juror, not his defense attourney - I
think he's fit to represent himself. So far, I haven't seen the
"prosecution" try to make a case - all I saw was a bunch of emotional
people with old grudges. If you think *you* have a case, make it once,
say, post 3 links/msg-id's that conclusively show him to be a troll.
Of course, we must each
make up our own minds on these matters; but I think you're wasting your
time if you seek to persuade anyone who knows C that Mr Tisdale is worth
^^^^^^^^^^^^^^^^^^

It must be one of those "true Irishman" arguments.

BTW, anyone who "knows C" would only find "off-topic" messages
interesting here.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top