What is a segmentation error?

This is a discussion on What is a segmentation error? within the C Programming forums, part of the General Programming Boards category; What is a segmentation error and what are some possible causes? I'll post code, if necessary, but I not asking ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    What is a segmentation error?

    What is a segmentation error and what are some possible causes? I'll post code, if necessary, but I not asking for someone to do my troubleshooting.

  2. #2
    Unregistered
    Guest
    Most of the time, a segmentation fault is when you try and use a pointer when it is uninitialized, or when you try and write past the end of an array, things like that....anything having to do with memory mostly. if you post the code, we may be able to point it out.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    sorry... Segmentation FAULT

    The code is as follows...

    #include <stdio.h>
    #define MAXEL 10
    double find_max(double [], double);
    double find_min(double [], double);
    double get_min_range(double list[], double first, double last);
    void sel_sort(double list[], int n);


    int main(void){
    double nums[MAXEL];
    double newInfo;
    int i = 0;
    int j = 0;
    int k = 0;
    double sum = 0;


    FILE *input = fopen("lab6.in", "r");

    if (input == NULL)
    {
    printf("Cannot open file.\n");
    return(0);
    }

    for (k = 0; fscanf(input, "%lf", &newInfo) != NULL; j++)
    nums[j] = newInfo;

    fclose(input);



    sel_sort(nums, 10);

    printf("\nThe descending order of the array is:\n\n");
    while( i < 10){

    printf("%6.2f\n", nums[i]);
    i++;

    }
    printf("\nThe maximum value in the array is %6.2f. \n", find_max(nums, 10));
    printf("\nThe minimum value in the array is %6.2f. \n\n", find_min(nums, 10));


    return(0);
    }

    double find_max(double last[], double num_ele){
    int i, max = last[0];
    for(i = 1; i < num_ele; i++)
    if(max < last[i]) max = last[i];

    return (max);
    }

    double find_min(double first[], double num_ele){
    int i, min = first[0];
    for(i = 1; i < num_ele; i++)
    if(min > first[i]) min = first[i];

    return (min);
    }

    double get_min_range( double list[], double first, double last){
    int i, small_sub;
    small_sub = first;

    for(i = first + 1; i <= last; ++i)
    small_sub = i;

    return(small_sub);
    }

    void sel_sort(double *list, int n){
    int temp;
    int i;
    for(i = 0; i < n - 1; i++){
    int max = i;
    int j = 0;

    for(j = i + 1; j < n; j++){
    if(list[j] < list[max]) { max = j; }
    }
    temp = list[max];
    list[max] = list[i];
    list[i] = temp;
    }
    }

    /***********************************************/

    /*** The file lab6.in reads as follows... */

    5.00
    2.00
    3.00
    8.00
    10.00
    7.00
    4.00
    1.00
    0.00
    9.00


    /**** Thanks for your input! *****/

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Is this a typo

    k = 0; fscanf(input, "%lf", &newInfo) != NULL; j++)
    nums[j] =

    you have "K = 0;" and then use J

    There are macros

    min() and max()

    Code:
    for(i=0;i<10;i++)
    {
       fMin=min(fMin,fValue[i]);
    }
    Watch how you pass the arrays. the compiler will not like it if you try to pass too much info NOT as pointers. Try sending ALL the arrays as a pointers

    ie
    prototype
    double find_max(double *, double num_ele);

    call
    fValue=find_max(&last[0], last[0]);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21