Thread: Why does this program crash when I do this?

  1. #1
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Why does this program crash when I do this?

    Hi I'm a newbie in C programming attending a night course. Right now I'm trying to learn about: arrays, calloc and pointers.
    None of these things has truly sunken in yet, I'm still not comfortable using them.

    Using:
    * Windows Vista 32-bit
    * Bloodshed Dev-C++ v.4.9.9.2
    * 4 GB RAM


    Example code 1: /* Complex_Numbers_01_INT.c */ - [ semi-OK ]
    I have 2 example codes. The first works fine except it can't handle floating-point numbers properly so you can only test it using Integers.


    Example code 2: /* Complex_Numbers_02_LF.c */ - [ BAD ]
    The second example code I basically replaced all INT data types with DOUBLE data types and replaced most %d with %lf when using:
    scanf() and printf()


    The Crash / The Problem
    Only when I run the second example code with the DOUBLE and %lf in the source code does the program crash. You have to run it around 6-10 times before it crashes.
    I'm suspecting it's some kind of memory leak problem but I am too newbie to see it and handle it.
    What am I doing wrong? What have I misunderstood?


    Example code 1: - [ semi-OK ]
    Code:
    /* Complex_Numbers_01_INT.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
      int *tempPointer_a1;
      int *tempPointer_b1;
      int n,i,j;
    
    
      printf("How many complex numbers do you want to input? --> "); scanf("%d",&n);
    /* calloc = Memory allocation for array */
      tempPointer_a1 = (int *) calloc(n,sizeof(int));
      tempPointer_b1 = (int *) calloc(n,sizeof(int));
    
        for(i=0 ; i<n ; i++){
            printf("\n\nInput a complex number Re(a%d)+Im(b%d), for element number <%d>.", i, i, i);
            printf("\ni.e. input 2 floating-point numbers: --> ");   
            scanf("%d %d", tempPointer_a1+i, tempPointer_b1+i );
        }
    
      for(i=0 ; i<n ; i++)
         printf("\n< Element number %d > , Re(a%d)+Im(b%d) = %d + %d (j)", i, i, i, tempPointer_a1[i], tempPointer_b1[i] );
    
      free(tempPointer_a1);
      free(tempPointer_b1);
      printf("\n\n");
    
      system("PAUSE");	
      return 0;
    }

    Example code 2: - [ BAD ]
    Code:
    /* Complex_Numbers_02_LF.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
      double *tempPointer_a1;
      double *tempPointer_b1;
      int n,i,j;
    
    
      printf("How many complex numbers do you want to input? --> "); scanf("%d",&n);
    /* calloc = Memory allocation for array */
      tempPointer_a1 = (double *) calloc(n,sizeof(int));
      tempPointer_b1 = (double *) calloc(n,sizeof(int));
    
        for(i=0 ; i<n ; i++){
            printf("\n\nInput a complex number Re(a%d)+Im(b%d), for element number <%d>.", i, i, i);
            printf("\ni.e. input 2 floating-point numbers: --> ");   
            scanf("%lf %lf", tempPointer_a1+i, tempPointer_b1+i );
        }
    
      for(i=0 ; i<n ; i++)
         printf("\n< Element number %d > , Re(a%d)+Im(b%d) = %lf + %lf (j)", i, i, i, tempPointer_a1[i], tempPointer_b1[i] );
    
      free(tempPointer_a1);
      free(tempPointer_b1);
      printf("\n\n");
    
      system("PAUSE");	
      return 0;
    }
    Last edited by Witch; 03-18-2010 at 06:03 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Double's are usually larger than integers... Therefore your calloc() in the second program is wrong. You simply haven't allocated enough space to store n * sizeof(double).

    To avoid that mistake, I would suggest:
    Code:
    double * x = NULL;
    /* ... */
    x = calloc(n, sizeof *x);
    I would also suggest:
    * Checking the return value of calloc().
    * Don't cast malloc(), realloc(), calloc().
    * Error checking for user input. Passing n straight from scanf() to calloc() is silly.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, the main issue is that in the second example you are doing:

    Code:
    tempPointer_a1 = (double *)(calloc(n,sizeof(int));
    You are allocating space for an int, to a variable that is going to hold a double. It should be sizeof(double).

    Also, stop casting malloc() and calloc(). Search the FAQ or the threads of this forum for the explanation.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, you don't need to cast void pointers in C (such as returned by calloc/malloc) -- the type conversion is automatic.

    You do need to do it in C++, which is why code sometimes appears this way.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Thumbs up

    Roger on the casting tips! I have an old skool teacher.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM