*** glibc detected *** big: double free or corruption (out): 0x09138008 ***
Im really not sure what to do here. I have tried to figure it out but the same message keeps coming up.
CODE: #include <stdlib.h> #include <math.h> int main () { /* main */ /* *********************** * Declaration Section * *********************** */ /* ************* * Constants * ************* */ /* This is the minumum value that can be input. */ const int min_value = 1; /* This is the program failure code. */ const int program_failure_code = -1; /* This is the program success code. */ const int program_success_code = 0; /* This is the initial sum. */ const int initial_sum = 0; /* What needs to be subtracted from the array. */ const int last_term = 1; /* This if for the harmonic mean. */ const int inverse = 1; /* ************* * Variables * ************* */ /* This is the independent variable. */ float* independent_variable = (float*)NULL; /* This is the dependent variable. */ float* dependent_variable = (float*)NULL; /* These are the variable names for each thing being calculated. */ float indep_arithmetic_mean, indep_root_mean_square, indep_harmonic_mean; float dep_arithmetic_mean, dep_root_mean_square, dep_harmonic_mean; float indep_sum, dep_sum, harmonic_indep_sum, harmonic_dep_sum; float rms_indep_sum, rms_dep_sum; /* This is the linear least squares regression. */ float linear_least_squares_regression; /* This is the first value. */ float first_value; /* This is the input value. */ int list_length; /* This is the index. */ /* This is the index. */ int index; /* ***************** * Input Section * ***************** */ /* Gets the input from the file for the list length. */ scanf("%d", &list_length); if ( list_length < min_value ) { printf("ERROR: That is not a valid input.\n"); exit(program_failure_code); } /* if ( value < min_value ) */ else if (list_length == 0) { printf("ERROR: That is not a valid input.\n"); exit(program_failure_code); } /* if(list_length == 0) */ /* This is allocating the independent variable. */ independent_variable = (float*)malloc(sizeof(float) * list_length); if (independent_variable == (float*)NULL) { printf("ERROR: Can not allocate.\n"); exit(program_failure_code); } /* if (value == (float*)NULL) */ /* This is alloating the independent variable. */ dependent_variable = (float*)malloc(sizeof(float) * list_length); if (dependent_variable == (float*)NULL) { printf("ERROR: Can not allocate.\n"); exit(program_failure_code); } /* if (value == (float*)NULL) */ for (index = first_value; index < (list_length - last_term); index ++) { scanf("%f %f", &independent_variable[index],&dependent_variable[index]); } /* for index */ /* *********************** * Calculation Section * *********************** */ /* Arithmetic mean for independent variable */ for (index = initial_sum; index < list_length; index ++) { indep_sum = indep_sum + independent_variable[index]; } /* for index */ indep_arithmetic_mean = (indep_sum / list_length); /* Arithmetic mean for dependent variable */ for (index = initial_sum; index < list_length; index ++) { dep_sum = dep_sum + dependent_variable[index]; } /* for index */ dep_arithmetic_mean = (dep_sum / list_length); /* Root mean square for independent variable. */ for (index = initial_sum; index < list_length; index ++) { rms_indep_sum += (independent_variable[index] * independent_variable[index]); } /* for index */ indep_root_mean_square = sqrt(rms_indep_sum / list_length); /* Root mean square for dependent variable. */ for (index = initial_sum; index < list_length; index ++) { rms_dep_sum += (dependent_variable[index] * dependent_variable[index]); } /* for index */ dep_root_mean_square = sqrt(rms_dep_sum / list_length); /* Harmonic Mean for independent variable */ for (index = initial_sum; index < list_length; index ++) { harmonic_indep_sum += (inverse / independent_variable[index]); } /* for index */ indep_harmonic_mean = (list_length / harmonic_indep_sum); /* Harmonic Mean for dependent variable */ for (index = initial_sum; index < list_length; index ++) { harmonic_dep_sum += (inverse / dependent_variable[index]); } /* for index */ dep_harmonic_mean = (list_length / harmonic_dep_sum); /* ****************** * Output Section * ****************** printf("%d\n", list_length); printf("%f indep arithmetic mean\n", indep_arithmetic_mean); printf("%f dep arithmethic mean\n", dep_arithmetic_mean); printf("%f indep root mean square\n", indep_root_mean_square); printf("%f dep root mean square\n", dep_root_mean_square); printf("%f indep harmonic mean\n", indep_harmonic_mean); printf("%f dep harmonic mean\n", dep_harmonic_mean); free(independent_variable); independent_variable = (float*)NULL; free(dependent_variable); dependent_variable = (float*)NULL; } /* main */ |
At some point your program accesses memory which it didn't allocate. It is most likely due to uninitialized variables when you access the elements in your dynamically allocated array. I was able to fix your program by doing nothing except initializing variables.
1) You have a declaration for "float first_value"; You don't initialize it (what it means is that first_value currently holds a **random** value). You then use this variable to loop over your array: for (index = first_value; index < (list_length - last_term); index ++) {....} Your loop should start at element 0 and continue while index < list_length. The loop will exit as soon as index == list_length, so no need to loop until list_length - last_term. 2) You also declare lots of "sum" variables: float indep_sum, dep_sum, harmonic_indep_sum, harmonic_dep_sum; None of these sums is initialized to 0. This won't cause a crash but could potentially result in incorrect calculations. 3) Besides that you should also #include <stdio.h> (needed for scanf), and notice that you never return program_success_code in the end of your main() function. You also have "/*", which starts a comment block, without terminating it with "*/". Plus other small errors... To avoid pitfalls, treat warnings as errors. Every warning for unused variables etc' could hint you of a real bug. You can also use -Wall on the command line when compiling. |
The glibc memory errors is quite often related to memory allocated on the stack of the function, instead of being dynamically allocated by malloc and siblings. This allows for a wide range of "strange" errors that are quite difficult to trace.
Make sure that every variable has a known value when defined, and every pointer leads to a known memory location, before being used. Run the program via 'ddd' to debug it and to find out where it fails. |
All times are GMT -5. The time now is 05:14 AM. |