Thread: Freeing memory for non-pointer variables

  1. #1
    Registered User SuperGodAntMan's Avatar
    Join Date
    Feb 2006
    Posts
    2

    Freeing memory for non-pointer variables

    I haven't done much with C++ as of yet, but I come to the language with a ton of LPC (C derivant used with MudOS) experience. To the end of learning some of the low-level stuff LPC doesn't handle (instead leaving the MudOS driver to handle it for you), I've done some reading around on memory deallocation. However, all of the deallocation operators seem to be specific to arrays.

    Code:
       RECT crc, wrc;
       GetClientRect(hwnd, &crc);
       GetWindowRect(hwnd, &wrc);
       // This resizes things so we get a client rect of 408x408 pixels.
       SetWindowPos(hwnd, HWND_NOTOPMOST, wrc.left, wrc.top, 816-crc.right, 816-crc.bottom, 0);
    
       ShowWindow(hwnd, nFunsterStil);
    
       // If I'm lucky, this wipes the no longer needed crc and wrc variables.
       // If I'm unlucky, this is a really really really bad thing to do.
       delete &crc;
       delete &wrc;
    This all happens in the winmain function before the message loop. I'm not going to be using crc and wrc beyond this part of the code, so in the interests of perfectionist optimization, I'm getting rid of them. My question is, can you use delete in this way on single (non-array) variable instances?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!
    My question is, can you use delete in this way on single (non-array) variable instances?
    No. You can only call delete with values that have been returned by new. Same with malloc/realloc and free.
    I'm not going to be using crc and wrc beyond this part of the code, so in the interests of perfectionist optimization, I'm getting rid of them.
    Perfectionist optimization is probably a futile goal. However, if you are really worried you can put the variables in a seperate block (or even better, a seperate function):
    Code:
    { // start block
       RECT crc, wrc;
       GetClientRect(hwnd, &crc);
       GetWindowRect(hwnd, &wrc);
       // This resizes things so we get a client rect of 408x408 pixels.
       SetWindowPos(hwnd, HWND_NOTOPMOST, wrc.left, wrc.top, 816-crc.right, 816-crc.bottom, 0);
    
       ShowWindow(hwnd, nFunsterStil);
    } // end block

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    What you will want to do is make functions, when a function is called, it allocates the memory for all the variables that it needs created (excluding variables that are passed by reference instead of value) then deletes them all when it is done.

  4. #4
    Registered User SuperGodAntMan's Avatar
    Join Date
    Feb 2006
    Posts
    2
    Thanks for the speedy responses, guys

    I eventually went with turning crc and wrc into a new'd array of RECTs, a la
    Code:
    RECT* rc = new RECT[1];
    which should make it compatible with delete.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I eventually went with turning crc and wrc into a new'd array of RECTs, a la
    >RECT* rc = new RECT[1];
    Great. There's just a little problem. Your array only holds one element.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why would you want to make it compatible with delete? What does delete do that you want to have happen?

    As anonytmouse mentioned, in the case of RECT there really is no reason to try to save that memory, but if you want them destroyed earlier just add an extra scope. Making a dynamic array just so you can call delete doesn't make sense, and is probably an unnecessary pessimization.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I am with Daved, let the computer do the memory managment when it can, instead of doind explicit calls to delete things. It is easier on you, and easier on anyone that wants/needs to debug your code. Besides adding functions and what not makes your program easier to read and trace through.

  8. #8
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    don't worry SuperGod I find myself doing the same thing.. paranoid optimization via managing all memory myself.. though the other guys have point that dynamic memory when not necessary makes things alot tougher to manage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 08:18 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. memory question (pointer related)
    By cjschw in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2004, 01:09 PM