Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Templates (segmentation fault)

Status
Not open for further replies.

pifs

Programmer
Mar 13, 2003
6
BR
Hi all,

consider the following problem:

// class A declaration
class A
{
virtual void do_one_something() = 0;

};

// class Atemplate declaration
template<class T>
class Atemplate:public A
{
public:
void do_one_somenthing(); //implementation is provided
// at other place in code

void getItsxx(T* xx){ xx = itsXX;}

private:
T* itsXX;
}

//Implamentation of a method in class B
void B::do_anything()
{
A* a = getAHandler(); // Get reference to instantiation of A

Atemplate<W>* aTemplate = dynamic_cast<Atemplate<W>*>(a);

W xx;

a->getItsxx(&xx); //I always get a segmentation fault here
}

Can anyone help me on that?? I have also tried to make a static_cast, but the problem persists...

Thanks!!!
 
Atemplate<W>* aTemplate = dynamic_cast<Atemplate<W>*>(a);

I'm not sure that makes any sense at all. Your upcasting from a class A pointer to a template pointer? I don't even know if that is supported but it twists my brain trying to figure it out!

Perhaps a design review might be in order. I will be watching this thread with interest to see if i learn something new!

-pete
 
Well,

first of all, I am downcasting from a base class A to a template class A<W>.
The reason to use this approach is because I am trying to develop a sort of API that has to handle implementatation specific types. The class templates are exposed to the API user and he will generate appropriate classes from it based on his specific implementation. The internal API code only deals with the base class, without having to know about the template classes.

Still could not get rid off the problem...please help me!!!

palbano, Thanks anyway for your interest!!!

 
>> downcast

That's not right is it? Don't you &quot;upcast&quot; to a more derived type? Which in this case is the template class yes?

>> Still could not get rid off the problem...please help
>> me!!!

Code:
    private:
        T* itsXX;

itsXX does not exist since the template instance was never allocated your just casting to it. That's my guess as to why your code is exploding.

&quot;Guardedly waiting for a different explanation&quot;
-pete
 
About downcasting:

I must confess that I never saw these terms: up- or downcasting. It seems to me, that in a class hierarchy, one normally represents derived classes as being below base classes. Anyway, this is not the issue.

The attribute &quot;T* itsXX&quot; has been set in another part of the code, so this is not the problem...

Still waiting for answers...

thanks again, pete
 
>> The attribute &quot;T* itsXX&quot; has been set in another part of
>> the code, so this is not the problem...

Not a believer yet. Can you post it?

-pete
 
More details on the code (with some corrections):

// class A declaration
class A
{
virtual void do_one_something() = 0;

};

// class Atemplate declaration
template<class T>
class Atemplate:public A
{
public:
void do_one_somenthing(); //implementation is provided
// at other place in code
void getItsxx(T* xx){ *xx = *itsXX;}
void setItsxx(T* xx){ *itsXX = *xx;}

private:
T* itsXX;
}

//Implementation of a method in class B
A* B::getAHandler()
{
W xx;

Atemplate<W>* aHandler = new Atemplate<W>();

aHandler->setItsxx(&xx);

return aHandler;
}



//Implementation of a method in class C
void C::do_anything(B* b)
{
A* a = b->getAHandler(); // Get reference to instantiation of A

Atemplate<W>* aTemplate = dynamic_cast<Atemplate<W>*>(a);

W xx;

aTemplate->getItsxx(&xx); //I always get a segmentation fault here
}


I hope the problem is clearer now...waiting for answers!!!

Thanks for the interest!!!
 
pifs,

I don't normally go this degree of effort to debug someone's code for them, but since i did here is your problem.

Code:
void setItsxx(T* xx){ *itsXX = *xx;}

the variable itsXX is type T* and has not been initialized. C/C++ programming 101 &quot;always initialize variables&quot;. ALWAYS

Therefore it has an unknown value which you are using as a memory address and then trying to write to it. This is a basic memory write error.

-pete
 
Upcasting, and downcasting refers to aquisition or loss of attributes... to upcast means to add persiction, methods or attributes (to go lower in the hierarchy tree)... yes, it is a little counter intuitive.

Downcasting is truncation, loss of data and/or methods. A down cast is foing from a float to an int.

This is also knowen as promotion and demotion, because you taking away power/skills. It's all just jargon, but it does help to express ideas when your talking to a team of programmers...

Templates, for the record, are not the cause of segmentationfaults. Memory access is.... Templates are the cause of sysbol refrencing errors, all kinds of syntax errors and sometimes casting errors, but by the vary nature of the thing templates don't cause segfaults... as shown by the problem found by palbano.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top