0
votes

So Im doing so Matrix calculations/conversions right now and now an assertion error showed up.

I started implementing a Matrix-CRS-Conversion and the program works just fine:

uint32_t size = 0;
for (uint32_t i = 0; i < ROWS; i++)
{
    for (uint32_t j = 0; j < COLUMNS; j++)
    {
        if(Matrix[i][j] != 0) 
            size++;
    }
}

// Allocating memory for CRS
uint32_t *values = (uint32_t*)malloc(size * sizeof(uint32_t));
uint32_t *columnindex = (uint32_t*)malloc(size * sizeof(uint32_t));
uint32_t *rowchange =  (uint32_t*)malloc(ROWS+1 * sizeof(uint32_t));

uint32_t position = 0;
uint32_t rowPtr = 0;
rowchange[0] = 0;
rowchange[ROWS] = size;

for (uint32_t i = 0; i < ROWS; i++)
{
    for (uint32_t j = 0; j < COLUMNS; j++)
    {
        if(Matrix[i][j] != 0) {
            values[position] = Matrix[i][j];
            columnindex[position] = j;
            position++;
        }
     }
     rowPtr++;
     if(rowPtr != ROWS)
         rowchange[rowPtr] = position;
}

When I print out the results everything is correct as intended.

Now I wanted to implement a matrix-vector-multiplication and I started allocating memory for a vector to multiplicate and a result vector:

uint32_t *multvector = (uint32_t*)malloc(ROWS * sizeof(uint32_t));
uint32_t *result = (uint32_t*)malloc(ROWS * sizeof(uint32_t));

I did it in the same way as above but now I get an Assertion error:

Matrix: malloc.c:2385: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

I have no idea why.

1

1 Answers

2
votes

You have corrupted malloc's heap, most likely:

uint32_t *rowchange =  (uint32_t*)malloc(ROWS+1 * sizeof(uint32_t));

should have been:

 uint32_t *rowchange =  (uint32_t*)malloc((ROWS+1) * sizeof(uint32_t));

or, even better:

 uint32_t *rowchange =  malloc((ROWS+1) * sizeof(*rowchange));

Notes on style/habits:

  1. Casting is a bad thing, and should only be used when necessary. It reduces the ability of the compiler to report on questionable type combinations, and makes the code harder to read, particularly in cases like this where it does nothing.

  2. sizeof(*rowchange) is usually preferred over sizeof(type-name) just to loosen the coupling a bit. It reduces the number of things to check for code review, and makes the intent more obvious.

  3. The usual, you should C+P something that can be run without having to add a tonne of scaffolding when you ask questions here.