LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2009, 11:38 AM   #1
oneputt
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
*** 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 */
 
Old 04-14-2009, 05:49 AM   #2
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
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.
 
Old 04-14-2009, 02:39 PM   #3
maresmasb
Member
 
Registered: Apr 2009
Posts: 108

Rep: Reputation: 24
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.

Last edited by Tinkster; 10-30-2010 at 04:26 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
*** glibc detected *** double free or corruption vvenugop Linux - General 0 02-19-2008 10:11 AM
glibc detected *** double free or corruption (!prev): krishna_162005 Linux - Server 2 06-29-2007 09:26 AM
*** glibc detected *** double free or corruption (!prev): krishna_162005 Linux - General 1 06-27-2007 03:43 AM
glibc detected *** double free or corruption (!prev): krishna_162005 Linux - Software 1 06-27-2007 03:42 AM
*** glibc detected *** double free or corruption (C++) urzumph Programming 25 01-07-2006 05:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration