简体   繁体   中英

C - 2D global array -> running into segmentation fault at size>4

My goal: a program that takes in a user specified number to make a global 2D array of their size, with 'size' amount of columns and 'size' amount of rows
This is a smaller part of a larger program I am working on, which requires the array to be global

ex: user runs program with ./a.out 5 The program makes a global array with 5 rows, 5 columns, and outputs it to the user

My Problem: an array can be created of size 0,1,2,3, and 4 without issue. As soon as I run the program with a user input of 5, it gives me a segmentation fault. It appears to have a problem with the last row(s) but I can't understand why it does for inputs>=5

What I've done/tried: Though the array has to be global , I've tried making the array a not global one by putting "int **" in front of the "array = " code. This does not change my problem, so I don't think it has to do with it being global

My questions:

  1. Why is my program giving me a segmentation fault for inputs that are greater than or equal to 5?

  2. How can I get make it accept inputs of larger numbers while still keeping it as a global array?

My code:

#include <stdio.h>
#include <stdlib.h>
//method declarations
void fill_array();
//global variables
int **array;
int size;

int main(int argc, char** argv){
    //fill the array with size specified by user
    //ASSUME THE USER INPUT TO BE A VALID INTEGER
    if(argc==2){
        fill_array(argv);
    }
}

void fill_array(char** argv){

    //initialize the variables
    int i,j;//loop counters

    //set size of array
    size = atoi(argv[1]);

    //make array of size 'size'
    int **array = (int**)malloc(size*sizeof(int));//initialize the array to hold ints
    for(i=0; i<size; i++){
        array[i] = (int*) malloc(size*sizeof(int));//initialize the second dimension of the array
    }

    //fill the array with values of i*j
    for(i=0; i<size; i++){
        for(j=0; j<size; j++){
            printf("i: %d and j: %d ",i,j);
            array[i][j] = i*j;//put a value in the array
            printf("... and we succeeded\n");
        }
    }

    //print the array when we are done with it
    for(i=0; i<size; i++){
        for(j=0; j<size; j++){
            printf("%d ",array[i][j]);
        }
        printf("\n");
    }
}

This line:

int **array = (int**)malloc(size*sizeof(int));//initialize the array to hold ints

should be:

int **array = malloc(size*sizeof(int*));//initialize the array to hold ints
                                   ^^^

Also, this prototype:

void fill_array();

should be:

void fill_array(char** argv);

Furthermore, you should as a general rule avoid globals - move the declarations of size and array inside an appropriate function and pass them as parameters as needed.

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