Thread: Pointer Question

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Pointer Question

    Hey I am currently teaching myself C++ programming and I have a question about when I need to delete pointers to free up memory. I have been going through the examples in my book I believe I somewhat understand what pointers are used for and also have read that eventually I will know better when I should use them though I am wondering do you always have to delete a pointer before the program ends? The examples I have been doing always delete the pointer at the very end of the program and I was wondering is this actually something needed to be done so the pointer does not eat up memory even after the program is done running or is it just something that is done in the examples to help get people into the habit of deleting their pointers?

    This is one of the examples that I am talk about how it has the "delete ps;" right at the end.

    Code:
    // newstrct.cpp -- using new with a structure
    #include <iostream>
    
    
    struct inflatable   //definition of structure
    {
        char name[20];
        float volume;
        double price;
    };
    
    
    int main()
    {
        using namespace std;
        inflatable *ps = new inflatable;    //allot memory for structure
        cout<< " Etner name of inflatable item: ";
        cin.get(ps->name, 20);              //method 1 for member access
        cout<< "Enter volume in cubic feet: ";
        cin >> (*ps).volume;                //method 2 for member access
        cout<< "enter price: $";
        cin >> ps -> price;
        cout<< "Name: " << (*ps).name << endl;      //method 2
        cout<< "Volume: " << ps->volume << " cubic feet\n"; //method 1
        cout<< "Price: $" << ps ->price << endl;            //method 1
        delete ps;                                  //free memory used by structure
        return 0;
    }
    If anyone is able to answer or even just give me advice on pointers in general it would be fairly helpful because out of all of the topics I have learned about so far I understand pointers the least.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Strictly speaking, no you don't need to delete a pointer allocated through new when a program ends, as a modern OS will clean up after your memory leak. However, it is a good (and standard) practice to always delete dynamically allocated memory. To quote Nike: Just Do It. Start off on the right foot by developing good habits.

    Note that (in my experience) in the real world you will rarely, if ever, see this notation for accessing a struct element through a pointer:
    Code:
    (*ps).name
    it will nearly always be
    Code:
    ps->name
    The former is mainly shown as an academic exercise.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Consider using std::string instead of char.
    It might be a good idea to research smart pointers now that you understand dynamic allocation.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rags_to_riches View Post
    Strictly speaking, no you don't need to delete a pointer allocated through new when a program ends, as a modern OS will clean up after your memory leak.
    Apart from the fact that there are real-world scenarios where a program runs without a modern operating system there is also often more to deleting objects than releasing memory. If destruction of an object has side effects other than cleaning up memory - where relying on an OS to clean up is insufficient. Examples include releasing system resources (mutexes, etc), finalising database transactions, etc etc.

    Yes, I know that people insist you can always modernise an operating system. That is fine in a one-person code base (where you have absolute control). It is not fine if you are subject to political or financial constraints that prevent selecting the perfect operating system for the job, etc etc.

    Also, if program code is eventually reused as part of some larger code base (eg copied and pasted as one function within a long-running program) then forgotten cleanup operations will become memory leaks.
    Last edited by grumpy; 02-11-2012 at 04:19 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Thanks everyone for the response, I was mostly worried that I could do something wrong with pointers and completely screw up my computer if I didnt delete them on accident. I also know that some of the things in code is not how they are commonly used or not the most optimal way to do it but the aurthor did mention how ps->volume is the more widely used method. For the char array he has I believe he was showing how an array is similar to a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  5. Pointer to pointer question
    By caduardo21 in forum C Programming
    Replies: 7
    Last Post: 07-18-2005, 04:03 PM