[Bf-committers] cast from flat arrays to multidimensional arrays

Jacques Guignot bf-committers@blender.org
Mon, 23 Jun 2003 21:16:31 +0000


While poking in blender code, I found strange things.

I show it on an example. Let us start from

void IMB_cspace(struct ImBuf *ibuf, float mat[][4])
{
short *cont_1,*cont_2,*cont_3,add[3];

    cont_1=(short *)malloc(256*3*sizeof(short));
    cont_2=(short *)malloc(256*3*sizeof(short));
    cont_3=(short *)malloc(256*3*sizeof(short));
//OK. Three short*, which are malloc'd

snip snip snip
    rotcspace(ibuf, cont_1, cont_2, cont_3, add);
//a call to rotcspace, with these pointers.
snip snip snip
}


Let's have a look at rotcspace

static void rotcspace(struct ImBuf *ibuf, short *cont_1, short *cont_2, 
short *cont_3, short *add)
{
snip snip snip
            cspadd(buf,cont_1,rect+0,x);
            cspadd(buf,cont_2,rect+1,x);
            cspadd(buf,cont_3,rect+2,x);
//calls cspadd. OK.
            cspret(buf,rect,x);
            rect += x<<2;
        }
        free(buf);
}

so far, so good, the pointers are passed to cspadd.
Let's have a look at cspadd.


Eeeekkkk!!!!

static void cspadd(short *buf, short cont[][3], unsigned char *rect, int x)
{
    short i;
    for (;x>0;x--){
        i = *(rect);
        rect += 4;
        buf[0] += cont[i][0];
        buf[1] += cont[i][1];
        buf[2] += cont[i][2];
        buf += 3;
    }
}


cspadd demands a short cont[][3], and a char* is passed-->warning!
warning: passing arg 2 of `cspadd' from incompatible pointer type

It is simple to rewrite cspadd :

static void cspadd(short *buf, short *cont, unsigned char *rect, int x)
{
    short i;
    for (;x>0;x--){
        i = *(rect);
        rect += 4;
        buf[0] += cont[i*3];
        buf[1] += cont[i*3+1];
        buf[2] += cont[i*3+2];
        buf += 3;
    }
}


The programmer clearly noticed that multi-dimensionnal arrays, in C,
are represented like flat arrays, and uses flat arrays as
multidimensional arrays.


I'm willing to get rid of the zillions of warnings generated by the
compilation.
Should these modifications be commited, or am I missing a point?

Tks in advance.