简体   繁体   中英

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

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

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, eg an 4x5 array of integers, then the compiler cannot derive the layout of this data structure. Ie 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, ie a + (4*i) + j . The compiler simply does not know the nr of columns per row, ie 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);
}

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