2

When allocating memory for the 2-dimensional array using malloc(),segmentation fault occurs when the input size(matrix N*N) is more than 5 (i.e., N>5).

The below code is working fine for inputs(N) less than 5.

Could you please help me out in figuring the problem?

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

int main(){
    int n;
    int i,j;
    int **adj;
    //reading size of a N*N matrix
    scanf("%d",&n);

    //dynamically allocating memory for a 2-dimensional array
    adj=(int**)malloc(sizeof(int)*n);
    for(i=0;i<n;i++){
        adj[i]=(int*)malloc(sizeof(int)*n);
    }

    //taking input from the file        
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&adj[i][j]);
        }
    }

    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            printf("%d\t",adj[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Littm
  • 4,923
  • 4
  • 30
  • 38
Ranadheer
  • 75
  • 1
  • 5
  • If N is constant per-array, it would be better if you realign it to one-dimentional array and calculate element index by simple multiplication by N. Beyond that, it should be `adj=(int**)malloc(sizeof(int*)*n);` – keltar Sep 25 '12 at 09:02
  • I hope it just was a bad copy-paste? – Alexey Frunze Sep 25 '12 at 12:49

3 Answers3

2

This is incorrect:

adj=(int**)malloc(sizeof(int)*n);

as you are allocating an array of int*, not int. Change to:

adj = malloc(sizeof(int*)*n); /* Cast unnecessary. */
/* or: adj = malloc(sizeof(*adj)*n); */

Recommend checking return value of scanf() to ensure an int was correctly read:

if (1 == scanf("%d", &n))
{
}

Useful read: Do I cast the result of malloc?

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 1
    To help the OP understand why, it's because on a 64-bit platform pointers are 64 bits (i.e. 8 bytes) while `int` is 32 bits (i.e. 4 bytes). So calling `malloc` with `sizeof(int)` means that the allocated memory will be _half_ of whats needed. – Some programmer dude Sep 25 '12 at 09:14
  • Size of integer on a machine depends on the combination of underlying OS and architecture. If OS (plus GCC) is 64 bits on a 64 bit machine, integer as well as a pointer too takes 8 bytes (64 bits)... isn't it? – Ranadheer Sep 25 '12 at 18:52
0
adj=(int**)malloc(sizeof(int)*n);

Replace this with

adj = (int**)malloc(sizeof(int*) * n);

Because in general sizeof(int*) != sizeof(int)

And another tip: initialize your variables!!!

Anton Guryanov
  • 12,109
  • 1
  • 15
  • 16
0

In addition to that what hmjd pointer out, perhaps it may provide few more information.

int p -> p is of type int.

int * p -> p is pointer (*p) type pointing to 'int"

int ** p -> p is pointer (*p) type pointing to "pointer to int ie: (int *)"

If it is read from left to write, it will be easier to understand.

int ** adj -> you need two pointer dereferences ( **) to reach the value 'int'.

ie: each location in the memory array pointed by 'adj', should hold the address of another chunk of memory. Hence you need to provide sizeof(int *), while allocating 'adj'.

Whoami
  • 13,930
  • 19
  • 84
  • 140