LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-21-2012, 03:24 PM   #1
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Rep: Reputation: Disabled
Question problem in executing c program: glibc detected


Hi all,

I'm trying to execute a program written in c and I get this message:

*** glibc detected *** ./program-name : free(): invalid next size (normal): 0x08a78dc0 ***

While compiling I don't get any error message and I'm sure the problem doesn't come from the program because it's been already used. So that I think that the problem is in the version of glibc that I use.

Here is what I have installed on my ubuntu 11.10
-gcc version 4.6.1
-libglib2.0

I tryed to type "rpm -q glibc" and it says that it's not installed, but when I type "sudo apt-get install glibc" I get the following answer: "impossible to find the package"

I am not an expert, neither with c language nor with Linux, so I have no idea where the problem could be!

Thanks a lot!!

Ashotti
 
Old 05-21-2012, 03:40 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,068

Rep: Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470
the 20 questions part .....
Quote:
I'm trying to execute a program written in c
1)What program?
2)where did you get it ?
3)what command(s) are you using to invoke it ?
Quote:
While compiling I don't get any error message
4)what commands did you use to do that ?
( the build line would be nice )
Quote:
I tryed to type "rpm -q glibc"
WTF ?
boggles the mind
Why would you think that a red hat software installer would run on a Debian based OS ?
Ubuntu DOSE NOT use " Red hat Package Manager "
 
Old 05-21-2012, 03:47 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by Ashotti View Post
I'm sure the problem doesn't come from the program because it's been already used.
You are wrong. The problem you reported is definitely caused by a bug in that program. Bugs don't always have symptoms, so previous testing without seeing this problem means absolutely nothing.

Quote:
So that I think that the problem is in the version of glibc that I use.
You are wrong.

Quote:
I am not an expert, neither with c language nor with Linux, so I have no idea where the problem could be!
Linux expertise is irrelevant to this problem. Expertise in C may be required. In any large program, this kind of bug is hard for a beginner to find. It is especially hard if you start out "knowing" things that aren't true, such as demonstrated in your initial statements about the problem.

If the program is small enough to post the whole source code, then you ought to do so. Any expert can likely spot this kind of bug with just a quick skim through the source code.

If the program is large, you would need to learn how to debug this kind of bug.

Last edited by johnsfine; 05-21-2012 at 03:50 PM.
 
Old 05-21-2012, 03:55 PM   #4
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
the answers:

Quote:
What program?
it's a program for filter data, I don't think it's importan to know what the program is supposed to do

Quote:
where did you get it ?
from the person who used it before, that's why I think it could be a problem with glibc

Quote:
what command(s) are you using to invoke it ?
./program-name

Quote:
what commands did you use to do that ?
(compiling)

I used: gcc program-name.c -o program-name -lm

Quote:
Why would you think that a red hat software installer would run on a Debian based OS ?
because before writing in this forum I tryed to google "how to get the version of glibc" and this is what I found.
How could I be supposed to know that rpm is "Red hat Package Manager" if I'm just a beginner in using Ubuntu?
 
Old 05-21-2012, 04:10 PM   #5
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,068

Rep: Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470Reputation: 2470
Quote:
t's a program for filter data, I don't think it's importan to know what the program is supposed to do
because if it is a 15 year old program it will NOT easily run on a new operating system
( and yes i do sometimes need to run very old fortran and C code )
Quote:
from the person who used it before, that's why I think it could be a problem with glibc
on what operating system did they run this program?
i am betting it was not on ubuntu 11.10

Quote:
gcc program-name.c -o program-name -lm
std. for a one file program that is using lib math


so what it looks like you are experiencing is trying to run a program WROTE for a very different operating system

Quote:
from the person who used it before
ask that person what OS they used

Last edited by John VV; 05-21-2012 at 04:11 PM.
 
Old 05-21-2012, 04:34 PM   #6
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks for all the answers.

The program is not so old because it was run in 2007.

I will definitely ask for the OS that was used.

But now I am a bit confused becaused I don't know if I have to find the root of the problem in the
program or in the differencies between the OS used.

Could Valgrind help me somehow?

Thanks again!
 
Old 05-21-2012, 04:39 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by John VV View Post
because if it is a 15 year old program it will NOT easily run on a new operating system
Do you also not understand the significance of this specific error?

The age and prior OS of the program are not relevant.

There is a specific kind of bug in the program. The bug was there during the prior use. The symptom was apparently not there during the prior use. The OP believes the lack of prior symptom is significant information. For some other type of bug, it might be. For this type of bug, lack of prior symptom is common and is not diagnostically useful.

