5 Replies - 2569 Views - Last Post: 05 September 2009 - 05:59 AM Rate Topic: -----

#1 orgovaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-September 09

double free or corruption error (when generating Gauss-noise)

Posted 04 September 2009 - 01:38 PM

Hi.
I'm trying to generate Gaussian noise the way i was adviced to.
(By summing up N random numbers in a range then divide by N.)
At first I want to have 100 normally distributed random numbers.
Problem is, when running my code im getting error message:

"double free or corruption (top): 0x000000000137b010".

I dont know why. Im getting the first 2 rnd nums on screen,
and only the first one in file, then comes the error message.
If anybody knows whats wrong with the code pls let me know.
Heres the code:

FILE *output;

main ()
{
 
  srand( time(NULL) );					//		
 

  int NoRN=10;						// No. of rnd nums generated in one ciclus
  int A [NoRN];						// vector
  int i, j;						//
  int SUM;						// will be sum of 50 rnd nums in each c.
 

  output=fopen("data.dat", "w");			//open file for output
 
  for(j=1; j<=100; j++)					// getting 100 normally d. rnd nums
  {
		  SUM=0;					//
   
		for (i=1; i<=NoRN; i++)			// generates 10 rnd nums btw 1 & 100
		{
			A[i] = rand() % 100 + 1;   
			SUM += A[i];			// adding the generated 50 nums
		   
		}
   
	fprintf(output, "%d\n", SUM/NoRN);		 // getting normally distributed rnd nums written in file
	  printf("%d\n", SUM/NoRN);			// getting normally distributed rnd nums written on screen
 
  fflush(output);
  fclose(output);
}
  return 0;
}

This post has been edited by orgovaan: 04 September 2009 - 01:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: double free or corruption error (when generating Gauss-noise)

#2 VentiLator  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 07-March 09

Re: double free or corruption error (when generating Gauss-noise)

Posted 04 September 2009 - 02:12 PM

maybe (just a guess) the rand generates a decimal and cant save it in your int... try using doubles!

*edit: okay, guess i was wrong^^ sorry for that.

This post has been edited by VentiLator: 04 September 2009 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7014
  • View blog
  • Posts: 14,665
  • Joined: 16-October 07

Re: double free or corruption error (when generating Gauss-noise)

Posted 04 September 2009 - 02:24 PM

You're asking for an index that is undefined.

// ok
int A [NoRN];
// all arrays are zero indexed
// So A has indexes 0..NoRN-1


for (i=1; i<=NoRN; i++) {
// You will ultimately ask for A[NoRN]
// that value doesn't exist
            A[i] = rand() % 100 + 1;  

// you want 
for (i=0; i<NoRN; i++) {


Was This Post Helpful? 0
  • +
  • -

#4 orgovaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-September 09

Re: double free or corruption error (when generating Gauss-noise)

Posted 05 September 2009 - 01:42 AM

Well, that's certainly an error in the code - or it should be... however, I fixed it and it's not the one which
causes the double free or corruption error and abort. (after getting the first two random numbers on
screen.) I guess it's something with the first (outer) 'for' ciclus but have no closer ideal what concretly it is.


View Postbaavgai, on 4 Sep, 2009 - 01:24 PM, said:

You're asking for an index that is undefined.

This post has been edited by orgovaan: 05 September 2009 - 01:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7014
  • View blog
  • Posts: 14,665
  • Joined: 16-October 07

Re: double free or corruption error (when generating Gauss-noise)

Posted 05 September 2009 - 04:44 AM

Looking closer, you seem to be closing your file inside your loop. So, after the first spin, you will get an error.

Also, I couldn't figure out why you needed an array in the first place. It also looked like C with shades of C++. I went with C.

This worked:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


double processTest(int NoRN) {
	int i, sum=0;
	for (i=0; i<NoRN; i++) {
		sum += rand() % 100 + 1;   
	}
	return (double)sum/(double)NoRN;
}

#define SIZE 100
int main () {
	FILE *output;
	int i;
	int NoRN = 10;

	srand( time(NULL) );
	
	output=fopen("data.dat", "w");
	for(i=0; i<SIZE; i++){
		double result = processTest(NoRN);
		fprintf(output, "%f\n", result);
		printf("%f\n", result);
	}
	fflush(output);
	fclose(output);
	
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#6 orgovaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-September 09

Re: double free or corruption error (when generating Gauss-noise)

Posted 05 September 2009 - 05:59 AM

Thanks a lot, baavgai, it was really helpful.
The 'irregularities' You've seen in my code are because of... well I guess it exlpains it...
I'm 'learning' C programming from the internet. Sometimes it's really confusing.

And the array thing... well it was just me. (: There was no use of it. And besides that,
the wrong usage of it caused the Double Free or Corruptinon Error, Aborted.
Leaving the whole array thing out and writing just SUM += rand() % 100 + 1 in the
inner for ciclus the program works fine also.

again,
thanks for help,
much obliged.

This post has been edited by orgovaan: 05 September 2009 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1