[m-dev.] Re: C Standard or GCC bug?
Fergus Henderson
fjh at cs.mu.OZ.AU
Mon Jan 15 16:29:46 AEDT 2001
On 15-Jan-2001, Levi Paul Cameron <l.cameron2 at ugrad.unimelb.edu.au> wrote:
>
> Either I have reached the limit of the C standard or there is a bug in
> GCC:
>
> If you create the following program:
>
>
> #include <mercury_imp.h>
>
> typedef struct {
> int a0;
> int b1;
> int c2;
> int d3;
> } DATA;
>
> DATA func(void) {
> DATA d = {0, 1, 2, 3};
> return d;
> }
>
> it compiles fine.
> However, as soon as you increase the data structre size
> (add another int to the data structure) GCC aborts with an
> error about si doing something it is not supposed to. Is this
> because it is being used already (si, di & bx all appear to
> be used as mercury registers) and this is a expected or is the
> C standard only supposed to allow returning small structures on
> the stack?
This is a bug/feature of GCC. It occurs only if you use GCC's global
register variables extension.
> (I can work around it by mallocing memory and returning a
> pointer, but for the part I am using it, it is going to be more
> efficent if this doesn't have to happen)
A more efficient work-around is to use pass-by-reference rather than
return by value: pass a pointer to the struct. Then you can use
MR_memcpy() to assign the result. This avoids the need to use
malloc().
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list