ccwork said:
Hi,
In 2.18 "structure-valued functions are usually implemented by
adding a hidden return pointer". What's that mean??
Often, if you define functions returning structs, the compiler
internally does something different:
struct foo bar (int baz)
{
struct foo tmp;
........
return tmp;
}
void tst (void)
{
struct foo test;
test = bar(42);
}
may be handled as if we had
void bar (int baz, struct foo *p)
{
/* work on *p */
........
}
void tst (void)
{
struct foo test;
bar(42, &test);
}
FAQ 2.18:
"2.18: This program works correctly, but it dumps core after it
finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ ... }
A: A missing semicolon causes main() to be declared as returning a
structure. (The connection is hard to see because of the
intervening comment.) Since structure-valued functions are
usually implemented by adding a hidden return pointer, the
generated code for main() tries to accept three arguments,
although only two are passed (in this case, by the C start-up
code). See also questions 10.9 and 16.4.
References: CT&P Sec. 2.3 pp. 21-2.
"
With main() being the first function (from our point of view),
there is no way to pass the address of a valid struct foo
object. At return from main(), we are in trouble.
This kind of error usually does not happen if you do not rely on
implicit int.
Cheers
Michael