Jump to content

How to use "Constructor call to Constructor" in SystemC module


thundium

Recommended Posts

Hi All:

I met a problem and need to use "constructor call to constructor". Can

any one point out how shall do that? Since SystemC module inherits

from sc_module, I am not sure where I made it wrong. (Maybe it is more

about a C++ probelm).

For my implementation, I got:

Error: (E513) an sc_module_name parameter for your constructor is required

And I made the two constructors like:

sub_module(sc_core::sc_module_name n,int a,int b,int c)

:sc_core::sc_module(n)

{

cout<<"a = "<<a<<endl;

cout<<"b = "<<b<<endl;

cout<<"c = "<<c<<endl;

}

sub_module(sc_core::sc_module_name n)

:sc_core::sc_module(n)

{

new (this)sub_module(n,t1,t2,t3);

}

If I delete the " new (this)sub_module(n,t1,t2,t3)", that constructor

can be used.

Thank you very much for your kind help

Best Regards

Thundium.

Link to comment
Share on other sites

The feature you're looking for is called "delegating constructors" in C++11. It is not possible in C++03.

Your solution is just plain wrong. You'd run the sub_module contructor twice, but the destructor would be run only once. See this article for a more detailed explanation of what happens.

One option could be to use default parameters instead:

sub_module( sc_core::sc_module_name n
         , int a = t1, int b = t2, int c = t3 )
       : sc_core::sc_module(n)
{
 // ...
}

Link to comment
Share on other sites

Hi Philipp:

There is some reasons behind which require me to use "constructor call to constructor".

And I did get successed with following C++ code. (It should not be C++ 11 in my machine, not exactly sure). I can run it.

So if I use sc_module instead of "my_sc_module", why it not works then?Some magaic inside sc_module?

class my_sc_module{
public:
my_sc_module(int my_sc_module_name){
 h=my_sc_module_name;
}
int h;
};

class my_sub_m:public my_sc_module
{
public:
my_sub_m(int my_sc_module_name):my_sc_module(my_sc_module_name){

 new (this)my_sub_m(my_sc_module_name,5);

 cout<<"my_sc_module_name = "<<h<<endl;
}

my_sub_m(int my_sc_module_name, int x1):my_sc_module(my_sc_module_name)
{
}
};

Link to comment
Share on other sites

If I just modifiy the previous code to following, it will crash and get "*** glibc detected *** " and " double free or corruption (fasttop): 0x081ffd30 ***"

BTW: I used the test like:

my_sub_m* a;

a=new my_sub_m(1); //For my_sc_module, which works

a=new my_sub_m("te"); //For sc_module, which breaks

class my_sub_m:public sc_core::sc_module
{
public:
my_sub_m(sc_core::sc_module_name na):sc_core::sc_module(na){
 new (this)my_sub_m(na,5);
}
my_sub_m(sc_core::sc_module_name na, int x1):sc_core::sc_module(na)
{

}
};

Link to comment
Share on other sites

Just read the linked article, I posted in my earlier reply.

You just can't use placement new with 'this' inside the constructor, period.

There is some reasons behind which require me to use "constructor call to constructor".

You have not shown any reason, why you need to use delegating constructors. You can either

  • use default parameters, or
  • duplicate the constructor body,
  • or use an intermediate base class, which always receives "all" constructor parameters

And I did get successed with following C++ code. (It should not be C++ 11 in my machine, not exactly sure). I can run it.

No, you did not succeed. Your code still has "undefined behaviour", which just seems to work correctly.

So if I use sc_module instead of "my_sc_module", why it not works then?Some magaic inside sc_module?

Yes, there is quite some magic in sc_module, like handling the SystemC object hierarchy, etc.

If I just modifiy the previous code to following, it will crash and get "*** glibc detected *** " and " double free or corruption (fasttop): 0x081ffd30 ***"

That's what I said: you mess up the lifetimes of the internal (sub-)objects. It's just undefinied behaviour in C++ and you can't use this approach.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...