Cannot delete objects

This is a discussion on Cannot delete objects within the C++ Programming forums, part of the General Programming Boards category; Hello, again. I'm having problems with the Visual Studio compiler. I've defined an "A" class and then created an "A" ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23

    Cannot delete objects

    Hello, again. I'm having problems with the Visual Studio compiler. I've defined an "A" class and then created an "A" object "a" inside of the main function. But whenever I attempt to "delete a;" the compiler spits out this error:

    Code:
    C2440: 'delete' : cannot convert from 'A' to 'void *'

    Definition of Class:
    Code:
    class A
    {
    public:
    	int i;
    };
    Inside of main function:
    Code:
    A a;
    delete a;
    I looked this up on MSDN and the description for C2440 is very varied but doesn't contain the word "delete" anywhere on there. So what am I doing wrong here?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You can only use delete when you have a corresponding 'new' for example:
    Code:
    int* foo = new int;
    //...do stuff with foo
    delete  foo; 
    //or
    int* bar = new int[10];
    //...
    delete [] bar;
    If you want more info, look up dynamic allocation.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    So, you're saying it's impossible to delete variables that aren't dynamically allocated?

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yes.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    There's another way, you can just push them into vecotor and erase them whenever you needed.
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    why would you want to delete variables that are not allocated with new operator? The system will delete them when the function returns to its caller.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    When a local variable goes out of scope it is destructed. If you want a local variable to be destructed earlier than the end of the function or other block of code it is declared in, just add an extra scope. One possible example is a file stream. Since the file stream is closed automatically when it goes out of scope, you might want to have it close earlier to free the memory used from opening the file. This example isn't really the best way to do things, since often it would be better to separate the lone function into multiple functions, but it demonstrates the idea.
    Code:
    void loadData(MyDataContainer& mdc)
    {
      {
        std::ifstream ifs("LotsOfUserData.txt");
    
        // ... read data into mdc
    
      } // ifs goes out of scope and the first big file is closed.
    
      {
        std::ifstream ifs("LotsOfObjectData.txt");
    
        // ... read data into mdc
    
      } // ifs goes out of scope and the second big file is closed.
    }

  8. #8
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Quote Originally Posted by Daved
    When a local variable goes out of scope it is destructed. If you want a local variable to be destructed earlier than the end of the function or other block of code it is declared in, just add an extra scope. One possible example is a file stream. Since the file stream is closed automatically when it goes out of scope, you might want to have it close earlier to free the memory used from opening the file. This example isn't really the best way to do things, since often it would be better to separate the lone function into multiple functions, but it demonstrates the idea.
    Code:
    void loadData(MyDataContainer& mdc)
    {
      {
        std::ifstream ifs("LotsOfUserData.txt");
    
        // ... read data into mdc
    
      } // ifs goes out of scope and the first big file is closed.
    
      {
        std::ifstream ifs("LotsOfObjectData.txt");
    
        // ... read data into mdc
    
      } // ifs goes out of scope and the second big file is closed.
    }
    Ah, I understand now. Thanks for your patience. I'm used to scripting languages like Javascript so the transition over to C++ is rough for me. But I understand now.

    I was using this in an example I was building. Basically it would add an item to a linked list whenever it was constructed and delete it to test the destructor. I just assumed I could use the delete statement to free up the memory of local or global variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 04-29-2008, 06:32 AM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM
  5. how to delete objects
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2001, 02:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21