Opened 9 months ago

Last modified 9 months ago

#220 new defect

Reference to Array of Generics does not work

Reported by: mlbrooks Owned by:
Priority: major Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description (last modified by mlbrooks)

The declaration float (&a)[7], which is pronounced, "a is a reference to a length-7 array of floats," is well-defined, useful, and works.

Its generic version, where the element type T replaces float, ought to be equivalently usable, but it is broken.

#ifndef __cforall
#include <stdio.h>
#endif

void doPrint(float v) {
    printf("%f\n", v);
}

void try1( float (&a)[7] ) {
    printf("%ld %p %p\n", sizeof(a), &a, &a[0]);
    doPrint(a[0]);
}

#ifdef __cforall
forall( otype T | {void doPrint(T);} )
void try2( T (&a)[7] ) {
    printf("%ld %p %p\n", sizeof(a), &a, &a[0]); // prints
    doPrint(a[0]);  // crashes
}
#endif

int main() {
    float items[7];
    items[0] = 3.14;
    printf("%f is 0x%x\n", items[0], *(unsigned int *)&items[0]);
    try1(items);
  #ifdef __cforall
    try2(items);
  #endif
}

CFA Actual: compiles with output

3.140000 is 0x4048f5c3
try1: 28 0x7fffffffe2b0 0x7fffffffe2b0
doPrint: 3.140000
try2: 8 0x7fffffffe2b0 0x4048f5c3
Cforall Runtime error (UNIX pid:920) Segment fault at memory location 0x4048f5c3.

CFA Expected: compiles with output

3.140000 is 0x4048f5c3
try1: 28 0x7fffffffe2b0 0x7fffffffe2b0
doPrint: 3.140000
try2: 28 0x7fffffffe2b0 0x7fffffffe2b0
doPrint: 3.140000

g++ Actual and Expected: compiles with output

3.140000 is 0x4048f5c3
try1: 28 0x7fffffffe370 0x7fffffffe370
doPrint: 3.140000

The CFA try2 output suggests that typeof(a) is float* instead of float[7].

The fact that g++ treats try1 consistently with CFA-actual supports CFA-try1 behaviour being a model for the expected behaviour, with the inconsistency of CFA-try2 being a bug.

For the printed in-stack address (0x7ff...), it is not relevant what the absolute value is. The same value occurs throughout one program run. It is relevant when the program prints that value, vs 0x4048f5c3.

Change History (1)

comment:1 Changed 9 months ago by mlbrooks

Description: modified (diff)
Note: See TracTickets for help on using tickets.