Freeing memory

This is a discussion on Freeing memory within the C++ Programming forums, part of the General Programming Boards category; how can I free the memory that I use for int, char, and other things? I want to free the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Freeing memory

    how can I free the memory that I use for int, char, and other things? I want to free the memory after I use the variable.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Why do you want to do that in the first place? If it was an object it would make perfect sense, but why are you worrying about that for POD's? I don't think that's even possible.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by LinuxCoder
    Why do you want to do that in the first place? If it was an object it would make perfect sense, but why are you worrying about that for POD's? I don't think that's even possible.

    of course it is.
    Code:
    int *pInt = new int;
    
    // some code
    
    delete pInt;
    as for why? usually it's for scoping reasons, i.e. you want to create a variable that lives for longer then an automatic scope.

    bikr692002, please note that for "normal" stack variables
    e.g.
    Code:
    void someFunc()
    {
        int x;
    }
    there's no need to free x's memory as it's done automatically at the end of x's scope.

    if your memory is REALLY tight (and I'm talking embedded device with a few k's here) you can limit the scope of any stack variable by wrapping { }'s around it.

    Code:
    void someBiggerFunc()
    {
        int x;
    
        // do lots of stuff
    
        
        {
            int y;
    
            // more stuff
         }
    
         // y has gone out of scope here
    
          x = 5; // x still ok
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If it was an object it would make perfect sense, but why are you worrying about that for POD's?
    Objects can be POD (Plain Old Data).

  5. #5
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    @ChaosEngine:

    Personally from his question i didn't think about variables allocated on the heap, probably just me but i thought his question was refering to deleting POD's allocated on the stack. My lapse i guess

    @Tonto:

    You're totally right, i employed the term incorrectly.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yes, I got the impression he was going to try to free up memory for local stack variables too

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by ChaosEngine
    of course it is.
    Code:
    int *pInt = new int;
    
    // some code
    
    delete pInt;
    as for why? usually it's for scoping reasons, i.e. you want to create a variable that lives for longer then an automatic scope.

    bikr692002, please note that for "normal" stack variables
    e.g.
    Code:
    void someFunc()
    {
        int x;
    }
    there's no need to free x's memory as it's done automatically at the end of x's scope.

    if your memory is REALLY tight (and I'm talking embedded device with a few k's here) you can limit the scope of any stack variable by wrapping { }'s around it.

    Code:
    void someBiggerFunc()
    {
        int x;
    
        // do lots of stuff
    
        
        {
            int y;
    
            // more stuff
         }
    
         // y has gone out of scope here
    
          x = 5; // x still ok
    }
    Thanks, that helps alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 02:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 01:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 07:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 09:51 PM

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