c - 2D array memory allocation and passing to a function -


i writing program print square,cube,multiples of 4, multiples of 5 2 dimensional array. getting segmentation fault error. can please explain going on? program not print elements of 2d array. ends in segmentation fault after 4th or 6th row.

#include<stdio.h> #include<stdlib.h>  void numbers(int ***arr,int nrows,int ncols); void square(int ***arr,int nrows,int ncols); void cube(int ***arr,int nrows,int ncols); void multiples_of_4(int ***arr,int nrows,int ncols); void multiples_of_5(int ***arr,int nrows,int ncols); void free_our_ptr(int ***arr,int nrows,int ncols);   int main(void) {      int i=0;      int nrows = 0;     int ncols = 0;      int menu_choice = 0;      int **arr;      printf("\nget number of rows : ");     scanf("%d",&nrows);      if(nrows > 10)     {         printf("\nrows should less 10 ");         exit(0);      }      printf("\nget number of columns : ");     scanf("%d",&ncols);      if(ncols > 10)     {         printf("\ncolumns should less 10 ");         exit(0);      }      /******* dynamic memory allocation array ***********/      arr = (int **) malloc ( nrows * sizeof(int));      for(i=0;i<nrows;i++)     {         arr[i] = (int *) malloc ( ncols * sizeof(int));     }      /********************************************************/      printf("\nthese choices : ");     printf("\nfill integers ---------------- 1");     printf("\nfill squares  ---------------- 2");     printf("\nfill cubes ------------------- 3");     printf("\nfill multiples of 4 ---------- 4");     printf("\nfill multiples of 5 ---------- 5");     printf("\n\nenter choice : ");     scanf("%d",&menu_choice);      switch(menu_choice)     {     case 1:         numbers(&arr,nrows,ncols);         break;      case 2:         square(&arr,nrows,ncols);         break;      case 3:         cube(&arr,nrows,ncols);         break;      case 4:         multiples_of_4(&arr,nrows,ncols);         break;      case 5:         multiples_of_5(&arr,nrows,ncols);         break;      default :         printf("\ninvalid choice, exiting program");         free(arr);         exit(0);     }     exit(0);  }  void numbers(int ***array,int nrows,int ncols) {     int i=0;     int j=0;     int number = 1;     int **arr;     arr = *array;      for(i=0;i<nrows;i++)     {         printf("\n");         for(j=0;j<ncols;j++)         {              arr[i][j] = number;             printf("%d\t",arr[i][j]);             number++;         }     }     free_our_ptr(&arr,nrows,ncols);   }  void square(int ***array,int nrows,int ncols) {     int i=0;     int j=0;     int number = 1;     int **arr;     arr = *array;      for(i=0;i<nrows;i++)     {         printf("\n");         for(j=0;j<ncols;j++)         {              arr[i][j] = number * number;             printf("%d\t",arr[i][j]);             ++number;         }     }     free_our_ptr(&arr,nrows,ncols); }   void cube(int ***array,int nrows,int ncols) {     int i=0;     int j=0;     int number = 1;     int **arr;     arr = *array;      for(i=0;i<nrows;i++)     {         printf("\n");         for(j=0;j<ncols;j++)         {              arr[i][j] = number * number * number;             printf("%d\t",arr[i][j]);             number++;         }     }     free_our_ptr(&arr,nrows,ncols); }  void multiples_of_4(int ***array,int nrows,int ncols) {     int i=0;     int j=0;     int number = 1;     int **arr;     arr = *array;      for(i=0;i<nrows;i++)     {         printf("\n");         for(j=0;j<ncols;j++)         {              arr[i][j] = number * 4;             printf("%d\t",arr[i][j]);             number++;         }     }     free_our_ptr(&arr,nrows,ncols); }  void multiples_of_5(int ***array,int nrows,int ncols) {     int i=0;     int j=0;     int number = 1;     int **arr;     arr = *array;      for(i=0;i<nrows;i++)     {         printf("\n");         for(j=0;j<ncols;j++)         {              arr[i][j] = number * 5;             printf("%d\t",arr[i][j]);             number++;         }     }     free_our_ptr(&arr,nrows,ncols); }  void free_our_ptr(int ***arr,int nrows,int ncols) {     int i;     for(i=0;i<nrows;i++)     {         free(arr[i]);     }     free(arr); } 

i tried suggestions, removed free_our_ptr() function altogether , freed memory in main() itself. on ubuntu linux gcc, gives segmentation fault error. however, on visual studio-windows7, runs fine, no errors in test cases. below modified code.

