By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

Unused method is acting up

P: n/a
Hi,

I have defined a method of a base class which writes to a C-style array
without initializing it. (No, I don't like C arrays either, but I have
to use it here because of another library I'm using.) Calling this
without first allocating memory would of course give a run-time error,
but what puzzles me is that I get a segmentation fault when this method
is defined, even if it is never called at all! Is that normal?

The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().
class S
{
//...
virtual void Skew( int* result ) const
{
vector<Real> v = this->AsReals();
for (unsigned i = 0; i < v.size(); ++i)
result[i] = (int)rint(v[i]);
}

virtual S* Clone() const = 0;
};
class TS : public S
{
//...
// does not override Skew()

TS* TS::Clone() const
{
return new TS(*this);
}
};

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Martin Magnusson" <lo*******@frustratedhousewives.zzn.com> wrote in message
news:bp**********@green.tninet.se...
Hi,

The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().

TS* TS::Clone() const
{
return new TS(*this);
}


What does your copy-constructor look like? More importantly probably: is
"this" non-null at the time you call the Clone function? (i.e., are you
calling Clone on a null pointer? We can't see the calling code, so we don't
know.)

If you get a run-time error just by defining a function that is never
called, it is NOT that function causing the problem (unless it really IS
getting called, and you just didn't know it...a debugger will tell you
that!). It is a problem elsewhere that simply fails to crash in some
limited testing when that function is not defined. Often this is caused by
referencing memory you haven't allocated or have already deallocated. (I
look first for null pointers, deleted pointers, and writing past array
bounds.)

Try debugging your app to see what is going on more clearly.

-Howard
Jul 22 '05 #2

P: n/a
"Martin Magnusson" <lo*******@frustratedhousewives.zzn.com> wrote...
I have defined a method of a base class which writes to a C-style array
without initializing it. (No, I don't like C arrays either, but I have
to use it here because of another library I'm using.) Calling this
without first allocating memory would of course give a run-time error,
but what puzzles me is that I get a segmentation fault when this method
is defined, even if it is never called at all! Is that normal?
No, it's not normal, but apparently it has nothing to do with that.
The segmentation fault is likely due to some other error, and if
you define your member function, you just help _uncover_ that other
error.

The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().
"Seems to"? Do you have a debugger? Do you know how to use one?
If not, follow the recommendations in FAQ 5.8, then we will try
to help. Please try to avoid posting code that "looks like the
non-working variation". You either post the real non-working code
or you likely all by yourself.

Memory problems like the one you describe are not easy to catch,
I'll give you that. However, they are not impossible to find and
just require some elbow grease.

Look for uninitialised pointers (not just arrays). Look for memory
_overruns_ (they are the most destructive little pests). Look for
failed memory allocations. And, do post your _real_ code after
distilling it to the minimum that still exhibits the error. We can
help, I am sure of it.


class S
{
//...
virtual void Skew( int* result ) const
{
vector<Real> v = this->AsReals();
for (unsigned i = 0; i < v.size(); ++i)
result[i] = (int)rint(v[i]);
}

virtual S* Clone() const = 0;
};
class TS : public S
{
//...
// does not override Skew()

TS* TS::Clone() const
{
return new TS(*this);
}
};

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.