473,657 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logic behind initializing via constructors

84 New Member
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 1550
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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
6341
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
16
7197
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 inherited constructor, since I cannot create a temporary vector before calling). Is there a possibility to assign a vector in one line, like it is possible for arrays?
2
1506
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 variable might be used or unused in a given object. For each field, there is a FieldIsValid() member function that must be called before accessing or mutating a given field. If a field is reported as not valid, it must not be accessed or mutated....
3
3488
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; // ... more protected fields }
1
996
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 where I place the code for instance variable initialization? in both of the constructors?
0
1209
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 services. · MABLE utilizes SOAP 1.2 protocol. · MABLE uses AXIS 1.4 as a web service transport. · MABLE support state-full conversations by implementing a conversation session.
5
2574
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
2286
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
1897
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 it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable* (even though it doesn't), whereas the original one does not. Thus, when I declare and use it before...
16
4178
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 private variable which is a priority queue. In class foo's header file, I declared it as: class foo { private:
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.