Quote:
operating system did they run this program?
i am betting it was not on ubuntu 11.10
Not relevant.

Quote:
so what it looks like you are experiencing is trying to run a program WROTE for a very different operating system
Wrong. What the Ashotti is experiencing is a program with a bug in it. That bug was symptom free in prior use due to what would seem (after you understood it) to be a bizarre accident. It is far less bizarre in real life once you consider that this kind of bug is fairly common and ordinarily the programmer would find and fix it. But by some improbable accident the bug was originally symptom free. So the bug was hidden until someone moved the program to a different environment. The different environment is actually irrelevant to the bug, but relevant to the improbable accident that caused the bug to be symptom free. You aren't trying to diagnose the accident that made the bug symptom free. You are trying to diagnose the bug. So the change of environment is not diagnostically useful info.

Quote:
Originally Posted by Ashotti View Post
Could Valgrind help me somehow?
In theory, this is exactly the kind of problem Valgrind is supposed to identify. In practice, my own experience with Valgrind has not been that good. I'm also exceptionally good at diagnosing these by other means. So I would not try Valgrind if it were my problem. But what is right for you is not the same as what is right for me. For you, Valgrind is better than any other specific suggestion I could make with the available info.

BTW, the most common programming error with this symptom is to allocate a buffer and then use slightly more than the whole buffer. If you had moved from a 32 bit OS to a 64 bit OS, then such an error could be from non portable coding making assumptions about the sizes of data types. But I deduce you are using 32 bit Ubuntu, so I don't think the allocation overrun (if there is one) is caused by portability errors in a 32 bit to 64 bit migration.

Quote:
it's a program for filter data, I don't think it's importan to know what the program is supposed to do
That can be important. Best guess, the bug is a buffer overrun. In a program for filtering data, a good follow up guess from a buffer overrun is that the difference (symptom free vs. failing) is in the filename (length of the full directory/.../name string) or content of the data, rather than in the recompile of the program. But guessing is only good if you get lucky. Valgrind or some more expert debugging approach is more reliable.

Last edited by johnsfine; 05-21-2012 at 05:02 PM.
 
1 members found this post helpful.
Old 05-22-2012, 01:48 AM   #8
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks!

Now it's clear why a program that had no problems before is showing some bugs now.

Unfortunately the code is quite long, so I can't post it here.

It allocates and deallocates severale vectors, the problem could be there, anyway the only thing
I can do is to pass through the code and try to find the problem.
I will try to use Valgrind as starting point.

Thanks a lot for all the suggestions!!
 
Old 05-22-2012, 07:11 AM   #9
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
I tried to use Valgrind and I found where the problem is!
The problem is with the malloc function, first the following is defined

coordx=(double *)malloc(N*sizeof(double));
coordy=(double *)malloc(N*sizeof(double));


and few lines after:

coordx[idum]=xbuf;
coordy[idum]=ybuf;



I just changed the code declaring coordx[N] and coordy[N] at the beginning without using the allocatable memory and now everything works properly, but I would be quite surprise if the problem stems simply from the use of the malloc function!

Do you have any suggestion regarding where exactly the problem could be?

I hope that what I want to say is understandable even though I'm not able to use a technical jargon!

Thanks!!
 
