Notices
Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Strange

Thread: C++ How to delete pointers?

  1. #1 C++ How to delete pointers? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    926
    I'm sure it seems like a easy question to answer when you read this thread title.

    I'm not asking how to delete the item with the address a pointer is pointing to.
    I'm asking how do I delete a pointer?

    Wait! Don't leave yet! (Just because I told you it isn't exactly what you were expecting) Please hear me out!

    To delete the item with the address a pointer is pointing to, I use:
    Code:
    delete[] foo;
    Hooray! We just reduced the memory space the application is consuming!

    But hold on for a second... ...
    Pointer takes memory space too...
    Now, I want to delete the pointer!
    Please note that I have no other use of the pointer, and wish to delete it.

    I know up-coming answers would be one of these:
    1. The memory space a pointer reserves is so little, and will only take a few bytes.
    2. You do not have to delete it if you are not going to use it anymore.
    My replies to the Frequently-Answered-Questions are these:
    1. If I have a huge application, that requires me to create tens of thousands of pointer, it would add up to a considerable amount of useless reserved computer memory. I prefer to delete it after I'm done with it.
    2. But I prefer to delete it.
    Anybody knows any ways to delete pointers?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,963
    It depends on the scope of the variable. Local variables (declared in a function) will be "deleted" when the function returns, for example.

    But I really wouldn't worry about it. The compiler will make a very good job of managing the memory used by the program. If you are creating tens of thousands of pointers, then presumably they are in a data structure such as an array. In which case you can free that memory using delete. (If you are creating variables called p00001 to p99999 then I think you need to reconsider your programming style.)

    The only time I have had to worry about memory usage is when programming embedded systems (usually in assembly code) where every byte counts. Otherwise, I never think about much it.


    RamenNoodles likes this.
    ei incumbit probatio qui dicit, non qui negat
    Reply With Quote  
     

  4. #3  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    If you just have a pointer, it is impossible to manually reclaim that space, just like it would be with any other variable. Like Strange said though, local variables are reclaimed when they go out of scope.

    Basically, you can only delete something you called new on, so unless you created your pointers with new (an array of pointers, for example) you can't delete them.

    That said, even 100,000 pointers only takes up about 400 KB of memory.
    Reply With Quote  
     

  5. #4  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    926
    is there any way to delete a globally declared variable somewhere along the line?

    is there any cheat to do so?, like the improper 'goto:' statement (that doesn't make sense to use in proper coding)
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  6. #5  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,963
    Quote Originally Posted by RamenNoodles View Post
    is there any way to delete a globally declared variable somewhere along the line?
    No. Although a smart compiler might notice that you stop using it at some point and reuse the memory for another variable..
    ei incumbit probatio qui dicit, non qui negat
    Reply With Quote  
     

  7. #6  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    It's often considered bad practice to use global variables in the first place, although I know I use them all the time anyway, but you really shouldn't have enough memory tied up in global variables to matter in any way even on embedded systems.

    If you're worrying about deleting global variables, you're probably suffering from premature optimization. Until you can measure where your memory is actually going (and measure that it's actually a problem) don't worry about it. Just delete whatever you called new on and leave it at that for now.
    Reply With Quote  
     

  8. #7  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    926
    Quote Originally Posted by MagiMaster View Post
    Just delete whatever you called new on and leave it at that for now.
    okay, thanks!
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

Similar Threads

  1. Dangers about laser pointers.
    By rogermon in forum Electrical and Electronics
    Replies: 7
    Last Post: March 27th, 2011, 09:09 PM
  2. want to delete
    By chloe in forum Biology
    Replies: 4
    Last Post: March 9th, 2011, 10:15 AM
  3. delete this
    By platonist in forum Personal Theories & Alternative Ideas
    Replies: 2
    Last Post: July 16th, 2009, 10:23 PM
  4. Laser Pointers, Help
    By ryan39 in forum Health & Medicine
    Replies: 3
    Last Post: March 6th, 2009, 04:57 PM
  5. delete
    By Sciencedude in forum General Discussion
    Replies: 1
    Last Post: October 6th, 2008, 11:03 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •