Thread: Deleting memory

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I allocated memory and i forgot to delete it at the end and then i compiled it.
    Then i realized my mistake and deleted it and recompiled the program.
    But it keeps giving me the same result as before having not deleted it.
    Even if i restart my system it wont compile with the changed code.
    Whats the deal with this? Its not the first time that it happens to me.
    Isnt memory freed when you close a program even if you didnt freed it in your program?

    Code:
    #include <iostream>
    using namespace std;
    
    
    int *pPointer;
    
    void SomeFunction()
    {
        // make pPointer point to a new integer
    
        pPointer = new int;
        
        *pPointer = 25;
    }
    
    int main()
    {
        cout<< "Value of *pPointer before:"<< *pPointer <<endl;
        
        SomeFunction(); // make pPointer point to something
    
        cout<< "Value of *pPointer after: "<< *pPointer <<endl;
        
        delete pPointer;
    
        system ("pause");
    }
    Last edited by Ducky; 03-04-2009 at 02:14 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you use *pPointer (i.e. dereference pPointer) before setting it to anything. However, since pPointer is a global variable, it will be initialized to all zeros -- so in effect, you're dereferencing a NULL pointer. (I've seen compilers that generate executables which detect this, but most do not.)

    I'm wondering if you meant to use something like this . . .
    Code:
    #include <iostream>
    using namespace std;
    
    
    int *pPointer;
    
    void SomeFunction()
    {
        // make pPointer point to a new integer
    
        pPointer = new int;
        
        *pPointer = 25;
    }
    
    int main()
    {
        cout<< "Value of pPointer before: "<< pPointer <<endl;
        
        SomeFunction(); // make pPointer point to something
    
        cout<< "Value of pPointer after: "<< pPointer <<endl;
        printf("Value of *pPointer after: %d\n\n", *pPointer);
        
        delete pPointer;
    
        system ("pause");
    }
    Oh, and why are you using cout some of the time, and printf() some of the time? You're either using C or C++ (probably C++ since you're also using new and delete) -- don't use both if you can help it.

    Also, see the FAQ for alternatives to system("pause"), which I dislike.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    Even if i restart my system it wont compile with the changed code.
    Your example program compiles for me, but if it does not compile, it could be because you did not #include <cstdlib> for std::system().

    Quote Originally Posted by Ducky
    Isnt memory freed when you close a program even if you didnt freed it in your program?
    Generally, yes, the operating system will reclaim the memory allocated to the process.

    By the way, this results in the dereferencing of a null pointer:
    Code:
    cout<< "Value of *pPointer before:"<< *pPointer <<endl;
    Incidentally, instead of this:
    Code:
    pPointer = new int;
    
    *pPointer = 25;
    you can write:
    Code:
    pPointer = new int(25);
    But then you should not be using a global variable; you can pass the pointer by reference instead.
    Last edited by laserlight; 03-04-2009 at 09:25 AM.
    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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Dwks!

    Well, you use *pPointer (i.e. dereference pPointer) before setting it to anything.
    I set it to 25 in the function.
    Or main() dont see the memory allocated in the function if you dont call the function itself?

    And even if i change it to your version it wont compile (it compiles the old code )
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    I set it to 25 in the function.
    Yes, but you call the function after dereferencing the pointer.

    Quote Originally Posted by Ducky
    Or main() dont see the memory allocated in the function if you dont call the function itself?
    Since pPointer is global, it has static storage duration and is thus zero initialised, hence it is a null pointer when control enters the global main() function.

    Quote Originally Posted by Ducky
    And even if i change it to your version it wont compile (it compiles the old code )
    That sounds like a problem with your development environment rather than your code per se. You might want to start over with a new project if you are unable to fix this problem.
    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
    Join Date
    Dec 2007
    Posts
    932
    Thank you Laserlight!

    Yes, but you call the function after dereferencing the pointer.
    Why would this be wrong?
    Dereferencing wont change anything it just looks at the value stored at its address.
    Isnt it?
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    Why would this be wrong?
    Dereferencing wont change anything it just looks at the value stored at its address.
    So, what is the address contained in pPointer before you call SomeFunction()?
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    lol You got me! There is none.

    So i got to point it to somewhere first. Thanks!
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ducky View Post
    lol You got me! There is none.

    So i got to point it to somewhere first. Thanks!
    Yes. And it being a global or local makes no difference here - if it's global it contains NULL, if it's a local, it contains whatever happened to be at that address, which is quite possibly completely meaningless as a pointer (e.g. some text, remnants of a floating point number), but it CAN be a valid memory address!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Matsp!

    I changed the code, its working now.

    Also, see the FAQ for alternatives to system("pause"), which I dislike.
    With system() its easy to close your program.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int  x;
    int *pPointer = &x;
    
    void SomeFunction()
    {
        // make pPointer point to a new integer
    
        pPointer = new int;
        
        *pPointer = 25;
    }
    
    int main()
    {
        cout<< "Value of *pPointer before:"<< *pPointer <<endl;
        
        SomeFunction(); // make pPointer point to something
    
        cout<< "Value of *pPointer after: "<< *pPointer <<endl;
        
        delete pPointer;
    
        system ("pause");
    }
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  5. Allocating and deleting memory
    By subdene in forum Windows Programming
    Replies: 3
    Last Post: 12-11-2002, 05:44 AM