Mamaliga
Mamaliga

Reputation: 3

Can someone please explain why my program crashes?

I don t get any errors at compilation. The program just crashes when I run it. I tried to print the matrix directly from the generate function and it printed the first line and a bit of the second.

This is my code

void generate(int **a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            a[i][j]=i+j;
        }
    }
}

void print(int **a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(&a);
    print(&a);
}

Upvotes: 0

Views: 83

Answers (2)

Dilip Kumar
Dilip Kumar

Reputation: 1746

1) you are allocating a single dimension memory.

a[i][j]=i+j; //is not valid.

Below is the modified code

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

void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            *a=i+j;
                a++; //increments to next memory location
        }
    }
}

void print(int *a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ",*(a++)); //notice another way of accessing 
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(a);
    print(a); //passing the pointer
    free(a); //Always always practice to free the allocated memory, don't ever do this mistake again
    return 0;
}

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

Two things:

First, int ** denotes a pointer to a pointer that points to an int, not a pointer that points to an array of ints.

Second, when you just pass a pointer to some data structure, e.g. an 4x5 array of integers, then the compiler cannot derive the layout of this data structure. I.e. a statement like a[i][j] would require that the compiler "knows" that each row i consists of 4 columns j, such that it can calculate the "place" to which the value should be stored, i.e. a + (4*i) + j. The compiler simply does not know the nr of columns per row, i.e. 4.

To overcome this while keeping the size of the array at least potentially variable (note that "4" and "5" are still hard coded in the function), you could do the following:

void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            *(a+(i*4+j)) = i+j;
        }
    }
}

void print(int *a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ", *(a+(i*4+j)));
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(a);
    print(a);
}

Upvotes: 0

Related Questions