Thread: Core dump using scanf in a function for arrays bigger than a certain length

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    Core dump using scanf in a function for arrays bigger than a certain length

    Good morning guys
    I have tried to write a function which allows to enter an array with scanf(). The array must be bidimensional, something like array[R][3] where R is a length decided by the user, 3 is the number of columns. To allocate memory for the array, I have to use malloc, and it seems to work fine. As you can see in the code below, which is not the final code but contains the function, the function must return the matrix created. So, for R values under 5, the program works. For R=5 and bigger, it returns "segmentation fault (core dump)", and I'd like to understand why this happens.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    float **MatriceInput(int R);
    int main(void){
        int i,R;
        float **M;
        printf("Enter the number of lines\n");
        scanf("%d",&R);
        M=MatriceInput(R);
        for(i=0;i<R;i++){
        printf("%f\t%f\t%f\n",**(M+i),*(*(M+i)+1),*(*(M+i)+2));
        }
        }
        
    
    
    
    
    float **MatriceInput(int R){
        int i,j;
        float **A;
        
        A=(float **)malloc(R*sizeof(float));
        if(A==NULL){
        printf("First malloc failed\n");
        }
        
        for(i=0;i<R;i++){
        A[i]=(float *)malloc(3*sizeof(float));
        if(A[i]==NULL){
        printf("Second malloc failed\n");
        }
        }
        for(j=0;j<3;j++){
        printf("Enter the column %d\n",j+1);
        for(i=0;i<R;i++){
        scanf("%f",&*(*(A+i)+j));
        }
        }
        return(A);
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 is indent your code.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    float **MatriceInput(int R);
    
    int main(void)
    {
      int i, R;
      float **M;
      printf("Enter the number of lines\n");
      scanf("%d", &R);
      M = MatriceInput(R);
      for (i = 0; i < R; i++) {
        printf("%f\t%f\t%f\n", **(M + i), *(*(M + i) + 1), *(*(M + i) + 2));
      }
    }
    
    float **MatriceInput(int R)
    {
      int i, j;
      float **A;
    
      A = (float **) malloc(R * sizeof(float));
      if (A == NULL) {
        printf("First malloc failed\n");
      }
    
      for (i = 0; i < R; i++) {
        A[i] = (float *) malloc(3 * sizeof(float));
        if (A[i] == NULL) {
          printf("Second malloc failed\n");
        }
      }
      for (j = 0; j < 3; j++) {
        printf("Enter the column %d\n", j + 1);
        for (i = 0; i < R; i++) {
          scanf("%f", &*(*(A + i) + j));
        }
      }
      return (A);
    }
    2. Don't cast the return result of malloc in a C program.
    Casting malloc - Cprogramming.com
    If you're getting 'can't cast void*' errors, then you need to stop trying to compile your C program with a C++ compiler.
    Renaming your source files to prog.c would be a good way.

    3. Get the sizes right.
    A = (float **) malloc(R * sizeof(float));
    You're supposed to be allocating pointers here, not floats.
    Eg.
    A = malloc(R * sizeof(float*));

    Or more generally, if you use this form, you never have to look back at the type.
    p = malloc(sizeof(*p));


    4. Normal array syntax works with pointers as well, so there's no need for a mess of * and ()

    Code:
    float **MatriceInput(int R)
    {
      int i, j;
      float **A;
    
      A = malloc(R * sizeof(*A));
      if (A == NULL) {
        printf("First malloc failed\n");
      }
    
      for (i = 0; i < R; i++) {
        A[i] = malloc(3 * sizeof(*A[i]));
        if (A[i] == NULL) {
          printf("Second malloc failed\n");
        }
      }
    
      for (j = 0; j < 3; j++) {
        printf("Enter the column %d\n", j + 1);
        for (i = 0; i < R; i++) {
          scanf("%f", &A[i][j]);
        }
      }
      return (A);
    }
    Extra bonus points for using 'row' and 'col' instead of i,j would further improve things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    Thanks for your clear and detailed answer, the function works properly now

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Cool

    Quote Originally Posted by GiulioDag01 View Post
    Thanks for your clear and detailed answer, the function works properly now
    Core dump using scanf in a function for arrays bigger than a certain length-results-jpg
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hi to over come with core dump
    By vijay85 in forum C Programming
    Replies: 9
    Last Post: 01-14-2009, 11:59 AM
  2. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 12:00 PM
  3. why core dump in this function?
    By kocika73 in forum C Programming
    Replies: 7
    Last Post: 02-17-2005, 10:05 PM
  4. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM
  5. core dump
    By lonbgeach in forum C Programming
    Replies: 1
    Last Post: 04-07-2003, 03:34 PM

Tags for this Thread