[Kos-misc] Fonction en C retournant une structure...

d2 David.Decotigny@irisa.fr
18 Sep 2001 09:53:35 +0200


Hello,

Pour resumer, cette fonctionnalite fait partie de la norme C ANSI (et
ISO).

FAQ comp.lang.c :

     Structures are often returned from functions in a location
     pointed to by an extra, compiler-supplied ``hidden'' argument to
     the function. Some older compilers used a special, static
     location for structure returns, although this made
     structure-valued functions non-reentrant, which ANSI C disallows.

(http://www.eskimo.com/~scs/C-faq/q2.9.html)


Cas de gcc/pcc (info gcc) :

     The method used by GCC is as follows: a structure or union which is
     1, 2, 4 or 8 bytes long is returned like a scalar.  A structure or
     union with any other size is stored into an address supplied by
     the caller (usually in a special, fixed register, but on some
     machines it is passed on the stack).  The machine-description
     macros `STRUCT_VALUE' and `STRUCT_INCOMING_VALUE' tell GCC where
     to pass this address.

     By contrast, PCC on most target machines returns structures and
     unions of any size by copying the data into an area of static
     storage, and then returning the address of that storage as if it
     were a pointer value.  The caller must copy the data from that
     memory area to the place where the value is wanted.  GCC does not
     use this method because it is slower and nonreentrant.

     On some newer machines, PCC uses a reentrant convention for all
     structure and union returning.  GCC on most of these machines uses
     a compatible convention when returning structures and unions in
     memory, but still returns small structures and unions in registers.

(http://gcc.gnu.org/onlinedocs/gcc-3.0.1/gcc_9.html#SEC143)

Conclusion : c'est tout a fait normal que ca marche. C'est egalement
normal que ca marche si on ne passe pas une reference, mais
directement la structure a la fonction.

Bonne journee,

-- 
d2