-1

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");
    }
}
The Fluffy Robot
  • 429
  • 1
  • 6
  • 12
  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Apr 25 '16 at 21:14
  • Note that it is [redundant and potentially dangerous to cast the result of malloc and friends in C](http://stackoverflow.com/q/605845/253056). – Paul R Apr 25 '16 at 21:14
  • 2
    Why you redeclare `int **array `? And first malloc should be `malloc(size*sizeof(*int))`. But I think, that debugging must be your way. – Simon Schüpbach Apr 25 '16 at 21:21
  • There is no 2D array in your code and noting which can be used as one. A pointer is not an array! – too honest for this site Apr 25 '16 at 21:22
  • 1
    @WeatherVane: One never can emphasis enough good style ;-) (Sorry, I have missed that comment. Removed mine. – too honest for this site Apr 25 '16 at 21:25

1 Answers1

1

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560