简体   繁体   中英

segmentation fault in during dynamic memory allocation with malloc

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

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.

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));

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]

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM