How do I know if memory is already allocated?

Discussion in 'C++' started by Kjell Arne Johansen, Sep 1, 2003.

  1. Hi

    How does it work?

    //This works
    //Constructor is run and memory allocated for m_AnObject
    CSomeClass SomeClass1;
    //copying data works fine
    SomeClass1 = SomeClass2;

    //Oooops, constructor is not run. (Should it be?) Memory is not
    //allocated for m_AnObject. Only copy constructor and assignment
    //operator is run. How do I check if memory is allocated for
    //m_AnObject???
    SomeClass SomeClass1 = SomeClass2;

    //The code
    //Standard constructor
    CSomeClass::CSomeClass()
    {
    m_AnObject = new CAnObject();
    }

    //Copy constructor
    CSomeClass::CSomeClass(const CSomeClass& orig)
    {
    *this = orig;
    }

    //Assignment operator overriding
    CSomeClass& CSomeClass::eek:perator=(const CSomeClass& rhs)
    {
    if(this != &rhs)
    { //Does m_AnObject point to legal memory??
    *m_AnObject = *rhs.m_AnObject;
    }
    return *this;
    }

    How do I find out if memory is already allocated?

    Regards
    Kjell Arne Johansen
     
    Kjell Arne Johansen, Sep 1, 2003
    #1
    1. Advertising

  2. Kjell Arne Johansen

    Attila Feher Guest

    Kjell Arne Johansen wrote:
    > Hi
    >
    > How does it work?
    >
    > //This works
    > //Constructor is run and memory allocated for m_AnObject
    > CSomeClass SomeClass1;
    > //copying data works fine
    > SomeClass1 = SomeClass2;
    >
    > //Oooops, constructor is not run. (Should it be?) Memory is not
    > //allocated for m_AnObject. Only copy constructor and assignment
    > //operator is run. How do I check if memory is allocated for
    > //m_AnObject???
    > SomeClass SomeClass1 = SomeClass2;
    >
    > //The code
    > //Standard constructor
    > CSomeClass::CSomeClass()
    > {
    > m_AnObject = new CAnObject();
    > }
    >
    > //Copy constructor
    > CSomeClass::CSomeClass(const CSomeClass& orig)
    > {
    > *this = orig;
    > }
    >
    > //Assignment operator overriding
    > CSomeClass& CSomeClass::eek:perator=(const CSomeClass& rhs)
    > {
    > if(this != &rhs)
    > { //Does m_AnObject point to legal memory??
    > *m_AnObject = *rhs.m_AnObject;
    > }
    > return *this;
    > }
    >
    > How do I find out if memory is already allocated?


    Why do you think it is not? Your constructor make it sure it does. The
    other functions only copy. In your code the pointer always points to a
    valid memory area returned by the new CAnObject() until your destructor is
    called (I hope you have one).

    --
    Attila aka WW
     
    Attila Feher, Sep 1, 2003
    #2
    1. Advertising

  3. On Mon, 01 Sep 2003 09:55:04 +0000, Kjell Arne Johansen wrote:


    > //Copy constructor
    > CSomeClass::CSomeClass(const CSomeClass& orig)
    > {


    m_AnObject = new CAnObject();

    > *this = orig;
    > }
    >
    > //Assignment operator overriding
    > CSomeClass& CSomeClass::eek:perator=(const CSomeClass& rhs)
    > {
    > if(this != &rhs)
    > { //Does m_AnObject point to legal memory??


    // By allocating memory for it in the copy constructor we can now be sure
    // that it does.

    > *m_AnObject = *rhs.m_AnObject;
    > }
    > return *this;
    > }



    CSomeClass::~CSomeClass()
    {
    delete m_AnObject;
    // make sure memory is freed
    }



    hth
    NPV
     
    Nils Petter Vaskinn, Sep 1, 2003
    #3
  4. On Mon, 01 Sep 2003 13:02:49 +0300, Attila Feher wrote:

    > Kjell Arne Johansen wrote:


    >>
    >> How do I find out if memory is already allocated?

    >
    > Why do you think it is not? Your constructor make it sure it does. The
    > other functions only copy. In your code the pointer always points to a
    > valid memory area returned by the new CAnObject() until your destructor is
    > called (I hope you have one).


    But the copy constructor does not allocate it's own memory, so if you
    copy an object the two will share the internal object. Then when one of
    them is destroyed (with a proper destructor) the other object will be left
    with a dangling pointer just waiting to cause a crash.
    If this is the desired behaviour the default copy constructor will do fine.

    regards
    NPV
     
    Nils Petter Vaskinn, Sep 1, 2003
    #4
  5. Kjell Arne Johansen

    Attila Feher Guest

    Nils Petter Vaskinn wrote:
    [SNIP]
    > But the copy constructor does not allocate it's own memory, so if you
    > copy an object the two will share the internal object. Then when one
    > of them is destroyed (with a proper destructor) the other object will
    > be left with a dangling pointer just waiting to cause a crash.
    > If this is the desired behaviour the default copy constructor will do
    > fine.


    Hm. I have apparently missed the broken copy constructor. The OP needs to
    do something like this (along the lines of his copy assignment operator):

    //Copy constructor
    CSomeClass::CSomeClass(const CSomeClass& orig)
    {
    m_AnObject = new CAnObject(*orig.m_AnObject);
    }

    Assuming his CAnObject type has a proper copy constructor.

    --
    Attila aka WW
     
    Attila Feher, Sep 1, 2003
    #5
  6. Hi

    Yes of course. Allocate memory in the copy constructor.
    Thank You.

    (I have a proper destructor :)

    Regards
    Kjell Arne

    On Mon, 01 Sep 2003 10:35:26 GMT, "Nils Petter Vaskinn"
    <> wrote:

    >On Mon, 01 Sep 2003 09:55:04 +0000, Kjell Arne Johansen wrote:
    >
    >
    >> //Copy constructor
    >> CSomeClass::CSomeClass(const CSomeClass& orig)
    >> {

    >
    >m_AnObject = new CAnObject();
    >
    >> *this = orig;
    >> }
    >>
    >> //Assignment operator overriding
    >> CSomeClass& CSomeClass::eek:perator=(const CSomeClass& rhs)
    >> {
    >> if(this != &rhs)
    >> { //Does m_AnObject point to legal memory??

    >
    >// By allocating memory for it in the copy constructor we can now be sure
    >// that it does.
    >
    >> *m_AnObject = *rhs.m_AnObject;
    >> }
    >> return *this;
    >> }

    >
    >
    >CSomeClass::~CSomeClass()
    >{
    > delete m_AnObject;
    > // make sure memory is freed
    >}
    >
    >
    >
    >hth
    >NPV
     
    Kjell Arne Johansen, Sep 1, 2003
    #6
  7. Kjell Arne Johansen

    llewelly Guest

    "Attila Feher" <> writes:

    > Nils Petter Vaskinn wrote:
    > [SNIP]
    >> But the copy constructor does not allocate it's own memory, so if you
    >> copy an object the two will share the internal object. Then when one
    >> of them is destroyed (with a proper destructor) the other object will
    >> be left with a dangling pointer just waiting to cause a crash.
    >> If this is the desired behaviour the default copy constructor will do
    >> fine.

    >
    > Hm. I have apparently missed the broken copy constructor. The OP needs to
    > do something like this (along the lines of his copy assignment operator):
    >
    > //Copy constructor
    > CSomeClass::CSomeClass(const CSomeClass& orig)
    > {
    > m_AnObject = new CAnObject(*orig.m_AnObject);
    > }
    >
    > Assuming his CAnObject type has a proper copy constructor.


    But if that is desirable why not just abandon new, the pointer, the
    hassle of dynamic memory allocation, and do this:

    class CSomeClass
    {
    //This class relies on the compiler-generated
    // default constructor,
    // copy constructor,
    // destructor,
    // and copy-assignment operator.
    CAnObject m_AnObject;
    public:
    //... Some public interface the OP didn't show us ...
    };

    Why do work when the compiler will do it for you? :)
     
    llewelly, Sep 1, 2003
    #7
  8. Kjell Arne Johansen

    Attila Feher Guest

    llewelly wrote:
    [SNIP]
    > class CSomeClass
    > {
    > //This class relies on the compiler-generated
    > // default constructor,
    > // copy constructor,
    > // destructor,
    > // and copy-assignment operator.
    > CAnObject m_AnObject;
    > public:
    > //... Some public interface the OP didn't show us ...
    > };
    >
    > Why do work when the compiler will do it for you? :)


    Maybe he is using the PIMPL idiom?

    --
    Attila aka WW
     
    Attila Feher, Sep 2, 2003
    #8
  9. On Tue, 2 Sep 2003 08:28:04 +0300, "Attila Feher"
    <> wrote:

    >llewelly wrote:
    >[SNIP]
    >> class CSomeClass
    >> {
    >> //This class relies on the compiler-generated
    >> // default constructor,
    >> // copy constructor,
    >> // destructor,
    >> // and copy-assignment operator.
    >> CAnObject m_AnObject;
    >> public:
    >> //... Some public interface the OP didn't show us ...
    >> };
    >>
    >> Why do work when the compiler will do it for you? :)

    >
    >Maybe he is using the PIMPL idiom?
    >
    >--
    >Attila aka WW
    >
    >

    Thank you for your help. The problem is solved and everything is
    fine. CSomeClass is an interface class operating on the CAnObject
    methods.

    Kjell Arne
     
    Kjell Arne Johansen, Sep 2, 2003
    #9
    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. Replies:
    5
    Views:
    662
    Matt Wharton
    Dec 9, 2004
  2. Dalton Barreto
    Replies:
    4
    Views:
    586
    Dalton Barreto
    Nov 7, 2006
  3. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    276
    Gregory Toomey
    Apr 23, 2004
  4. mac8500
    Replies:
    3
    Views:
    79
    Rahul
    May 3, 2006
  5. Chris
    Replies:
    1
    Views:
    158
Loading...

Share This Page