473,383 Members | 1,822 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,383 software developers and data experts.

logic behind initializing via constructors

84
thanks to some people here I know now that if I have a class CmbCalc then the following code
Expand|Select|Wrap|Line Numbers
  1. CmbCalc::CmbCalc() :infla(true) 
will initialize infla to be true.
This class is defined in a header file what it implies it is a global class for all .cc files which include this header file, right?

Now the question is when will the constructor be executed?

All this problem arises from a already written monte-carlo simulation code. There is a function called master() which writes to disk and makes the function slave() do the calculations. The slave() always defines an object of type CmbCalc, does this mean the constructor is executed every time the master calls the slave?
This is important because at the of the slave's calculation one has infla=false and the code to work properly needs to start always with infla=true.

Thanks very much for any clarification!
Aug 8 '07 #1
7 1534
weaknessforcats
9,208 Expert Mod 8TB
Constructors are called every time an object is created.

If your slave() function has a CmbCalc as a local variable, then the constructor will be called.

If your slave() function has a CmbCalc as an argument variable, then the constructor will be called.

If your slave() function has a CmbCalc as a reference argument variable, then the constructor will not be called.
Aug 8 '07 #2
sanctus
84
The slave has a definition like:
Expand|Select|Wrap|Line Numbers
  1. CmbCalc cmbcalc;
  2.  
So if I understood you right the constructor is called and it solves my doubt (but not the problem since the program still goes in an eternal loop stopping when using 2 Gb of active memory...)

Thanks very much.
Aug 8 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
The loop is a separate issue.

Is the slave() calling itself??

If so, this is a recursion and will continue until you crash. Recursive calls need a means of limiting the number of recursions.
Aug 8 '07 #4
sanctus
84
The loop is a separate issue.

Is the slave() calling itself??

If so, this is a recursion and will continue until you crash. Recursive calls need a means of limiting the number of recursions.

No the slave sends the results to the master() (via MPI, don't understand that really) and requests a new task. Then the master sends a new task...there is a do-while "loop" in the master() with the condition being just a boolean.

I'll try to learn to use "advanced gdb" (in the sense using it further than just to see where a program crashes) to find what it keeps calculating...
Aug 9 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
It's probably the Message Passing Interface (MPI). There may be many slave() calls still running unless you have forced them to completion.

I would look there.
Aug 9 '07 #6
sanctus
84
Thanks, eventually I figured it out a pointer was not deleted: in the new function I added I called the old pointer to be deleted instead of the new one... and in this in 8 different slaves so obviously there would be no memory left.
Something what I don't understand though is the way in the program it was original deleted, it had a line
Expand|Select|Wrap|Line Numbers
  1. delete pointerA;//maybe there is delete[], I don't remember now but that is not a problem
  2. pointerA=0;//this is the problem
  3.  
If one deletes it why also pose =0?
Aug 10 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
When you delete a pointer, the memory allocated for it is returned to the free store. At the location where the memory is allopcated, there is a header just in front of that address which contains information bout how big th allocation is. After the delete, this header is overwritten by the delete operator. Should you delete again, the second delete will see this overwritten header and will crash your program.

You protect against a second delete by setting the pointer to zero. Deleting a zero pointer does nothing. delete just returns. Unfortunately, this only works when you are certain that there is only one coppy of the pointer.

That is what the setting of the pointer to zer is about.

You can protect against deletion of memory when there are multiple copies of the pointer in the program by using a handle class object instead of the pointer.

These are called smart, or managed, pointers. This article explains how this works.
Aug 10 '07 #8

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
16
by: Emanuel Ziegler | last post by:
Hi, I am using the vector class from the STL. Normally, I initialize values element by element, which is very uncomfortable and sometimes impossible (e.g. when passing a constant vector to an...
2
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
1
by: dotnettester | last post by:
I have two constructors, one that takes a string parameter and another with no parameters. I want initialize one instance variable from a value defined in web.config file. My question is...
0
by: Al Fatykhov | last post by:
Using MABLE logic engine with existing .NET applications. MABLE web services provide an interface to MABLE business objects and logic. Let us review some technical details of the MABLE web...
5
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
16
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.