LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2010, 03:30 PM   #1
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Rep: Reputation: 0
C program. error: *** glibc detected *** ./proyecto: malloc(): memory corruption


I was working in this program. It's supposed to calculate the poisson Table. however It has to stop when the result is 1. It is not stopping, so it's giving me the previous error.

void PoissonTable(){
int current=0;
PoissonArr = (float*) malloc (sizeof(float)*10);
float store=0;
while(store < 1 )
{

PoissonArr[current] = Poisson(current);

if(current != 0)
PoissonArr[current] += PoissonArr[current-1];

store = PoissonArr[current];

printf("store %f\n",store);

if(store >= 1)
break;

current++;
}

}
The question is: How can I stop the program if in the cycleI'm already defining that it should stop when the result equals or is bigger than 1??
Thank you !!

Last edited by Carlos2313; 10-24-2010 at 04:04 PM.
 
Old 10-24-2010, 04:14 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You should learn to use CODE tags to post readable code.

Quote:
Originally Posted by Carlos2313 View Post
It is not stopping, so it's giving me the previous error.
I don't think you have posted the right information to let us diagnose the problem.

Code:
while(store < 1 )
Code:
if(store >= 1)
     break;
Quote:
How can I stop the program if in the cycleI'm already defining that it should stop when the result equals or is bigger than 1??
I assume you added a second way to break out of the loop (making the first way redundant) because you didn't think the first one was working.

So you seem to have some mental model of what is happening in your code (that store reaches 1 or higher and the loop keeps going). But you don't seem to really know what is happening. You could use a debugger, or add some temporary printouts, or add some "defensive programming". Any of those is better than guessing.

Code:
PoissonArr = (float*) malloc (sizeof(float)*10);
...
Code:
current++;
For example, defensive programming might follow current++; with
Code:
if (current >= 10 ) {
   printf("Exceeded size of array.  Aborting.\n");
   exit(-1);
}
Maybe a bit drastic (exit instead of return) but you get the idea.

Last edited by johnsfine; 10-24-2010 at 04:17 PM.
 
Old 10-24-2010, 04:22 PM   #3
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
yeap, that's true

..... This is the output I got:

store 0.122456
store 0.379615
store 0.649631
store 0.838643
store 0.937874
store 0.979551
store 0.994138
store 0.998514
store 0.999663
store 0.999931
store 0.999987
store 0.999998
store 1.000000
store 1.000001
*** glibc detected *** /media/NYU/SO
proyect: malloc(): memory corruption: 0x0804c0b8 ***

According the information I have the variable store has the correct result.. However I just don't get it ... I don't know why the program is not stopping

hehe, yes, I think that I am going to have to use that solution... Otherwise the program won't stop
Quote:
if (current >= 10 ) {
printf("Exceeded size of array. Aborting.\n");
exit(-1);
}
Thank you so much !!!

Last edited by Carlos2313; 10-24-2010 at 04:28 PM.
 
Old 10-24-2010, 04:42 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What about just making the array big enough?

Quote:
Originally Posted by Carlos2313 View Post
..... This is the output I got:

store 0.122456
store 0.379615
store 0.649631
store 0.838643
store 0.937874
store 0.979551
store 0.994138
store 0.998514
store 0.999663
store 0.999931
store 0.999987
store 0.999998
store 1.000000
store 1.000001
You allocated room for 10 values.

The 13'th value was very close to 1. It was so close that printf rounded it to 1. But it was less than 1. It probably is the value that would have been 1 if you were computing with infinite precision. Welcome to floating point. It isn't infinite precision.

Then the 14'th value was more than 1 and your loop stopped, just as you programmed it to.

By then you have stored 14 values into an array allocated for 10.

It may be surprising (but a programmer better learn this early) that storing 14 values where there was room for only 10 typically does not crash right at the place the bad stores occur. Typically it crashes later. In complicated code it usually crashes in an obscure way with no obvious connection to the bad stores. In a program this simple it often crashes (as it did for you) in the malloc code either releasing the chunk that was exceeded or the next time you allocate another chunk.

Last edited by johnsfine; 10-24-2010 at 04:46 PM.
 
Old 10-24-2010, 04:56 PM   #5
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Cool o.o

You are so right ! Thank you so much... I don't know how I could not see that

I really appreciate your help

Last edited by Carlos2313; 10-24-2010 at 05:04 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 ***: malloc() memory corruption Dinarchik Programming 5 02-16-2010 05:38 PM
*** glibc detected *** ruby: malloc(): memory corruption: priceey Linux - Software 0 10-16-2009 05:24 PM
*** glibc detected *** /usr/sbin/httpd: malloc(): memory corruption problem - Pawang Linux - General 2 06-27-2009 02:23 AM
*** glibc detected *** malloc(): memory corruption arvind.ayyangar Programming 2 11-20-2006 11:59 PM
glibc detected *** malloc(): memory corruption: abefroman Linux - Software 2 04-12-2006 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

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
Open Source Consulting | Domain Registration