Hi,
Although I've been using C++ for a while, I've only recently started
writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a
matrix of any type. It has three data members, two integers for the
number of rows and columns in it, and a vector of size rows*columns to
hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and
I am running into memory problems. Specifically when using my own +=
operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) {
std::transform( begin(), end(), begin(), std::bind2nd(st d::plus<T>(),
scalar)); return *this; };
During testing, T=int and S=int (I use two types here to allow int
scalars to be added to double matrices), and scalar=0.
The memory error comes because this function calls the mgMatrix copy
constructor and the call to vector.resize(1 0000*10000) returns an
out-of-memory exception.
BTW, mgMatrix::begin () and mgMatrix::end() are wrappers for the vector
functions of the same name.
Can anyone tell me why the copy constructor is called from the above
function, when invoked as: testMatrix += 0;?
If required, I can post the full source code online.
Cheers,
Craig Nicol. 3 1395
Craig Nicol wrote: Hi,
Although I've been using C++ for a while, I've only recently started writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a matrix of any type. It has three data members, two integers for the number of rows and columns in it, and a vector of size rows*columns to hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and I am running into memory problems. Specifically when using my own += operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) { std::transform( begin(), end(), begin(), std::bind2nd(st d::plus<T>(), scalar)); return *this; };
During testing, T=int and S=int (I use two types here to allow int scalars to be added to double matrices), and scalar=0.
The memory error comes because this function calls the mgMatrix copy constructor and the call to vector.resize(1 0000*10000) returns an out-of-memory exception.
BTW, mgMatrix::begin () and mgMatrix::end() are wrappers for the vector functions of the same name.
Can anyone tell me why the copy constructor is called from the above function, when invoked as: testMatrix += 0;?
return *this;
A temporary object is created, not used by the caller and gets destroyed.
--
Karl Heinz Buchegger kb******@gascad .at
Craig Nicol wrote: Although I've been using C++ for a while, I've only recently started writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a matrix of any type. It has three data members, two integers for the number of rows and columns in it, and a vector of size rows*columns to hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and
That's a 100 million element matrix, and each element is what, a double?
That's 800 MB. Create a couple of those and you're likely to run out of
memory pretty quickly...
I am running into memory problems. Specifically when using my own += operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) {
Just like any other assignment operator, += should return a reference,
and not an object:
template<class S> mgMatrix<T>& operator +=(S scalar) { ...
std::transform( begin(), end(), begin(), std::bind2nd(st d::plus<T>(), scalar)); return *this; };
During testing, T=int and S=int (I use two types here to allow int scalars to be added to double matrices), and scalar=0.
The memory error comes because this function calls the mgMatrix copy constructor and the call to vector.resize(1 0000*10000) returns an out-of-memory exception.
Of course. You asked it to return another object of mgMatrix<T> type. BTW, mgMatrix::begin () and mgMatrix::end() are wrappers for the vector functions of the same name.
Can anyone tell me why the copy constructor is called from the above function, when invoked as: testMatrix += 0;?
Because you defined it as returning an object.
If required, I can post the full source code online.
Not required.
Victor
Victor Bazarov wrote: Craig Nicol wrote:
Although I've been using C++ for a while, I've only recently started writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a matrix of any type. It has three data members, two integers for the number of rows and columns in it, and a vector of size rows*columns to hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and
That's a 100 million element matrix, and each element is what, a double? That's 800 MB. Create a couple of those and you're likely to run out of memory pretty quickly...
That I realise. I'm stress-testing it to see how it falls over. If I
don't push it to the limits, I'm never going to test all the exceptions.
Better it fails now than when its embedded in another program. I am running into memory problems. Specifically when using my own += operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) {
Just like any other assignment operator, += should return a reference, and not an object:
template<class S> mgMatrix<T>& operator +=(S scalar) { ...
That's the problem. Thanks for your help. Now I just need to get VC++ to
release it's lock on the *.pdb file :(
Cheers,
Craig Nicol. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Shea Martin |
last post by:
I have a String class (I know I am re-inventing the wheel, yes I have
heard of boost, and of QString).
My copy constructor does a deep (strcpy) of the char *_buffer member.
I have a member function func(const String ¶m). When an actual
String is passed as the param this is nice and efficient.
I have a constructor which takes a const char* as an argument. And
|
by: Edward Diener |
last post by:
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
by: A |
last post by:
Hi,
A default copy constructor is created for you when you don't specify one
yourself. In such case, the default copy constructor will simply do a
bitwise copy for primitives (including pointers) and for objects types call
their default constructor.
Any others points i should know?
|
by: away |
last post by:
1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.
If don't pass objects of such class as value in a function and don't do
assignment, should copy constructor and assignment operator be unnecessary?
2. If a base class have a pointer member, should its derived classes also
need copy constructor and assignment operator, no matter if a derived class
itself has a...
|
by: trying_to_learn |
last post by:
Why do we need to explicitly call the copy constructor and the operator
= , for base class and member objects in composition?
....book says "You must explicitly call the GameBoard copy-constructor or
the default constructor is automatically called instead"
Why cant the compiler do this on its own. if we are making an object
through copr construction for an inherited class , then why not simply
call the corresponding copy constructors for...
| |
by: blangela |
last post by:
I have decided (see earlier post) to paste my Word doc here so that it
will be simpler for people to provide feedback (by directly inserting
their comments in the post). I will post it in 3 parts to make it more
manageable.
Below is a draft of a document that I plan to give to my introductory
C++ class.
Please note that I have purposely left out any mention of safety issues
in the ctors which could be resolved thru some combination...
|
by: John Salmon |
last post by:
g++ complains about illegal access to a private member
when the following is compiled with a private copy
constructor for the class C. When the copy constructor
is public, the program runs and demonstrates(?) that
the copy constructor is never called (at least no sign
of its chatter on std::cout is visible).
So - is it necessary to have a public copy constructor
in order to use "function style" initializers for an
array of objects? ...
|
by: JurgenvonOerthel |
last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and
Derived2 derive publicly from Base.
Given a 'const Base &input' I want to initialize a 'const Derived1
&output'.
If the dynamic type of 'input' is Derived1, then 'output' should
become a reference to 'input'.
Otherwise 'output' should become a reference to the (temporary) result
of the member function 'input.to_der1()' which returns a Derived1
object by value.
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
|
by: JD |
last post by:
Hi,
My associate has written a copy constructor for a class. Now I need to add
an operator = to the class. Is there a way to do it without change her code
(copy constructor) at all? Your help is much appreciated.
JD
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |