Re: const foo * and const foo &
On 1/26/2011 11:33 AM, Ralf Goertz wrote:
I am desperate. I have a huge program using boost::python which crashes
when called from python but which runs fine, when called via main().
While trying to find the problem I encountered an even weirder problem.
Then I tried to write a short program that exhibits the problem, but
that problem vanishes, i.e. the short program behaves as expected but
the big one doesn't. Here is the short one:
#include<iostream>
#include<vector>
using namespace std;
struct foo;
struct Bar {
void bar(const foo&) {
}
};
vector<vector<const foo*> >v;
struct foo { };
int main() {
foo f;
Bar b;
v.push_back(vector<const foo *>(1,&f));
b.bar(*v.back().back()); // (*)
return 0;
}
This compiles. But when I omit the dereferencing in the marked line the
compiler complains (as it should):
error: no matching function for call to 'Bar::bar(const foo*&)'
The problem is: in the big program I do exactly the same. But here the
program compiles *whether* *or* *not* I dereference. How can that be?
It could be that your 'bar' function is a template there. Or that there
are two of them, one for references to const, and one for pointers (i.e.
they are overloaded). Or it could be something else entirely, and there
is no sense in guessing. Post the code. At least the relevant parts.
Start by removing what's irrelevant. After each removal try running and
see if it still crashes. If it doesn't stop crashing after you've
removed all irrelevant parts, you got the code to post; post it. If it
stops crashing somewhere, you found another reason for it to crash;
investigate.
The (main()-called) program crashes when compiled without dereferencing
and works fine otherwise.
<shrug> We're not mind readers, honest.
> The python called version crashes in both
cases. Every crash happens after the program has already stepped over
the marked line without problem and without producing wrong results.
I am puzzled. What's going on?
Something is definitely afoot. Fishy. No way to tell what it is until
you post the code that doesn't work.
V
--
I do not respond to top-posted replies, please don't ask