Old 05-22-2012, 07:53 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Assuming Valgrind gave you correct info (something I don't completely trust) your change has just changed the symptom, not fixed the bug.

Quote:
Originally Posted by Ashotti View Post
coordx=(double *)malloc(N*sizeof(double));
coordy=(double *)malloc(N*sizeof(double));


and few lines after:

coordx[idum]=xbuf;
coordy[idum]=ybuf;
If Valgrind is correct then idum >= N.

In place of N in the malloc, you need a value at least one greater than the largest value that idum can be.

Is the code setting idum simple enough that you can compute the largest possible value for idum at the point the coord arrays are allocated?

Quote:
I just changed the code declaring coordx[N] and coordy[N] at the beginning without using the allocatable memory
That just changed the symptom. Probably the program now produces a wrong answer instead of the error messages.

Quote:
I would be quite surprise if the problem stems simply from the use of the malloc function!
I'm glad to see you understand that.

Quote:
I hope that what I want to say is understandable even though I'm not able to use a technical jargon!
Perfect info, except that you didn't say where the value of idum comes from.

Often bugs like this are just off by one. If idum is a loop index ending at N such as
for (idum=1; idum<=N, idum++)
Then you just need (N+1) in place of N inside the malloc. Note those () are needed.

Last edited by johnsfine; 05-22-2012 at 08:03 AM.
 
Old 05-22-2012, 08:37 AM   #11
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
First of all, thanks again!

Quote:
That just changed the symptom. Probably the program now produces a wrong answer instead of the error messages.
ok, this is even worse!

Quote:
Often bugs like this are just off by one. If idum is a loop index ending at N such as
for (idum=1; idum<=N, idum++)
Then you just need (N+1) in place of N inside the malloc. Note those () are needed.
idum is an index initialized to zero at the beginning of the loop created to put data into the vectors, here is the part of the code:

idum=0;

while(!feof(coordf))
{
fscanf(coordf,%f,.....,&xbuf,&ybuf,...);
coordx[idum]=xbuf;
coordy[idum]=ybuf;
idum=idum+1;
}

so, if I understood well, I would guess that the problem is in the fact that idum becomes too big, but I have no idea of why this happens.
I tried to put idum=0; exactly at the end of the "while" cycle but it looks like the program doesn't like it.
 
Old 05-22-2012, 09:19 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
That use of feof also looks to me like a bug. That is another common mistake. feof is not true until after the first failure to read at the end of the file. When all the data has been read and the file is positioned at the end, feof is still false.

I think the correct way to code that is:
Code:
while(EOF != fscanf(coordf,%f,.....,&xbuf,&ybuf,...))
{
coordx[idum]=xbuf;
coordy[idum]=ybuf;
idum=idum+1;
}
Where did N come from in the code you posted earlier? Apparently, N was supposed to represent the maximum number of coordinates that might be in the file. Is that a number that would be practical to know when the function starts? Or is the whole design flawed?

If N were correct (though that seems doubtful) the bug with feof would be enough to cause the whole symptom. In which case the correction to the feof bug would fix the whole problem.

I never use fscanf in my own code, so I might be overlooking some other detail in my suggested correction. Hopefully other C programmers are reading this thread. I know that my suggestion doesn't make the program reliable in the face of bad input. I was just aiming for working if the input is good. (My own code always aims for reasonable responses to bad input, which rules out such simplistic uses of fscanf).

Last edited by johnsfine; 05-22-2012 at 09:27 AM.
 
Old 05-22-2012, 09:38 AM   #13
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Where did N come from in the code you posted earlier?
the part of code I posted before is in a function and the value of N is passed when the function is called by the main().
The value of N is unknown before starting the code because it is the size of the file read at the beginning.

I don't know what the feof is exactly, so I just tried to add a condition to the while cycle as follow:

idum=0;
while(!feof(coordf),idum<N)
{
.....
}

in this way it works because idum never gets bigger than N, but it might simply be, as before, that I just bypassed the problem instead of solving it!
 
Old 05-22-2012, 09:49 AM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by Ashotti View Post
The value of N is unknown before starting the code because it is the size of the file read at the beginning.
That sounds like you may be saying the file gets read twice: Once before main calls this function (during which it can correctly compute N) and again inside the function that uses N. If that is how it works, then N could be exactly correct.

Quote:
while(!feof(coordf),idum<N)
That has the same meaning in C as simply:
while(idum<N)
which might be correct.
Quote:
in this way it works because idum never gets bigger than N, but it might simply be, as before, that I just bypassed the problem instead of solving it!
If the value of N the program computed before calling this function was exactly correct, then your change to the while loop will work perfectly (solve the problem rather than bypass it).
 
Old 05-23-2012, 04:23 AM   #15
Ashotti
LQ Newbie
 
Registered: May 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
I checked with several files and the program reads correctly the value of N and the main passes the correct value to the function.
With the condition I put on idum I don't have the problem anymore.

Quote:
If the value of N the program computed before calling this function was exactly correct, then your change to the while loop will work perfectly (solve the problem rather than bypass it).
Wow, I'm really happy for that.

I really appreciate your help and I really thank you!!!!

Ashotti
 
  


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
[SOLVED] C program. error: *** glibc detected *** ./proyecto: malloc(): memory corruption Carlos2313 Programming 4 10-24-2010 04:56 PM
I am getting glibc detected error in my program submission.. strantrier Linux - Newbie 1 12-29-2009 01:45 PM
problem: *** glibc detected *** free(): invalid pointer: shashank929 Programming 2 04-16-2009 02:09 AM
"glibc detected -> program aborted" plz help in debugging.. rs_vijay Programming 6 11-22-2006 12:16 AM
Error running a C program, glibc detected *** free(): invalid next size (normal) mesh2005 Programming 3 11-08-2006 01:52 PM


All times are GMT -5. The time now is 06:58 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