1

I'm using malloc to allocate memory for a matrix, but afterwards any access I do to the matrix to set an element for example gets me a segmentation fault.

This is what I'm doing right now:

int **matrix = malloc(rows_number * columns_number * sizeof(int));

if (matrix) matrix[0][0] = 1;

Why can't I access the matrix after it was created? The malloc call is successful so I have enough contiguous memory for the whole matrix.

Kao Dome
  • 105
  • 1
  • 1
  • 8
  • 1
    malloc is the least understood, most questioned part of the C language - please search here and elsewhere for a plethora of answers to this and every other imaginable question about this subject. honestly, I can't believe there is a question about malloc that hasn't been asked -- and answered :) – KevinDTimm Dec 19 '12 at 19:47
  • 1
    possible duplicate of [allocate matrix in C](http://stackoverflow.com/questions/2128728/allocate-matrix-in-c) – KevinDTimm Dec 19 '12 at 19:48
  • See the top answer on this question: – BimmerM3 Dec 19 '12 at 19:51
  • @BimmerM3, that top answer is just wrong. This is not a 2D array but a pointer to pointer emulation of a 2D array. C has native support for 2D array, why not use it? – Jens Gustedt Dec 19 '12 at 20:52

4 Answers4

2

int **matrix declares matrix to be a pointer to a pointer to int. You want a pointer to an array of int. This would be int (*matrix)[columns_number]. You can allocate it nicely with:

int (*matrix)[columns_number] = malloc(rows_number * sizeof *matrix);
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I see, thank you for the answer, I marked the other one as the accepted one because it seemed more complete but I like this way of doing it because the access to the matrix is more intuitive. – Kao Dome Dec 31 '12 at 18:48
2

The problem here is that you have allocated just memory, yet you have declared your object as a set of pointers to pointers. The double subscript requires a pointer vector pointing to each row or a type that tells the compiler how many columns there are.

So some options are:

  • do the subscript calculation with code, perhaps using a macro
  • do two mallocs() and initialize a row pointer vector
  • malloc an object of a specific type so the compiler can see it

For example...

int (*matrix)[ROWS][COLS] = malloc(ROWS * COLS * sizeof(int));
...
(*matrix)[i][j]

The following declaration would also work:

int (*matrix)[][COLS] = malloc(ROWS * COLS * sizeof(int));
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • Sorry for replying late, I see what I was doing wrong. I'm marking this answer as the accepted one because it seems to be the most complete. Thank you. – Kao Dome Dec 31 '12 at 18:46
1

You have allocated memory for a dynamic array of int pointers, not int elements. You could try something like this:

int *matrix_storage = malloc(rows_number * columns_number * sizeof(int));
int **matrix = malloc(rows_number * sizeof(int*));
for( int i = 0; i < rows_number; ++i )
    matrix[i] = &matrix_storage[ i * columns_number ];
K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

Malloc returns a void pointer so you haven't made a 2 dimensional array. Try something like:

int **matrix;
matrix = malloc(rows_number * sizeof(int*));
for (int i=0; i<rows_number; i++)
    matrix[i] = (int*)malloc(columns_number * sizeof(int));
Foggzie
  • 9,691
  • 1
  • 31
  • 48