CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    Clarify me on this example

    Hi,
    I have the following code.

    class CVehicle
    {
    }

    class CCar : public CVehicle
    {
    }

    int main()
    {
    CVehicle *m_VehicleType;
    CCar m_Car;
    m_VehicleType = &m_Car;
    delete(m_VehicleType); // Can i do this way
    }

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: Clarify me on this example

    AFAIK attempting to call delete on an object allocated on the stack is undefined behavior. There's no guarantee what will happen - but it won't be anything good. Don't do it.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Clarify me on this example

    delete(m_VehicleType); // Can i do this way
    No. You have to pair new with delete, and new[ ] with delete[]. If there was no new, you must not call any delete for an object.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Sep 2008
    Posts
    48

    Re: Clarify me on this example

    Yes, even i am also thinking the same, becoz i have read many times that we need to use delete only on objects created by using 'new' operator. This is clear to me now.

    So it means we should always use in this way

    Base *ptr = new Derived();

    if we want base class pointer to point to Derived object, am i correct ?

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Clarify me on this example

    No, not necessarily. It all depends on how the classes are declared and what their lifetimes are.

    In your example just remove the 'delete' line. The car object will be automatically destructed when the execution leaves 'main()'
    Code:
    int main()
    {
    	      CVehicle *m_VehicleType;
    	      CCar m_Car;
    	      m_VehicleType = &m_Car;
    } << m_Car destroyed here.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Sep 2008
    Posts
    48

    Re: Clarify me on this example

    Hi, If we do this way, i mean removing the delete statement, is it not going to be a memory leak problem ?

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Clarify me on this example

    No.
    The CCar object is created within the body of 'main' and destroyed when it goes out of scope, automatically.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured