So im trying to create two dynamic arrays, one for holding reference points and the other for holding query points but i get this error: malloc(): corrupted top size Aborted (core dumped) I want to create 2 2D arrays with the same widths, row size doesnt matter but there should be at least one row.
double **createSyntheticReference(int rows, int columns)
{
int i,j;
// Dynamically allocate a 2D array
double **student = (double **)malloc(rows * sizeof(int *));
for ( i=0; i<rows; i++)
student[i] = (double *)malloc(columns * sizeof(int));
// load synthetic data
for(i=0; i<rows;i++) {
for(j=0; j<columns; j++) {
student[i][j] = rand()%100+1;
}
}
for(i=0; i<rows; i++) {
printf("%lf", student[i][0]);
for(j=1; j<columns; j++) {
printf("%17lf", student[i][j]);
}
puts("");
}
return student;
}
double **createSyntheticQuery(int columns)
{
int i,j;
int rows = 10;
// Dynamically allocate a 2D array
double **query = (double **)malloc(rows * sizeof(int *));
for ( i=0; i<rows; i++)
query[i] = (double *)malloc(columns * sizeof(int));
// load synthetic data
for(i=0; i<rows;i++) {
for(j=0; j<columns; j++) {
query[i][j] = rand()%100+1;
}
}
for(i=0; i<rows; i++) {
printf("%lf", query[i][0]);
for(j=1; j<columns; j++) {
printf("%17lf", query[i][j]);
}
puts("");
}
return query;
}
in my main i run this to try and create the 2 2d arrays:
double **student = createSyntheticReference(rows, columns);
double **query = createSyntheticQuery(columns);
@kaylum helped me with problem, i didnt see that im allocating space for int but it should be double
double **student = (double **)malloc(rows * sizeof(double *));
for ( i=0; i<rows; i++)
student[i] = (double *)malloc(columns * sizeof(double));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.