Patrick.SE
Patrick.SE

Reputation: 4564

Segmentation fault when freeing my matrix

I'm using code::blocks.

Code sends a seg fault when freeing the matrix after 2-3 iterations in dealloc_mat.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int **_mat;
int _lines, _columns;


void alloc_mat();
void dealloc_mat();

int main(int argc, char *argv[])
{
    _lines = 31, _columns = 22;

    alloc_mat();
    dealloc_mat();

    return 0;
}

void alloc_mat()
{
    int i, row, col;
    _mat = malloc(sizeof(int *) * _lines);

    for(i = 0 ; i < _lines ; i++)
    {
        int *tmpMatrix = malloc(sizeof(int) * _columns);
        _mat[i] = &tmpMatrix[i];
    }

    for(row = 0 ; row < _lines ; row++)
    {
        for(col = 0 ; col < _columns ; col++)
        {
            _mat[row][col] = 0;
        }
    }
}

void dealloc_mat()
{
    int row;

    for(row = 0; row < _lines; row++)
    {
        free(_mat[row]);
    }

    free(_mat);
}

Upvotes: 0

Views: 98

Answers (3)

ryyker
ryyker

Reputation: 23208

Here are a couple of functions used to allocate memory for strings, arrays of strings actually, you can easily modify them for your purposes:

char **strings; // created with global scope (before main())   
void allocMemory(int numStrings, int max)
{
    int i;
    strings = malloc(sizeof(char*)*(numStrings+1));
    for(i=0;i<numStrings; i++) 
      strings[i] = malloc(sizeof(char)*max + 1);  
}

void freeMemory(int numStrings)
{
    int i;
    for(i=0;i<numStrings; i++)
        if(strings[i]) free(strings[i]);
    free(strings);  
}

Here is how the above would be modified (and used) for ints: (note, it is really just recognizing the differences in sizeof(type))
Note also: using malloc() does not initialize values. If you want to guarantee an initial value for each element (eg. 0), you can use calloc() instead.

void allocMemoryInt(int rows, int cols);
void freeMemoryInt(int numStrings);
int **array;

int main(void)
{
    allocMemoryInt(10, 3);
    freeMemoryInt(10);
    return 0;   
}

void allocMemoryInt(int rows, int cols)
{
    int i;
    array = malloc(sizeof(int *)*(rows));  //create memory for row pointers
    for(i=0;i<rows; i++) 
      array[i] = malloc(sizeof(int)*cols + 1);  //create memory for (row * col) elements
}

void freeMemoryInt(int rows)
{
    int i;
    for(i=0;i<rows; i++)  
        if(array[i]) free(array[i]);//call free for each row 
    free(array);  //free pointer array(will clean up everything allocated)
}

Upvotes: 0

Crowman
Crowman

Reputation: 25908

The problem is that you're not allocating it correctly. This:

for(i = 0 ; i < _lines ; i++)
    {
        int *tmpMatrix = malloc(sizeof(int) * _columns);
        _mat[i] = &tmpMatrix[i];
    }

should be this:

for(i = 0 ; i < _lines ; i++)
    {
        _mat[i] = malloc(sizeof(int) * _columns);
    }

Further, _mat, _lines and _columns are reserved identifiers in C, and you shouldn't use them. Any identifier beginning with an underscore with file scope in the ordinary (i.e. _mat) or tag (i.e. struct _mat) namespaces is reserved.

Upvotes: 1

rabensky
rabensky

Reputation: 2934

Here's the bug:

_mat[i] = &tmpMatrix[i];

Should be

_mat[i] = &tmpMatrix[0];

or better

_mat[i] = tmpMatrix;

Upvotes: 1

Related Questions