Thread: how to delete objects

  1. #1
    Unregistered
    Guest

    Question how to delete objects

    i am new into c++ programming, and i have when i want to delete objects.

    myClass* p_myClass=new myClass;
    delete p_myClass;

    this is working out well, but this is a pointer, if i use the following statements

    myClass myObject;
    delete myObject;

    ...the compiler (Visual C++) won't accept this.

    if i try
    delete &myObject

    ...the compiler accept. but i get a "runtime error".

    I hope that anyone "out there" can help me.


    Thanks!
    Thomas

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You only need to delete dynamically instantiated objects, i.e. those created with new or some other allocator. It is not necessary, and indeed is an error, to try to delete static objects.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Unregistered
    Guest
    isn't possible that the computer runs out of memory if i do not delete my objects?

    and what is the difference between a "normal" object and a "static" object.

    isn't the (default) constructor called (and the object initialized) when i write:
    myClass myObject;

    thanks in advance!

    Thomas

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    If you have not created your own constructor than a default constructor is created for you.

    isn't possible that the computer runs out of memory if i do not delete my objects?
    Only dynamically allocated objects will cause this problem because they are instantiated on the free store and the only way to delete them is to maunally release the memory.

    Other instances are automatic and they are destroyed once the function goes out of scope -except for the objects in main (they are all static). When objects are instanciated in a called function the memory is reserved from the function stack, not from the free store, and when the program returns from the called function, the function stack is released into the free store and all the objects are destroyed.
    Last edited by Witch_King; 08-30-2001 at 05:57 AM.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Registered User Hoegje's Avatar
    Join Date
    Aug 2001
    Posts
    4
    isn't the (default) constructor called (and the object initialized) when i write:
    myClass myObject;
    Yes it is, and when your object goes out of scope, the (default) destructor for that object is called, so that will take care of all the memory cleanup for you. That is only if you haven't put anything on the free store (with new) somewhere in hte use (or construction) of the object. If so, then you would have to write your own desctructor, which would delete that memory chunck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 04-29-2008, 06:32 AM
  2. Cannot delete objects
    By theJ89 in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2006, 10:25 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM