-1

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);
  • 2
    `double **student = (double **)malloc(rows * sizeof(int *));` That has `double` on one side and `int` on another. Any alarms there for you? Casting isn't magic - if you force cast it to something incorrect you will get incorrect results. – kaylum Apr 17 '21 at 06:56
  • 1
    [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Apr 17 '21 at 06:58
  • @kaylum tx didnt realise that sometimes stupid mistakes are made ;) Its working now – Programmer_007 Apr 17 '21 at 07:09
  • @Barmar what do you prefer ? – Programmer_007 Apr 17 '21 at 07:10
  • 2
    `double **student = malloc(rows * sizeof(*student));` – Barmar Apr 17 '21 at 07:12
  • Nothing at all in your code has anything to do with an "array". A *pointer-to-pointer* (e.g. `double**`) is a single pointer to an allocated block of memory containing more pointers where each pointer in turn points to (holds the address of) a block of allocated memory containing `double` values. This allows you to simulate 2D indexing, but is NOT an array. `query` is a pointer-to-pointer. `query[i]` is a pointer to allocated block of `double`. `query[i][j]` is a `double` value within that allocated block. – David C. Rankin Apr 17 '21 at 07:18

1 Answers1

0

@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));
  • You can avoid the problem in general by referencing the variable rather than the type: `rows * sizeof(*student)` and `columns * sizeof(*student[i])` – Barmar Apr 17 '21 at 07:13