#include<stdlib.h>  void numbers(int ***arr,int nrows,int ncols); void square(int ***arr,int nrows,int ncols); void cube(int ***arr,int nrows,int ncols); void multiples_of_4(int ***arr,int nrows,int ncols); void multiples_of_5(int ***arr,int nrows,int ncols);  int main(void) {      int i=0;      int nrows = 0;     int ncols = 0;      int menu_choice = 0;      int **arr;      printf("\nget number of rows : ");     scanf_s("%d",&nrows);      if(nrows > 10)     {         printf("\nrows should less 10 ");         exit(0);      }      printf("\nget number of columns : ");     scanf_s("%d",&ncols);      if(ncols > 10)     {         printf("\ncolumns should less 10 ");         exit(0);      }      printf("\nnumber of rows = %d",nrows);     printf("\nnumber of cols = %d",ncols);      /******* dynamic memory allocation array ***********/      arr = (int **) malloc ( nrows * sizeof(int));      for(i=0;i<nrows;i++)     {         arr[i] = (int *) malloc ( ncols * sizeof(int));     }      /********************************************************/      printf("\nthese choices : ");     printf("\nfill integers ---------------- 1");     printf("\nfill squares  ---------------- 2");     printf("\nfill cubes ------------------- 3");     printf("\nfill multiples of 4 ---------- 4");     printf("\nfill multiples of 5 ---------- 5");     printf("\n\nenter choice : ");     scanf_s("%d",&menu_choice);      switch(menu_choice)     {     case 1:         numbers(&arr,nrows,ncols);         break;      case 2:         square(&arr,nrows,ncols);         break;      case 3:         cube(&arr,nrows,ncols);         break;      case 4:         multiples_of_4(&arr,nrows,ncols);         break;      case 5:         multiples_of_5(&arr,nrows,ncols);         break;      default :         printf("\ninvalid choice, exiting program");         free(arr);         exit(0);     }      free(arr);      exit(0);  }  void numbers(int ***array,int num_rows,int num_columns) {     int i=0;     int j=0;      int number = 1;     int **arr;     int r1 = num_rows;     int c1 = num_columns;     arr = *array;      printf("\nnumber of rows = %d",r1);     printf("\nnumber of cols = %d",c1);      for(i=0;i<r1;i++)     {         printf("\n");         for(j=0;j<c1;j++)         {              arr[i][j] = number;             printf("%d\t",arr[i][j]);             number++;         }     }   }  void square(int ***array,int num_rows,int num_columns) {     int i=0;     int j=0;     int number = 1;     int **arr;      int r2 = num_rows;     int c2 = num_columns;      arr = *array;      printf("\nnumber of rows = %d",r2);     printf("\nnumber of cols = %d",c2);      for(i=0;i<r2;i++)     {         printf("\n");         for(j=0;j<c2;j++)         {              arr[i][j] = number * number;             printf("%d\t",arr[i][j]);             ++number;         }     }  }   void cube(int ***array,int num_rows,int num_columns) {     int i=0;     int j=0;     int number = 1;     int **arr;      int r3 = num_rows;     int c3 = num_columns;      arr = *array;      printf("\nnumber of rows = %d",r3);     printf("\nnumber of cols = %d",c3);      for(i=0;i<r3;i++)     {         printf("\n");         for(j=0;j<c3;j++)         {              arr[i][j] = number * number * number;             printf("%d\t",arr[i][j]);             number++;         }     }  }  void multiples_of_4(int ***array,int num_rows,int num_columns) {     int i=0;     int j=0;     int number = 1;     int **arr;      int r4 = num_rows;     int c4 = num_columns;      arr = *array;      printf("\nnumber of rows = %d",r4);     printf("\nnumber of cols = %d",c4);      for(i=0;i<r4;i++)     {         printf("\n");         for(j=0;j<c4;j++)         {              arr[i][j] = number * 4;             printf("%d\t",arr[i][j]);             number++;         }     }  }  void multiples_of_5(int ***array,int num_rows,int num_columns) {     int i=0;     int j=0;     int number = 1;     int **arr;      int r5 = num_rows;     int c5 = num_columns;      arr = *array;      printf("\nnumber of rows = %d",r5);     printf("\nnumber of cols = %d",c5);      for(i=0;i<r5;i++)     {         printf("\n");         for(j=0;j<c5;j++)         {              arr[i][j] = number * 5;             printf("%d\t",arr[i][j]);             number++;         }     }  } 

arr = (int **) malloc ( nrows * sizeof(int)); 

is not right. needs be

arr = (int **) malloc ( nrows * sizeof(int*));                                        ^^^^ array of pointers int. 

the first 1 allocates enough space nrows of ints.
second 1 allocates enough space nrows of int*s.

also, since using c, not c++, don't cast return value of malloc. see do cast result of malloc?

just use:

arr = malloc ( nrows * sizeof(int*)); 

the function free_our_ptr not right either.

void free_our_ptr(int ***arr,int nrows,int ncols) {     int i;     for(i=0;i<nrows;i++)     {         free(arr[i]);              ^^^^ not right     }     free(arr);          ^^^ not right. } 

my suggestion fix change interface to:

void free_our_ptr(int **arr, int nrows, int ncols);                       ^^^^ use int**, not int***. 

then, code in function work. however, you'll need change places gets used.

instead of calling as:

free_our_ptr(&arr,nrows,ncols); 

you'll need call as:

free_our_ptr(arr,nrows,ncols);              ^^^ no & 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -