How do I know if memory is already allocated?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kjell Arne Johansen

    How do I know if memory is already allocated?

    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::CSo meClass()
    {
    m_AnObject = new CAnObject();
    }

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

    //Assignment operator overriding
    CSomeClass& CSomeClass::ope rator=(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
  • Attila Feher

    #2
    Re: How do I know if memory is already allocated?

    Kjell Arne Johansen wrote:[color=blue]
    > 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::CSo meClass()
    > {
    > m_AnObject = new CAnObject();
    > }
    >
    > //Copy constructor
    > CSomeClass::CSo meClass(const CSomeClass& orig)
    > {
    > *this = orig;
    > }
    >
    > //Assignment operator overriding
    > CSomeClass& CSomeClass::ope rator=(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?[/color]

    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


    Comment

    • Nils Petter Vaskinn

      #3
      Re: How do I know if memory is already allocated?

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

      [color=blue]
      > //Copy constructor
      > CSomeClass::CSo meClass(const CSomeClass& orig)
      > {[/color]

      m_AnObject = new CAnObject();
      [color=blue]
      > *this = orig;
      > }
      >
      > //Assignment operator overriding
      > CSomeClass& CSomeClass::ope rator=(const CSomeClass& rhs)
      > {
      > if(this != &rhs)
      > { //Does m_AnObject point to legal memory??[/color]

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


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



      hth
      NPV

      Comment

      • Nils Petter Vaskinn

        #4
        Re: How do I know if memory is already allocated?

        On Mon, 01 Sep 2003 13:02:49 +0300, Attila Feher wrote:
        [color=blue]
        > Kjell Arne Johansen wrote:[/color]
        [color=blue][color=green]
        >>
        >> How do I find out if memory is already allocated?[/color]
        >
        > 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).[/color]

        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

        Comment

        • Attila Feher

          #5
          Re: How do I know if memory is already allocated?

          Nils Petter Vaskinn wrote:
          [SNIP][color=blue]
          > 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.[/color]

          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::CSo meClass(const CSomeClass& orig)
          {
          m_AnObject = new CAnObject(*orig .m_AnObject);
          }

          Assuming his CAnObject type has a proper copy constructor.

          --
          Attila aka WW


          Comment

          • Kjell Arne Johansen

            #6
            Re: How do I know if memory is already allocated?

            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"
            <no@spam.for.me .invalid> wrote:
            [color=blue]
            >On Mon, 01 Sep 2003 09:55:04 +0000, Kjell Arne Johansen wrote:
            >
            >[color=green]
            >> //Copy constructor
            >> CSomeClass::CSo meClass(const CSomeClass& orig)
            >> {[/color]
            >
            >m_AnObject = new CAnObject();
            >[color=green]
            >> *this = orig;
            >> }
            >>
            >> //Assignment operator overriding
            >> CSomeClass& CSomeClass::ope rator=(const CSomeClass& rhs)
            >> {
            >> if(this != &rhs)
            >> { //Does m_AnObject point to legal memory??[/color]
            >
            >// By allocating memory for it in the copy constructor we can now be sure
            >// that it does.
            >[color=green]
            >> *m_AnObject = *rhs.m_AnObject ;
            >> }
            >> return *this;
            >> }[/color]
            >
            >
            >CSomeClass::~C SomeClass()
            >{
            > delete m_AnObject;
            > // make sure memory is freed
            >}
            >
            >
            >
            >hth
            >NPV[/color]

            Comment

            • llewelly

              #7
              Re: How do I know if memory is already allocated?

              "Attila Feher" <attila.feher@l mf.ericsson.se> writes:
              [color=blue]
              > Nils Petter Vaskinn wrote:
              > [SNIP][color=green]
              >> 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.[/color]
              >
              > 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::CSo meClass(const CSomeClass& orig)
              > {
              > m_AnObject = new CAnObject(*orig .m_AnObject);
              > }
              >
              > Assuming his CAnObject type has a proper copy constructor.[/color]

              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? :-)

              Comment

              • Attila Feher

                #8
                Re: How do I know if memory is already allocated?

                llewelly wrote:
                [SNIP][color=blue]
                > 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? :-)[/color]

                Maybe he is using the PIMPL idiom?

                --
                Attila aka WW


                Comment

                • Kjell Arne Johansen

                  #9
                  Re: How do I know if memory is already allocated?

                  On Tue, 2 Sep 2003 08:28:04 +0300, "Attila Feher"
                  <attila.feher@l mf.ericsson.se> wrote:
                  [color=blue]
                  >llewelly wrote:
                  >[SNIP][color=green]
                  >> 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? :-)[/color]
                  >
                  >Maybe he is using the PIMPL idiom?
                  >
                  >--
                  >Attila aka WW
                  >
                  >[/color]
                  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

                  Comment

                  Working...