473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointer gets lost when calling one construction from another

A constructor to a class takes some arguments including a pointer which is assigned to a class variable. The constructor then calls a default constructor, but inside the default constructor the pointer is no longer valid.

some code should illustrate.

Expand|Select|Wrap|Line Numbers
  1. Feature::Feature(){
  2.     cout<<"at b "<<oc<<endl;
  3.     cout<<"b is "<<oc->geoSphere->getNumVs()<<endl;
  4.     scoreData=ArrayTools::allocate2DArray<float>(oc->numSteps,oc->geoSphere->getNumVs());
  5.     //some stuff
  6. }
  7. Feature::Feature(float weight,OVASControl* o) : weight(weight),oc(o) {
  8.     cout<<"at a "<<oc<<endl;
  9.     cout<<"a is "<<oc->geoSphere->getNumVs()<<endl;
  10.     Feature();
  11. }
  12.  
here i've puts some prints in to demonstrate, the output is below:

at a 0x1641bb0
a is 642 <-this is the correct value.
at b 0x7fadef8c0ded
Segmentation fault <-trying to access the pointer now segfaults.

since the flow goes from one constructor directly to the other, i dont understand how the class variable oc gets lost. Can anyone explain?

best regards,

Liam
Feb 11 '11 #1

✓ answered by hype261

What you are trying to do is constructor chaining, calling a constructor inside another constructor, and the current C++ standard doesn't support this. Languages like C# do. From reading about the new standard C++ 0X standard this is one feature that will be implemented.

Secondly your code has a bug in it assuming your default constructor is declared public. If somebody calls the default constructor the pointer oc won't point to anything and will probably crash when you try to dereference it.

5 2160
hype261
207 100+
What you are trying to do is constructor chaining, calling a constructor inside another constructor, and the current C++ standard doesn't support this. Languages like C# do. From reading about the new standard C++ 0X standard this is one feature that will be implemented.

Secondly your code has a bug in it assuming your default constructor is declared public. If somebody calls the default constructor the pointer oc won't point to anything and will probably crash when you try to dereference it.
Feb 11 '11 #2
Many thanks for the clear answer. I had no idea constructor wasn't allowed.

The presence of the default construct was originally only because i kept getting compile errors demanding one even though i wasn't explicitly calling one. I'll try again to get rid of it.
Feb 11 '11 #3
newb16
687 512MB
I tried it with printf("this %d\n", this); inserted in both constructors and it shows that your constructor call creates new, another Feature object and then discards it.
Feb 11 '11 #4
@newb16, you mean that if you use chained constructors like this it can cause a memory leak?
Feb 11 '11 #5
newb16
687 512MB
No memory leak, but the Feature(); call doesn't call the Feature() constructor of 'this' object but creates a new Feature object that is destroyed when it leaves the scope.
Feb 14 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Doug Taylor | last post by:
Hi, I originally posted this in dotnet.security, but have moved it here by request: Hi, I am trying to programmatically add a user with read permissions to the DACL of a registry key. ...
6
by: Tony G. | last post by:
Hi there, I have an APS 3 application, running on a Windows 2003 Web edition server - it is a very busy website, and when users are click on certain links (membership info), a new window i...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
4
by: Pieter Linden | last post by:
Hi, Hopefully it's just me missing something obvious (but at 2AM, not much is obvious, I guess...) I have a table of courses that people have taken (StudentID,CourseID) and the user selects a...
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
3
by: Tom | last post by:
I have a VB .NET application that has a text box with the following code to handle the leave event. Private Sub txtIDiscountRate_TextChanged(ByVal sender As System.Object, ByVal e As...
3
by: Jurgen Appelo | last post by:
I asked this question earlier, but unfortunately the two replies I got did not solve the problem. Here it is again, but now with the code: After an Update my FormView always loses its viewstate...
1
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; ...
4
by: TS | last post by:
I am losing my colgroup html element of a table that i toggle back and forth of being visible=true/false. is there any workaround since the rendered html doesn't contain it even though it has not...
2
by: KiddoGuy | last post by:
Hello, I am trying to save my position in a file to resume after running some checks that will position the file pointer at EOF. fpos_t *floc; /* location in file to where i want to return */ /*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.