Like Tree1Likes
  • 1 Post By grumpy

Pointer Question

This is a discussion on Pointer Question within the C++ Programming forums, part of the General Programming Boards category; Hey I am currently teaching myself C++ programming and I have a question about when I need to delete pointers ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    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,432
    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 03:19 PM.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

  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, 10:59 AM
  2. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 01: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

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