Thread: need to delete an object to free memory

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    need to delete an object to free memory

    Hi,


    i know this is not clever to do and there can be problems if an exception it thrown, but my app is running out of memory and this is because i load some data into my object process it and don't use it any more. now, with processed results i need to do some stuff further but for that i need memory. and i would have tons of it if i deleted my object. so how can i delete an object:


    Code:
    int main(){
    
    Data<unsigned long, unsigned long> myobject(file_in);
    vector<unsigned long> results(myobject.GetData().size());
    
    
      for(unsigned long i = 0;i<myobject.GetData().size();i++){
        // do stuff
      }
      //don't need myobject any more i have my results
    
    delete myobject; // does not work
    
    }
    is there any way around this ?? I need memory. Should i maybe do this in another function and then send back a copy of my results. then my object should be deleted automatically since it is out of scope, right??

    thnx
    Last edited by baxy; 12-08-2013 at 02:30 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A just-plain-variable is deleted once it goes out of scope. If you really need myobject to go away, then you need to declare it in a smaller scope and then have that scope end when you no longer need the object.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    thnx i'll try that

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Use pointers ie:
    delete must be used in conjunction with new.
    Code:
    myobject *obj = new myobject;
    //do stuff with obj using the -> operator and not the . operator
    //I'm done with obj lets get rid of it
    delete obj;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ~Kyo~
    Use pointers ie:
    delete must be used in conjunction with new.
    Although that is an option, it is inferior to tabstop's suggestion because you then have to do manual memory management, unless you use appropriate smart pointers instead. (But do you even need to reuse the (smart) pointer in the first place?)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by laserlight View Post
    Although that is an option, it is inferior to tabstop's suggestion because you then have to do manual memory management, unless you use appropriate smart pointers instead. (But do you even need to reuse the (smart) pointer in the first place?)
    He was trying to use delete to begin with instead of saying don't use delete I gave him another option. My response not only gives a solution it shows how to properly use delete(which is worth at least going over). Pointers aren't entirely evil either while they do give more places to make mistakes and memory leaks they also give you more control over the memory allocation(which is what he wanted?). Generally if your running out of memory you are probably not running on a modern computer, but more likely a FPGA or something.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ~Kyo~
    Pointers aren't entirely evil either while they do give more places to make mistakes and memory leaks they also give you more control over the memory allocation(which is what he wanted?).
    With a smart pointer, it is possible to get the control over memory deallocation (well, not exactly: more like the lifetime of the object pointed to) in the middle of a block by calling an appropriate function, e.g., reset. At the same time, the advantage of RAII is retained.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by ~Kyo~ View Post
    He was trying to use delete to begin with instead of saying don't use delete I gave him another option. My response not only gives a solution it shows how to properly use delete(which is worth at least going over). Pointers aren't entirely evil either while they do give more places to make mistakes and memory leaks they also give you more control over the memory allocation(which is what he wanted?). Generally if your running out of memory you are probably not running on a modern computer, but more likely a FPGA or something.
    But the point was that the OP was looking to get rid of the memory occupied by the container and simply reached for delete as some kind of test, or the like, because he/she was unsure on how to accomplish this.
    The easiest, fastest and less complex solution was also given: simply force the container to go out of scope. delete isn't necessary at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. using delete and free
    By Pulock2009 in forum C Programming
    Replies: 4
    Last Post: 09-10-2010, 09:20 AM
  3. delete object from inside object
    By Drogin in forum C++ Programming
    Replies: 10
    Last Post: 01-02-2009, 08:55 PM
  4. delete on malloced object
    By Yasir_Malik in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2004, 05:50 AM
  5. free() or delete?
    By Trauts in forum C++ Programming
    Replies: 7
    Last Post: 05-14-2003, 04:20 PM