C
Charles Banas
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the function (which was translated from Fortran code),
among other heinous and numerous declarations, is this bit:
static float bbuff[TBUF_SIZE][60][60];
static int bkey[TBUF_SIZE];
static int buse[TBUF_SIZE];
static int bmsg[TBUF_SIZE];
static int bptr;
int ii, jj;
float buff[60][60];
register char *r1buff;
register char *r2buff;
the purpose of bbuff is to make available multiple terrain buffers so
that it doesn't have to repeatedly read the terrain data file (which
is small and insignificant, so it's not that much of a bottleneck).
performance aside, here's a snippet from a section that returns one of
the full buffers in lieu of reading the original data file:
if (stat&TBUF_USE)
for (i = 0;i < TBUF_SIZE;i++)
{
if ((*key == bkey) && buse)
{
r1buff = (char *) & (buff[0][0]);
r2buff = (char *) & (bbuff[0][0]);
for (j = 0;j < 3600;j++, r1buff++, r2buff++)
*r1buff = *r2buff;
/* memcpy((char *)&(buff[0][0]),
(char *)&(bbuff[0][0]),
sizeof(float)*3600); */
/* transpose index buffer */
for (ii = 0; ii < 60 ; ii++)
for (jj = 0; jj < 60; jj++)
sbuff[jj][ii] = buff[ii][jj];
if (fd != NULL)
fclose(fd);
return (bmsg);
}
}
now you can see that the memcpy was originally tried, but later
dumped, likely because of how C arrays work. but the for loop caught
my attention: they're basically just copying bewteen two
de-referenced pointers. is this kosher? i know it works with both
Visual C++ 6 and GCC 3.3, but to me, it doesn't look like it should
work. can someone explain to me why the buffer copies correctly and
successfully despite the rather strange-looking method?
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the function (which was translated from Fortran code),
among other heinous and numerous declarations, is this bit:
static float bbuff[TBUF_SIZE][60][60];
static int bkey[TBUF_SIZE];
static int buse[TBUF_SIZE];
static int bmsg[TBUF_SIZE];
static int bptr;
int ii, jj;
float buff[60][60];
register char *r1buff;
register char *r2buff;
the purpose of bbuff is to make available multiple terrain buffers so
that it doesn't have to repeatedly read the terrain data file (which
is small and insignificant, so it's not that much of a bottleneck).
performance aside, here's a snippet from a section that returns one of
the full buffers in lieu of reading the original data file:
if (stat&TBUF_USE)
for (i = 0;i < TBUF_SIZE;i++)
{
if ((*key == bkey) && buse)
{
r1buff = (char *) & (buff[0][0]);
r2buff = (char *) & (bbuff[0][0]);
for (j = 0;j < 3600;j++, r1buff++, r2buff++)
*r1buff = *r2buff;
/* memcpy((char *)&(buff[0][0]),
(char *)&(bbuff[0][0]),
sizeof(float)*3600); */
/* transpose index buffer */
for (ii = 0; ii < 60 ; ii++)
for (jj = 0; jj < 60; jj++)
sbuff[jj][ii] = buff[ii][jj];
if (fd != NULL)
fclose(fd);
return (bmsg);
}
}
now you can see that the memcpy was originally tried, but later
dumped, likely because of how C arrays work. but the for loop caught
my attention: they're basically just copying bewteen two
de-referenced pointers. is this kosher? i know it works with both
Visual C++ 6 and GCC 3.3, but to me, it doesn't look like it should
work. can someone explain to me why the buffer copies correctly and
successfully despite the rather strange-looking method?