1

I'm trying to create a 2d array, specifically an adjacency matrix for directed graphs. I've never tried this with dynamic memory allocation before and I've hit a snag. Here's the code:

int n, i;
printf("Number of nodes is: ");
scanf("%d", &n);

int ** array = malloc(n * sizeof(int*));
for(i = 0; i < n; i++)
    array[i] = malloc(n * sizeof(int));

printf("Number of edges is: ");
scanf("%d", &m);

int x, y;
for(i=0;i<m;i++)
{
    scanf("%d %d", &x, &y);
    array[x][y]=1;
}

As soon as I finish entering all the edges, the program stops working and throws the usual "exe has stopped working".

Where's the problem?

EDIT: houssam spotted my error. The first "for" should have gone from 1 to n. When I entered 1 6 as an edge, the program crashed because there were only nodes 0-5. Thanks for spotting that. Such a simple mistake.....

hornet07
  • 47
  • 7
  • your forget to `free` ? – Emadpres May 12 '15 at 17:03
  • Where exactly does it crash? Could you mark the point at the code? Also, did you validate your own input? – Zach P May 12 '15 at 17:08
  • 2
    is it possible that you entered some wrong "x y" values? – Daniele May 12 '15 at 17:08
  • 1
    Could you give the whole program? Nothing here (other than out of bounds inputs) would cause a crash.. – mtijanic May 12 '15 at 17:12
  • You probably also want to explicitly initialize each `array[i][j]` to 0, either with another loop or by using `calloc`. – Jeff Ames May 12 '15 at 17:17
  • How do you know you've allocated enough space when you don't do boundary checking anywhere? Your `scanf("%d %d", &x, &y);` could read values that are outside the your array's allocated memory. Then when you `array[x][y]=1;` you could be scribbling all over the place. – pedwards May 12 '15 at 17:19
  • As written I don't see any obvious problems; agree that you need to validate your inputs. If your compiler supports VLAs, you can simplify the allocation as `int (*array)[n] = malloc( sizeof *array * n );` - this will allocate a *contiguous* `n` by `n` block of memory. If your compliler doesn't support VLAs, then you're kind of stuck with the current method. – John Bode May 12 '15 at 17:25
  • The program will segfault as written if you enter a number larger than `node` is set to during the `scanf("%d %d", &x, &y);` – pedwards May 12 '15 at 17:32
  • @hornet07: you are welcome, I have already modified the answer to handle errors in the input. – houssam May 12 '15 at 17:49

1 Answers1

1

you may entered wrong values for x or y , their values should be less than n and greater than or equal to 0:

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

int main()
{
    int m ,n, i;
    printf("Number of nodes is: ");
    scanf("%d", &n);

    int ** array = malloc(n * sizeof(int*));
    for(i = 0; i < n; i++)
        array[i] = malloc(n * sizeof(int));

    printf("Number of edges is: ");
    scanf("%d", &m);

    int x, y;
    for(i=0;i<m;i++)
    {
        scanf("%d %d", &x, &y);
        if (x >=0 && y >= 0 && x < n && y < n) // here
            array[x][y]=1;
        else
            printf("error in your values x=%d y=%d\n" , x , y);
    }
    return 0;
}

EDIT #1:
based on comment from user : M Oehm to check the return value of scanf , see:
scanf fails why? , I can refactor the code to be like:

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

int main()
{
    int m ,n, i;
    printf("Number of nodes is: ");
    scanf("%d", &n);

    int ** array = malloc(n * sizeof(int*));
    for(i = 0; i < n; i++)
        array[i] = malloc(n * sizeof(int));

    printf("Number of edges is: ");
    scanf("%d", &m);

    int x, y;
    for(i=0;i<m;i++)
    {
        //int num_read = scanf("%d %d", &x, &y);
        if(scanf("%d %d", &x, &y) < 2)
        {
            printf("please enter valid two integers..\n");          
            while (getchar() != '\n'); // read all characters in the input stream.
        }
        else if (x >=0 && y >= 0 && x < n && y < n) // here
        {
            array[x][y]=1;
            printf("array[%d][%d]=1;\n" , x, y);

        }
        else
            printf("error in your values x=%d y=%d\n" , x , y);
    }
    return 0;
}
Community
  • 1
  • 1
houssam
  • 1,823
  • 15
  • 27