 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Patrick Leslie Polzer Guest
|
Posted: Fri Jan 21, 2005 10:32 am Post subject: Problem overloading operator << |
|
|
Hello,
another strange problem occured to me.
The following method
template <typename FK, typename FD>
std::ostream& operator << ( std::ostream& out, MacroNode
{
out << "(" ;
for ( int i = 0; i < (2 * mn.order); ++i )
if ( mn.nodes[i] == NULL )
out << "X|" ;
else
out << stringify(&mn.nodes[i]->key) + ")" ;
return ( out );
}
fails at runtime with the message:
*** glibc detected *** corrupted double-linked list: 0x... ***
If I comment out
out << "(" ;
and
+ ")"
everything's okay. Why?
Leslie
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dtmoore Guest
|
Posted: Sat Jan 22, 2005 5:10 am Post subject: Re: Problem overloading operator << |
|
|
Patrick Leslie Polzer wrote:
| Quote: | Hello,
another strange problem occured to me.
The following method
template <typename FK, typename FD
std::ostream& operator << ( std::ostream& out, MacroNode
{
out << "(" ;
for ( int i = 0; i < (2 * mn.order); ++i )
if ( mn.nodes[i] == NULL )
out << "X|" ;
else
out << stringify(&mn.nodes[i]->key) + ")" ;
return ( out );
}
fails at runtime with the message:
*** glibc detected *** corrupted double-linked list: 0x... ***
If I comment out
out << "(" ;
and
+ ")"
everything's okay. Why?
|
That is a weird error message, and I would say it suggests you have an
error (probably pointer/memory related) elsewhere in your code. Such
problems are often very tricky to debug, and can seem to be caused by
innocuous code, as seems to be the case in your example.
Having said that, you could at least clean up your routine a bit by
only doing the output in one place. Use a variable to store the
partially constructed string, and only dump it to the output stream at
the end of the function.
Unfortunately, even if your error message goes away after making the
suggested change, it doesn't mean that you have necessarily found the
bug. At least it wouldn't make me sleep any easier, given my past
experiences with things like running off the end of the array, which
also caused similarly cryptic messages. IIWY I would keep looking ...
perhaps "visualizing" the construction and destruction of the all the
Node objects in your tree, just to make sure each that the number of
calls match up.
HTH,
Dave Moore
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Jan 22, 2005 5:37 am Post subject: Re: Problem overloading operator << |
|
|
Patrick Leslie Polzer <leslie.polzer (AT) gmx (DOT) net> writes:
| Quote: | If I comment out
out << "(" ;
and
+ ")"
everything's okay. Why?
|
The program probably has undefined behavior for some reason that is
impossible to tell from what you post.
Please post a *minimal* (<50 lines), but complete program that
illustrates what you are experiencing.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Patrick Leslie Polzer Guest
|
Posted: Sat Jan 22, 2005 11:20 am Post subject: Re: Problem overloading operator << |
|
|
Thomas Maeder wrote:
| Quote: | Please post a *minimal* (<50 lines), but complete program that
illustrates what you are experiencing.
That's not easy (otherwise I would have). |
The program implements a B-Tree which needs, to properly test it,
at least a Node class, a MacroNode class, an insertion method
(which is quite long) and, of course, a test file.
However, you can find
- the sources
- the sources in a tarball
- a bzipped core file
- a bzipped static executable
at http://www.fmi.uni-passau.de/~polzer/btree/
in case you want to tackle it anyway. Please note that older
versions of GNU libc (mine is 2.3.4) may not detect this error.
Leslie
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Patrick Leslie Polzer Guest
|
Posted: Sat Jan 22, 2005 11:23 am Post subject: Re: Problem overloading operator << |
|
|
dtmoore wrote:
| Quote: | Having said that, you could at least clean up your routine a bit by
only doing the output in one place. Use a variable to store the
partially constructed string, and only dump it to the output stream at
the end of the function.
That was the original way I did it... no luck here. |
| Quote: | IIWY I would keep looking ...
That seems the only way of doing it - yet it'd might be easier if |
I knew _how_ glibc detects this "corruption".
Think I'm going to ask in some more appropriate newsgroup then.
Kind regards and thanks,
Leslie
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Jan 23, 2005 2:39 am Post subject: Re: Problem overloading operator << |
|
|
Patrick Leslie Polzer <leslie.polzer (AT) gmx (DOT) net> writes:
| Quote: | Please post a *minimal* (<50 lines), but complete program that
illustrates what you are experiencing.
That's not easy (otherwise I would have).
The program implements a B-Tree which needs, to properly test it,
at least a Node class, a MacroNode class, an insertion method
(which is quite long) and, of course, a test file.
However, you can find
- the sources
- the sources in a tarball
- a bzipped core file
- a bzipped static executable
at http://www.fmi.uni-passau.de/~polzer/btree/
|
I won't go there.
If I (and everybody else willing to help you) goes there and does the
minimizing, it will be done N times. If you do it and post the result,
it will be done 1 time. By the person who will benefit the most from
it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Jørgensen Guest
|
Posted: Tue Jan 25, 2005 10:42 am Post subject: Re: Problem overloading operator << |
|
|
"Patrick Leslie Polzer" <leslie.polzer (AT) gmx (DOT) net> wrote
| Quote: | Thomas Maeder wrote:
Please post a *minimal* (<50 lines), but complete program that
illustrates what you are experiencing.
That's not easy (otherwise I would have).
The program implements a B-Tree which needs, to properly test it,
at least a Node class, a MacroNode class, an insertion method
(which is quite long) and, of course, a test file.
|
I had a quick look at your source code. It seems that you never use the copy
constructor and the assignment operator for the MacroNode class. I would
recommend declaring them private and remove the implementation. That will
leave fewer places to search for the bug.
Secondly, in the MacroNode class you have "Node** nodes", which appears to
be an array of pointers to Node. I would start by examining all uses of this
array. It's quite possible that you have an out-of-bounds error. One
possible way to catch such an error is to define a new class Array<>, and
then define Array<Node *> nodes. This Array class can then perform all the
necessary bounds checking.
BTW, I tried the test program and my platform (Linux, i386, g++ version
3.2), and there was no error message. But then again, I'm using glibc
version 2.3.2.
Happy bug-hunting!
-Michael.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Tue Jan 25, 2005 10:48 am Post subject: Re: Problem overloading operator << |
|
|
Patrick Leslie Polzer wrote:
[code which causes a weird runtime error is...]
I only looked quickly, but your copy constructor for
MacroNode is wrong.
You have, with nodes being of type Node ** :
: nodes = new Node*[ 2 * order ];
:
: for ( int i = 0; i < (2 * order); i++ )
: if (mn.nodes[i] != NULL)
: *nodes[i] = *mn.nodes[i];
Here you dereference the pointer nodes[i], which has not
yet been initialized.
: else
: nodes[i] = NULL;
I only looked quickly, so I don't know if there are any
other errors.
BTW you could simplify your code a lot by using
std::vector
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|