O
Ovid
Hi all,
I'm having a problem trying to create a 2D array whose dimensions are
determined at runtime. Below my signoff is a minimal test case that
hopefully demonstrates what I'm trying to do. Unfortunately, this
segfaults. The output is the following:
$ gcc -Wall -c arrays.c
$ gcc -o arrays arrays.o
$ ./arrays
0, 0 is 0.790188
0, 1 is 0.344383
0, 2 is 0.733099
1, 0 is 0.748440
1, 1 is 0.861647
1, 2 is 0.147551
2, 0 is 0.285223
2, 1 is 0.718230
2, 2 is 0.227775
3, 0 is 0.503970
3, 1 is 0.427397
3, 2 is 0.578871
Segmentation fault
Note: This is the first pass of this problem, but the next pass will
require turning this into a 3 dimensional array with none of the
parameters known at compile time. I assume this has no bearing on the
problem, but I'm not sure.
Any critique of the code is welcome as it's been years since I've
worked with C.
Cheers,
Curtis "Ovid" Poe
#include <stdio.h>
#include <stdlib.h>
double **weights;
double ** malloc_weights(int num_rows, int rowsize)
{
int i;
/* The last element is 0, so free_weights can detect the last row
*/
weights = malloc(sizeof(void *) * (num_rows+2)); /* one extra for
sentinel */
if(weights == 0) return 0;
/* allocate the actual rows */
for(i = 0; i < num_rows; i++) {
weights = malloc(rowsize);
if(weights == 0) {
return 0;
}
}
/* initialize the sentinel value */
weights[num_rows+1] = 0;
return weights;
}
void assign_random_weights(int rows, int cols)
{
int i,j;
for (i = 0; i < rows+1; i++) {
for (j = 0; j < cols; j++) {
weights[j] = ( ((float)rand() / (float)RAND_MAX) -.05
);
printf("%d, %d is %f\n", i, j, weights[j]);
}
}
}
int main(void)
{
double **array;
int i,j;
int rows = 4;
int cols = 3;
array = malloc_weights(rows,cols*sizeof(double));
assign_random_weights(rows,cols);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
printf("%d, %d is %f\n", i, j, weights[j]);
return 0;
}
I'm having a problem trying to create a 2D array whose dimensions are
determined at runtime. Below my signoff is a minimal test case that
hopefully demonstrates what I'm trying to do. Unfortunately, this
segfaults. The output is the following:
$ gcc -Wall -c arrays.c
$ gcc -o arrays arrays.o
$ ./arrays
0, 0 is 0.790188
0, 1 is 0.344383
0, 2 is 0.733099
1, 0 is 0.748440
1, 1 is 0.861647
1, 2 is 0.147551
2, 0 is 0.285223
2, 1 is 0.718230
2, 2 is 0.227775
3, 0 is 0.503970
3, 1 is 0.427397
3, 2 is 0.578871
Segmentation fault
Note: This is the first pass of this problem, but the next pass will
require turning this into a 3 dimensional array with none of the
parameters known at compile time. I assume this has no bearing on the
problem, but I'm not sure.
Any critique of the code is welcome as it's been years since I've
worked with C.
Cheers,
Curtis "Ovid" Poe
#include <stdio.h>
#include <stdlib.h>
double **weights;
double ** malloc_weights(int num_rows, int rowsize)
{
int i;
/* The last element is 0, so free_weights can detect the last row
*/
weights = malloc(sizeof(void *) * (num_rows+2)); /* one extra for
sentinel */
if(weights == 0) return 0;
/* allocate the actual rows */
for(i = 0; i < num_rows; i++) {
weights = malloc(rowsize);
if(weights == 0) {
return 0;
}
}
/* initialize the sentinel value */
weights[num_rows+1] = 0;
return weights;
}
void assign_random_weights(int rows, int cols)
{
int i,j;
for (i = 0; i < rows+1; i++) {
for (j = 0; j < cols; j++) {
weights[j] = ( ((float)rand() / (float)RAND_MAX) -.05
);
printf("%d, %d is %f\n", i, j, weights[j]);
}
}
}
int main(void)
{
double **array;
int i,j;
int rows = 4;
int cols = 3;
array = malloc_weights(rows,cols*sizeof(double));
assign_random_weights(rows,cols);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
printf("%d, %d is %f\n", i, j, weights[j]);
return 0;
}