GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 24 votes, 5.00 average.
  #1  
Old 14-Jun-2006, 14:27
george89 george89 is offline
New Member
 
Join Date: Jun 2006
Posts: 21
george89 is on a distinguished road

A new error message: glibc detected *** free(): invalid pointer: where should I begin


I am making a calculator program, and when I compile it with the following:
Code:
g++ -g calc.cpp -Wall -Wextra -o calc
I get absolutely no errors, just a warning about a function returning nothing when not a void. That actual warning is:
Code:
warning: control reaches end of non-void function
Though, I fixed the problem easily, I did run the problematic version of the program and it gave me the following when it came to that function.

Code:
*** glibc detected *** free(): invalid pointer: 0xb7f4fc90 *** Aborted

Here is the actual function before it was fixed to a void return type:

CPP / C++ / C Code:
std::string output()
{
    for (int i = 0; i < uid.szlength; i++)
      {
          std::cout << uid.data[i] << std::endl;
      }
}

All it did was output the data out onto the terminal, but didn't return a string. Now my question is that I do not know what exactly the error meant. If I were to encounter that error, what should I be looking for? I did gdb with my program and when it reached that function it gave me the following:

Code:
40 output(); (gdb) next 12341 2 3 4 *** glibc detected *** free(): invalid pointer: 0xb7f4fc90 *** Program received signal SIGABRT, Aborted. 0xffffe410 in __kernel_vsyscall ()

Is there a man page I can read (I did g++, but more or less skimmed through the parts that weren't c or c++ related.) describing errors? Or if there are too many, frequenty encountered ones? If not, then what should I do to figure out errors such as the one above? Where would I begin to look in my program?
Last edited by LuciWiz : 15-Jun-2006 at 00:04. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 14-Jun-2006, 16:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A new error message: glibc detected *** free(): invalid pointer: where should I b


Quote:
Originally Posted by george89
I compile it with the following:
Code:
g++ -g calc.cpp -Wall -Wextra -o calc
First of all: congratulations for turning on the error messages. I'm always surprised at things that don't get reported when I don't use -Wall (I usually use "-Wall -pedantic", and sometimes I throw in "-ansi" just for the heck of it.)
Quote:
Originally Posted by george89
That actual warning is:
Code:
warning: control reaches end of non-void function

People with lots more experience than myself always seem to know what warnings to ignore and which ones to fix. I just fix 'em all. That way significant ones (like this one) don't get lost in the chaff of hundreds of "warning: comparing signed and unsigned quantities" or some such (usually) benign ones.

Quote:
Originally Posted by george89
Though, I fixed the problem easily, I did run the problematic version of the program and it gave me the following when it came to that function.

Code:
*** glibc detected *** free(): invalid pointer: 0xb7f4fc90 *** Aborted

Knowing about gdb and how to use it is very valuable, and, again, I congratulate you for your efforts.

Here's the thing: If the code expects something to be returned from the function (and it does if your function prototype said that it was going to return a string) then it reads something from somewhere (stack? register? whatever). This might have bad side effects (or, maybe, not). It may cause other program bugs to manifest themselves whereas they were previously, just by chance, rendered harmless. It could happen; I've seen it happen. If I had a nickel for every time someone told me, "I only changed one thing and it couldn't possibly cause this error", but it did, then I would have ... well, more than a nickel.

Quote:
Originally Posted by george89
Here is the actual function before it was fixed to a void return type:
Meaningful detective work via remote control would require more context (like, maybe, the entire program) --- But that's not an invitation for you to post the whole buggy thing. I note that you are using global variables for the data and the length (something that makes programs hard to debug ---with or without gdb). I, personally, won't bother with debugging stuff that I know is filled with gratuitous use of globals and is run with ignored warnings, etc.

That doesn't mean that I think it's a Bad Thing to try to understand the error messages, and I think I understand the reason for your questions. After all, if you "get it" this time, then if (when) you have a really hard time with a program some day and you see that error message, maybe you will have a clue as to where to look. Fair enough. (See footnote.)
Quote:
Originally Posted by george89

Now my question is that I do not know what exactly the error meant. If I were to encounter that error, what should I be looking for?

Code:
*** glibc detected *** free(): invalid pointer: 0xb7f4fc90 *** Program received signal SIGABRT, Aborted. 0xffffe410 in __kernel_vsyscall ()

This is the thing you see when you delete something that you didn't get from new (or delete[] something you got from new or delete something that you got from new[]). Note that C++ new/delete may use C library functions malloc() and free(), and this could give that message. (Note I said could; it might be something completely different.) I have seen people declare arrays (int x[5];) and then, later, delete [] x. This may or may not give a seg fault, and gdb may or may not give the error message you saw. Remember, that the actual manifestation of bugs as errors may be dependent on undefined behavior, and and actual bugs pop up as a result of something somewhere else in the program. That's what makes it fun.


Regards,

Dave

Footnote:

"Each problem that I solved became a rule which served afterwards to
solve other problems."
---Rene Descartes (1596-1650), Discours de la Methode
  #3  
Old 16-Jun-2006, 00:18
george89 george89 is offline
New Member
 
Join Date: Jun 2006
Posts: 21
george89 is on a distinguished road

Re: A new error message: glibc detected *** free(): invalid pointer: where should I b


Quote:
Originally Posted by davekw7x
Footnote:

"Each problem that I solved became a rule which served afterwards to
solve other problems."
---Rene Descartes (1596-1650), Discours de la Methode

Duely noted. By the way, thank you for the help and advice, I appreciate it.
  #4  
Old 17-Jul-2010, 15:31
ecleric ecleric is offline
New Member
 
Join Date: Jul 2010
Posts: 1
ecleric is on a distinguished road

Re: A new error message: glibc detected *** free(): invalid pointer: where should I begin


I came across this error and didn't use gdb, I solved it though. I was writing a program that would use the system command, the command was in a string then I converted it into a char* I did it this way to easily append a command together through the string class, to write output files and in a function, I didn't have a space at the end of the command and added one and got rid of the error.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hip Hop and All kinds of New Beats Heard on This Site>>> hiphop2006 Music Forum 0 08-Mar-2006 18:47
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:41.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.