Copy constructor called from member function?

Discussion in 'C++' started by Craig Nicol, Aug 26, 2004.

  1. Craig Nicol

    Craig Nicol Guest

    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(std::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(10000*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.
     
    Craig Nicol, Aug 26, 2004
    #1
    1. Advertising

  2. 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(std::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(10000*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
     
    Karl Heinz Buchegger, Aug 26, 2004
    #2
    1. Advertising

  3. 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(std::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(10000*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, Aug 26, 2004
    #3
  4. Craig Nicol

    Craig Nicol Guest

    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.
     
    Craig Nicol, Aug 26, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    556
    velthuijsen
    Apr 16, 2004
  2. Tom Widmer
    Replies:
    1
    Views:
    520
    Victor Bazarov
    Nov 17, 2004
  3. TechCrazy
    Replies:
    5
    Views:
    405
    Karl Heinz Buchegger
    Jul 15, 2005
  4. Yan
    Replies:
    2
    Views:
    302
    Phlip
    Mar 27, 2006
  5. cinsk
    Replies:
    35
    Views:
    2,695
    James Kanze
    Oct 11, 2010
Loading...

Share This Page