Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Segmentation Fault

    Hey guys I have this program that gives me "Segmentation Fault". I have posted the code below please can anyone tell me what's wrong ?....I am compileing it in munro using gcc.

    Thanks

    Code:
    #include<stdio.h>                /*Including Header Files */ 
    #include<stdlib.h>
                                     /*Function Prototypes */    
    void allocat(float *ptr_x,int size);
    void read_num(float *ptr_x, int size);
    float average(float *ptr_x,int size);
    float range(float *ptr_x,int size);
    void display(float *ptr_x, int size,float mean,float rang);
    
    /* Main Function Starts here */
    int main(int argc, char *argv[]) /* Passing Comand line Arguments */
       {
    
        int size = atoi(argv[1]);   /* Converting char string to integer */
        float rang;
        float *ptr_x;
        float mean;
        allocat(ptr_x,size);
        read_num(ptr_x,size);      /* Function Calls */   
        mean = average(ptr_x,size);
        rang= range(ptr_x,size);
        display(ptr_x,size,mean,rang);
     
        return 0;                 /* Returning 0 */
       }
    
     /*********************************************
     *  Purpose: To dynamically allocate the memory for values  *
     * Pre Condition: Passing the pointer for memory allocation  *
     *                       and passing the integer size of array    	 *
     * Post Condition: None                                   		 *
    **********************************************/ 
    void allocat(float *ptr_x,int size)
      {        /* allocating the memory dynamically by malloc */
       ptr_x = (float*)malloc(size * sizeof(float)); 
      }        /* type cast to avoid void pointers */
    
     /*********************************************
     *   Purpose: To read the values entered from the keyboard  *
     * Pre Condition: Passing the pointer for memory allocation  *
     *                and passing the integer size of array     	 *
     * Post Condition: None                                    	 *
     *********************************************/
    void read_num(float *ptr_x,int size)
      {
       int i;
       printf("Enter the Values:");
       for(i=0; i<size; i++)
        {
         scanf("%f",&ptr_x[i]);
       }
      }
    /*********************************************
     * Purpose: Calculating the average of the  values entered    *
     *                from the keyboard                                           *
     * Pre Condition: Passing the pointer for array                     *
     *                        and passing the integer size of array     	*
     * Post Condition: Returning the average as float          	*
     *********************************************/
    float average(float *ptr_x,int size)
     {
      int i;
      float sum = 0;
      float mean;
      for(i=0; i<size; i++)
       {
        sum += ptr_x[i]; 	/* taking the sum */  
       } 
      mean = sum/size;     /* calculating the average */
      return mean;            /* returning the average */  
     }
    
    /*******************************************
     *Purpose: Calculating the range of the  values entered     *
     *               from the keyboard                                         *
     * Pre Condition: Passing the pointer for array                  *
     *                       and passing the integer size of array        *
     * Post Condition: Returning the range as float                  *
    ********************************************/
    float range(float *ptr_x,int size)
     {
      int i;
      float rang;
      float temp[2];
      temp[0] = ptr_x[0];
      for(i=0; i<size; i++)
       {
        if(temp[0]<ptr_x[i])
          {
           temp[0] = ptr_x[i];             /* largest value */
          }
       }
      for(i=0; i<size; i++)
       {
        if(temp[1]>ptr_x[i])
         {
          temp[1] = ptr_x[i];              /* Smallest Value */
         }
        }
      rang = temp[0] - temp[1];      /* calculatin the range */
      return rang;                            /* returning the range */
     }
    
    /********************************************                                       
     * Purpose: Displaying the values, the average of the         *
     *               values, the range of the values entered            *
     *               from the keyboard                                         *
     * Pre Condition: Passing the pointer for array, its average*
     *                        and its range and passing the integer      * 
     *                        size of array                                          *
     * Post Condition: None                                   	               *
    ********************************************/
    void display(float *ptr_x, int size,float mean,float rang) 
     {
      int i;
      printf("The entered Values:\n");
      for(i=0; i<size; i++)
       {
        if((i%5)==0) printf("\n");
           printf("%6.2f",ptr_x[i]);
       }   
      printf("\nThe average is:%f\n",mean);
      printf("The range is    :%f\n",rang);
     }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can you give us a hint on where the seg fault occurs? It could be happening on the very first line of your main() function.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Actually it doesn't give me the line number. All it says is Segmentation fault when i try to run it. I guess if I had Visual C++ I can stop it and see what line is it, but unfortunatley I don't have that ...so it there another way to find out which line is it?.

    Thanks for any help

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'll be it happens right after you call allocat. Here's why:
    Code:
    void allocat(float *ptr_x,int size)
      {        /* allocating the memory dynamically by malloc */
       ptr_x = (float*)malloc(size * sizeof(float)); 
      }
    This does NOT assign memory allocated to 'ptr_x' outside of this function. It instead causes a memory leak, by basicly throwing away all memory allocated immediately after the function ends.

    You need to change this to use a pointer to a pointer, or return the memory allocated. Why on earth you don't simply call malloc instead of this function I don't know.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    void allocat(float *ptr_x,int size)
      {        /* allocating the memory dynamically by malloc */
       ptr_x = (float*)malloc(size * sizeof(float)); 
      }        /* type cast to avoid void pointers */
    This is wrong twice
    1. Do not cast malloc in C (there's a FAQ on the topic, read it)
    2. This does NOT result in a change to the value of ptr_x in the calling function.
    You allocate memory here, and then it immediately leaks when the function returns.

    You need to do something like
    Code:
    float *allocat(float *ptr_x,int size);
    
    /* in main */
    ptr_x = allocat(size);
    
    /* definition */
    float *allocat(int size)
    {        /* allocating the memory dynamically by malloc */
       float *ptr_x;
       ptr_x = malloc(size * sizeof(float)); 
       return ptr_x;
    }
    > I am compileing it in munro using gcc.
    If you mean Linux, then you do
    Code:
    gcc -g prog.c
    gdb a.out
    gdb is the command line debugger. Type 'help' to get some basic information.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks guys gonna try that and let you know.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    <looks at Quzah's answer>
    Ooooh - deja vue
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM