CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Hybrid View

  1. #1
    Join Date
    Jun 2003
    Location
    India Trichy
    Posts
    245

    How can I delete a Object which is not created by new operator

    Hi gurus.
    I want delete a object which was not created dynamically (new or someother way) how can i do this.

    ex..
    class MyClass
    {
    .....
    ....
    };

    int main()
    {
    MyClass objMyObj;
    .......
    .......
    /// after some stuffs I want delete this object from Memory How can I remove it from Memory...
    ......
    . .......
    .....
    return 1;
    }

  2. #2
    Join Date
    Feb 2002
    Posts
    69
    If you use NEW then you can use like this



    Code:
    MyClass* p = new MyClass[100];
    --
    ---
     delete[] p;

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Yes, but don't use new unless you have.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  4. #4
    Join Date
    Sep 2003
    Location
    India
    Posts
    196
    yeah!!its correct way as shivabharat suggests to use delete only when u use new.

    And to delete the object which is created without new, here the destructor will do ur job in deleting the memory alocated for the object created without using new.

    what u suggest Souldog

  5. #5
    Join Date
    Sep 2003
    Location
    India
    Posts
    196
    yeah!!its correct way as shivabharat suggests to use delete only when u use new.

    And to delete the object which is created without new, here the destructor will do ur job in deleting the memory alocated for the object created without using new.

    what u suggest Souldog

  6. #6
    Join Date
    Jun 2003
    Location
    India Trichy
    Posts
    245
    Hi I know that well, so that I want know is there any other way to do this...
    remove that Object from memory...

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    It is done automatically.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  8. #8
    Join Date
    Feb 2002
    Posts
    69
    When you arent using a new or malloc the memory will be allocated in stacks. Stacks get cleared up automatically and you dont have to clear them explicitly.


    I am not sure if this code make any sense



    Code:
    #include <iostream.h>
    
    class base
    {
    private:
    		int i;
    public:
    
    		base()
    		{
    			i=10;
    		}
    
    		~base()
    		{
    			cout<<"Object destroyed"<<endl;
    		}
    
    		void show()
    		{
    			cout<<"I value is "<<i<<endl;
    		}
    
    		virtual void base::destroy ()
    	   { 
    			
    			base::~base(); 
    		}
    
    
    };
    int main()
    {
    	base b;
    	b.show();
    	b.destroy();
    	base c;
    	c.show(); 
    	return 0;
    }

  9. #9
    Join Date
    Jun 2003
    Location
    India Trichy
    Posts
    245
    Hi thx for ur reply.
    but after calling ur destry (method) the object's memory still not freeing. object still in the memory only.

    the object still refering same memory area. (after destroy method u can call b.show() again u w't get any error)

    rgs
    Balamurali C
    Last edited by ccbalamurali; October 20th, 2003 at 07:57 AM.

  10. #10
    Join Date
    Feb 2002
    Posts
    69
    Humm!! intresting......

    I will get back to regarding this got to work on this..........


  11. #11
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Try this:
    Code:
    #include <iostream.h>
    
    class base
    {
    private:
        int* pi;
    public:
    
        base() : pi( 0 )
        {
            pi = new int;
            *pi=10;
        }
    
        ~base()
        {
            delete pi;
            pi = 0;
            cout<<"Object destroyed"<<endl;
        }
    
        void show()
        {
            cout<<"*pi is "<<*pi<<endl;
        }
    
        virtual void base::destroy ()
        {
            base::~base(); 
        }
    };
    
    int main()
    {
        base b;
        b.show();
        b.destroy();
        b.show();
    
        return 0;
    }
    but after calling ur destry (method) the object's memory still not freeing
    The object is not destroyed. It's dtor was explicitly invoked in destroy(), but the object was still "valid" and in scope. If you check the output from the original source, you will see that "Object destroyed" is printed 3 times, even though only 2 objects are created. The implementation of the dtor in the original source was trivial; in the source I posted, it actually and concretely affects the object.

    Note that explicit calls to the dtor are seldom necessary. Most frequently, one calls a dtor explicitly on an object allocated using a user-defined new operator that takes a placement argument. See "Explicit Destructor Calls" in the "C++ Language Reference" of the MSDN library for more info.
    Last edited by vicodin451; October 20th, 2003 at 08:16 AM.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  12. #12
    Join Date
    Feb 2002
    Posts
    69
    By deafult the size of a class is 1 byte (I guess so)

    I tried this

    Code:
    base b;
    cout<<"Size of object "<<sizeof(b)<<endl;
    b.show();
    b.destroy();
    //b.show();
    cout<<"Size of object "<<sizeof(b)<<endl;
    return 0;
    Gave me output

    Size of object 8
    *pi is 10
    Object destroyed
    Size of object 8
    Object destroyed
    Whats happening???

  13. #13
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    If you used the code I posted, the class is 8 bytes because of the int* class member (4 bytes), and the vtbl ptr (also 4 bytes, as a result of destroy being virtual). If you change destroy to be non-virtual, the size of the class will be 4 bytes (for the int* member).

    Whats happening
    Even though "destroy" is called, it's not really "destroying" the object. You could have called the function "masticate" and the same thing would happen. "Destroy" explicitly calls the destructor which is a no-no in this case, as the destructor is also automatically invoked with the object goes out of scope - at the end of main. Since it has been explicitly invoked, the second (automatic) invocation will take place when the object is in an indeterminate state.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  14. #14
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728
    When an object is allocated on the stack its lifetime is controlled automatically by the compiler. When it goes out of scope it is destroyed.

    If you need to manage the lifetime of the object independently of its scope then you must use new / delete.
    Dave Mclelland.

  15. #15
    Join Date
    Jun 2003
    Location
    India Trichy
    Posts
    245
    ya I want manage life time of Object myself with out creating dynamically (using New oprator) like Java? I think there ways to do this? I do't know exactly.
    regs
    Balamurali C

Page 1 of 2 12 LastLast

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