F
Francine.Neary
I'm experimenting with a program that manipulates a large matrix, i.e.
a 2-deep array. I started by just allocating this in one go, but later
it occurred to me that, as there are still some very old systems
around where such a large allocation might fail, perhaps it would be
better to split the allocation into many allocations, one for each
row, to increase portability to legacy systems.
The advantage I see is that the memory allocator or operating system
might not be able to find a contiguous block, but several smaller
blocks might be OK. The downside would be that there is likely to be
more overhead in the allocator, and my program itself needs to store
an extra thousand pointers to keep track of the individual rows.
What do people think is best?
Here's some example code in case what I say isn't clear.
#include <stdlib.h>
#define ROWS (1<<10)
#define COLS (1<<20)
/* approach 1 */
static int *M;
void init()
{
if(!M=malloc(ROWS*COLS*sizeof(int)))
abort();
}
/* approach 2 */
static int *r[ROWS];
void init()
{
int i;
for(i=0; i<ROWS; (r[i++]=malloc(COLS*sizeof(int))) || (abort(),0));
}
a 2-deep array. I started by just allocating this in one go, but later
it occurred to me that, as there are still some very old systems
around where such a large allocation might fail, perhaps it would be
better to split the allocation into many allocations, one for each
row, to increase portability to legacy systems.
The advantage I see is that the memory allocator or operating system
might not be able to find a contiguous block, but several smaller
blocks might be OK. The downside would be that there is likely to be
more overhead in the allocator, and my program itself needs to store
an extra thousand pointers to keep track of the individual rows.
What do people think is best?
Here's some example code in case what I say isn't clear.
#include <stdlib.h>
#define ROWS (1<<10)
#define COLS (1<<20)
/* approach 1 */
static int *M;
void init()
{
if(!M=malloc(ROWS*COLS*sizeof(int)))
abort();
}
/* approach 2 */
static int *r[ROWS];
void init()
{
int i;
for(i=0; i<ROWS; (r[i++]=malloc(COLS*sizeof(int))) || (abort(),0));
}