I
I wish
I found someone wrote this code
int **array2, nrows, ncolumns, i;
scanf( "%d %d", &nrows, &ncolumns );
array2 = malloc(nrows*sizeof(int *) + nrows*ncolumns*sizeof(int));
for(i = 0; i < nrows; i++ )
array2 = (int *)array2 + nrows + i * ncolumns;
This can decrease malloc times to just once.
It it legal? I think the code assume the sizeof( int ) is equal to
sizeof( int * ), then the code may not be portable.
--
|
___
(-_-)
<| |>---------------------------------- ShepJeng.twbbs.org -------------
/ cherry.cs.nccu.edu.tw
int **array2, nrows, ncolumns, i;
scanf( "%d %d", &nrows, &ncolumns );
array2 = malloc(nrows*sizeof(int *) + nrows*ncolumns*sizeof(int));
for(i = 0; i < nrows; i++ )
array2 = (int *)array2 + nrows + i * ncolumns;
This can decrease malloc times to just once.
It it legal? I think the code assume the sizeof( int ) is equal to
sizeof( int * ), then the code may not be portable.
--
|
___
(-_-)
<| |>---------------------------------- ShepJeng.twbbs.org -------------
/ cherry.cs.nccu.edu.tw