4

This might be very silly question to ask.
I am using malloc to memory allocation.
Program compiles fine but segmentation fault occurs while launching.
Here is min code:

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

int main()
{
    int row,col,i;
    int **mat;
    row = 3; //made row as static for testing
    col = 4; //made col as static for testing
    *mat = (int *)malloc(sizeof(int)*row);
    for (i=0;i<row;i++)
       mat[i] = (int *) malloc (sizeof(int)*col);
}

i compiled with: gcc -ggdb test.c
on gdb its giving:

(gdb) run
Starting program: /slowfs/swe91/abjoshi/clients/fw_cl_for_seg_fault/a.out

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004cc in main () at 1test.c:10
10          *mat = (int *)malloc(sizeof(int)*row);

Note: gcc version 4.5.2

2
  • 2
    *mat = (int *)malloc(sizeof(int)*row); mat doesn't point to anything yet. Commented Sep 18, 2013 at 18:21
  • @CharlieBurns: that's what i am assigning memory to pointer then i will store values in that memory location. Please let me know if i am missing something. Commented Sep 18, 2013 at 18:25

5 Answers 5

10

The common malloc idiom is as folows: to allocate an array of n elements pointed by pointer p use the following universal syntax

p = malloc(n * sizeof *p);

That will help you to avoid errors as the one in your code. Note the key points here: don't cast the result of malloc and don't use types under sizeof. And in general: as much as possible, avoid mentioning types anywhere outside declarations.

In your case the first allocation should look as

mat = malloc(row * sizeof *mat);

The allocations inside the cycle should look as

for (i = 0; i < row; ++i)
   mat[i] = malloc(col * sizeof *mat[i]);

The important benefit of this approach is that the code is type-independent: at any moment you can change the declaration of mat from int **mat to, say, double **mat, but the allocation code will continue to remain valid - you won't have to make any changes there.

Sign up to request clarification or add additional context in comments.

1 Comment

If I am assigning the void pointer returned from malloc directly to the array pointer, then I am getting the following error: "error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]". How do I resolve it?
3

Instead of

*mat = (int *)malloc(sizeof(int)*row);

I think you want

mat = (int **)malloc(sizeof(int *) * row);

As it is, you are dereferencing mat which is uninitialized. You need to malloc() mat before you deference it.

You can leave the cast off the malloc as well:

mat = malloc(sizeof(int *) * row);

As well as the sizeof(int *) reference:

mat = malloc( row * sizeof(*mat));

Comments

2

Compiler would have told it with -Wall

$ gcc -Wall y.c
y.c: In function ‘main’:
y.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
y.c:11:8: warning: ‘mat’ is used uninitialized in this function [-Wuninitialized]

Comments

1

In the line *mat = (int *) malloc(sizeof(int)*row); you are trying to dereference mat. mat is currently uninitialized.

Comments

0

Here's what:

mat is a double pointer and it is uninitialized (or NULL). When you dereference it:

*mat = (int *)malloc(sizeof(int)*row);

you are dereferencing a NULL value and trying to assign to it, causing the segmentation fault.

First initialize mat with a pointer to list of (int*) pointers. Then individually take each pointer in the list and allocate (int) space for each.

mat = (int *)malloc(cols*sizeof(int *));
for (i=0;i<cols;i++)
    mat[i] = malloc(rows*sizeof(int))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.