I’m trying to store three square, 2d arrays into a buffer so I use one contiguous block of memory. If the array size is rxr, my formula is:
buffer = (double*) malloc((r + r + r) * sizeof(double *) +
(r*r + r*r + r*r) * sizeof(double));
if(buffer == NULL) {
printf("out of memory\n");
return 0;
}
for(i = 0, j = 0; i < r; i++) {
a[i] = &buffer[j];
j+=r;
}
for(i = 0; i < r; i++) {
b[i] = &buffer[j];
j+=r;
}
for(i = 0; i < r; i++) {
c[i] = &buffer[j];
j+=r;
}
a = buffer[j];
b = buffer[j + r];
c = buffer[j + r + r];
As you can see, I’m lost. a, b, c, are declared as double pointers (meant to be pointers to arrays of arrays), so I want each to have an array of size r, with each element pointing to its own separate array of size r. Any ideas…
You’re confusing malloc’ing pointers with malloc’ing doubles themselves. Just allocate the doubles like:
Then where do your actual matrices go?
Please check for off by one errors 🙂 But the point is you don’t need to malloc out both the double* and the double.