public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* sizeof() function parameter array: known problem?
@ 2005-07-01  8:45 Etienne Lorrain
  2005-07-01  9:17 ` Jonathan Wakely
  2005-07-01  9:17 ` Andrew Haley
  0 siblings, 2 replies; 3+ messages in thread
From: Etienne Lorrain @ 2005-07-01  8:45 UTC (permalink / raw)
  To: gcc

  The result of this funtion is 1, is there a C lawyer around?

$ cat tmp.c
unsigned fct (unsigned array[10])
{
        return sizeof(array) / sizeof(array[0]);
}
$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
$ gcc -S -o tmp.s tmp.c
$ cat tmp.s
        .file   "tmp.c"
        .text
.globl fct
        .type   fct, @function
fct:
        pushl   %ebp
        movl    %esp, %ebp
        movl    $1, %eax
        leave
        ret
        .size   fct, .-fct
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)"
$

  Etienne.


	

	
		
___________________________________________________________________________ 
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger 
Téléchargez cette version sur http://fr.messenger.yahoo.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sizeof() function parameter array: known problem?
  2005-07-01  8:45 sizeof() function parameter array: known problem? Etienne Lorrain
@ 2005-07-01  9:17 ` Jonathan Wakely
  2005-07-01  9:17 ` Andrew Haley
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2005-07-01  9:17 UTC (permalink / raw)
  To: Etienne Lorrain; +Cc: gcc

On Fri, Jul 01, 2005 at 10:45:19AM +0200, Etienne Lorrain wrote:

>   The result of this funtion is 1, is there a C lawyer around?

The parameter is treated as unsigned* since an array is converted to
a pointer when passed through a function.

C99 says in 6.7.5.3:

   [#7] A declaration of a parameter as ``array of type'' shall
   be adjusted to ``qualified pointer to type'', where the type
   qualifiers (if any) are those specified within the [  and  ]
   of  the  array  type derivation.

> $ cat tmp.c
> unsigned fct (unsigned array[10])

These prototypes are all equivalent, and any sized array (or just a
plain pointer) can be passed to them:

    unsigned fct (unsigned array[10]);
    unsigned fct (unsigned array[]);
    unsigned fct (unsigned* array);

> {
>         return sizeof(array) / sizeof(array[0]);
> }

Therefore what you're testing is sizeof(unsigned*)/sizeof(unsigned)
which is 1 on x86 and most other 32-bit targets (but 2 on e.g. x86_64)

Hope that helps,

jon


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sizeof() function parameter array: known problem?
  2005-07-01  8:45 sizeof() function parameter array: known problem? Etienne Lorrain
  2005-07-01  9:17 ` Jonathan Wakely
@ 2005-07-01  9:17 ` Andrew Haley
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Haley @ 2005-07-01  9:17 UTC (permalink / raw)
  To: Etienne Lorrain; +Cc: gcc

Etienne Lorrain writes:
 >   The result of this funtion is 1, is there a C lawyer around?
 > 
 > $ cat tmp.c
 > unsigned fct (unsigned array[10])
 > {
 >         return sizeof(array) / sizeof(array[0]);
 > }

This is 6.7.5.3, Para. 7.

Andrew.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-07-01  9:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-01  8:45 sizeof() function parameter array: known problem? Etienne Lorrain
2005-07-01  9:17 ` Jonathan Wakely
2005-07-01  9:17 ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).