According to the documentation at
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html it should be possible to
use a VLA as a function parameter and preserve the array length for use with
sizeof().

The documentation indicates that the C program at end of this description
should produce the following output

s: <This is a string>
sizeof(s): 17
s: <This is a string>
sizeof(s): 17

However gcc 4.4.1 produces the following output instead (note that the second
sizeof calculation is incorrect)

s: <This is a string>
sizeof(s): 17
s: <This is a string>
sizeof(s): 4

Note that the following C program is compiled with the -std=c99 option.


#include <string.h>
#include <stdio.h>

void
vararray(size_t l, const char s[l])
{
    printf("s: <%s>\nsizeof(s): %d\n", s, sizeof(s));
}

int
main()
{
    char s[] = "This is a string";

    printf("s: <%s>\nsizeof(s): %d\n", s, sizeof(s));

    vararray(sizeof(s), s);
    return 0;
}


-- 
           Summary: VLA as function parameter loses sizeof property
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tbullock at comlore dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42656

Reply via email to