This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Dimensions of array parameters


Hi,

Ian Lance Taylor <iant@google.com> skribis:

> ludovic.courtes@inria.fr (Ludovic CourtÃs) writes:
>
>> I understand.  However, Iâm concerned about keeping the information at
>> compile-time.  For example:
>>
>>   extern void foo (int a, int x[a]);
>>   static void bar (void) {
>>     int x[123];
>>     foo (456, x);
>>   }
>>
>> Here the compiler could emit a diagnostic in âbarâ.

Oh, and within âfooâ itself:

  int foo (int a, int x[a])
  {
    return foo[a + 23];  /* <- warning here */
  }

> I see.  That seems to have some potential.  It would be a gcc extension,
> of course.  Perhaps something like
>
> extern void foo (int a, int x[__attribute__ ((dim (a)))])
>
> could be implemented.

Why use special syntax for this?  It seems to me that âint x[a]â conveys
the exact same information.

> This would issue an error if foo were called such that the length of
> the x were not at least a.  A serious problem with this approach would
> be that it is not reliable, since the caller of foo might not know the
> length of the array being passed as a pointer.

In that case the call site could just be ignored, or a âcannot determine
array sizeâ warning could be issued.

> I suppose it would be possible to issue runtime checks when static
> checks don't work, and issue a compilation error in cases were no
> runtime check is possible.  Without those checks I think the extension
> would be ill advised.  With those checks I don't know how often it
> would usable in practice.

Yes.

Thanks,
Ludoâ.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]