I have to create a matrix with the width and height determined by two parameters obtained from the writing of a file. But, in some cases when the matrix is too big, I've got a segmentation fault
. I think is probably because I'm creating the matrix in a static way, so I need to create it dynamically, but is here where my problem appears, because I don't know how to do it.
My code right now is this:
FILE * fp;
unsigned int width=0;
unsigned int height=0;
//Open the file. argv[4] parameter contains the file
fp=fopen (argv[4],"r");
//Go to the last position which indicates the size
fseek(fp, 0, SEEK_END);
//Return to the start:
rewind(fp);
//The value of the first 4 bytes represent the width
size_t return1 = fread(&width,4,1,fp);
//The value of the next 4 bytes represent the height
size_t return2 = fread(&height,4,1,fp);
//Matrix creation
if (return1 > 0 && return2 > 0) {
unsigned int matrix[width][height];