CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2007
    Posts
    425

    Releasing allocated memory

    Hello everyone, I'm usually over in the Java forums, and I'm back over to this realm following a numerical computing class I am registering for, and have to embarrassingly say that it's been quite some time since I've touched C++, and need two questions clarified.

    Let's start off with a simple data structure that we will use for this example; a node which will be used to maintain a list.
    Code:
    class Node {
    public:
    	int someValueThatMakesThisNodeImportant;
    	Node*	pNext;
    	Node*	pPrev;
    	Node(TYPE type, float p, float v);
    };
    At the end of my program execution, I've registered a function to perform some clean-up, from the tail of my list data structure all the way back to the head, and finally free (well, rather, delete) the memory associated to this structure's head node.

    So now to the questions:

    1) Whether or not I retain more than one pointer to a Node (or in this case, a subtype of node, but that's irrelevant), does it make a difference if I want to free the memory that any one of these pointers is pointing to (and this of course does assume that any other pointers will not be accidentally used in any other point of execution),

    and

    2) when using the delete keyword, do I simply specify delete on the pointer itself, or must i implicitly specify that I want to delete the memory associated to the address that the pointer is pointing to.

    Thanks for your time,
    Deliverance

  2. #2
    Join Date
    Feb 2008
    Posts
    48

    Re: Releasing allocated memory

    1) You are able to deallocate the memory pointed to by a pointer, irrespective of how many other pointers point there. The other pointers become 'dangling' and can *still be dereferenced*, giving undefined behaviour.

    2) You call delete on the pointer, which deallocates the memory if points to. You cannot call delete on anything but a pointer!

    I personally use references wherever possible, pointers only when necessary. References are much harder to abuse.

  3. #3
    Join Date
    Apr 2007
    Posts
    425

    Re: Releasing allocated memory

    Quote Originally Posted by Liche
    I personally use references wherever possible, pointers only when necessary. References are much harder to abuse.
    Thanks for the reply, yes, I am a lot more used to C, and as such I must say I have a bad habit of overly using pointers given the non-existence of references in C, but before going forward with this I am just making sure I release all my resources as required. Eventually I want to deploy my server to a production environment, and I don't want to find that a week later my server crashed.

    Cheers,

  4. #4
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507

    Re: Releasing allocated memory

    You can use standard list too, in that case you dont have to worry about de allocate the memory, standard list will do itself.

    Code:
    std::list<int> lstYourList;

  5. #5
    Join Date
    Apr 2007
    Posts
    425

    Re: Releasing allocated memory

    Quote Originally Posted by Zeeshan
    You can use standard list too, in that case you dont have to worry about de allocate the memory, standard list will do itself.

    Code:
    std::list<int> lstYourList;
    I realize there are a lot of built-in collection classes, but there are still cases where pointers are required, for custom data structures, and for those scenarios I will require that I have concrete knowledge of memory allocation in C++.

    Thanks for the point though.

  6. #6
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507

    Re: Releasing allocated memory

    Even if you have your own custom data you can make list of that.

    For example

    Code:
    struct sSubject
    {
            unsigned int                   m_iCourseID;
            std::string                      m_strName;
            unsigned int                   m_iCredit;
            std::string                      m_strTeacher;        
    };
    
    struct sStudent
    {
            unsigned int                     m_iRollNumber;
            std::string                        m_strName;
            std::vector<sSubject>     m_vecSubjects;
    };
    
    std::list<sStudent>    lstStudentsList;
    If you can give us information about your custom data structure, then we might come up with a standard library solution where you might not need to work with pointer and worry about deallocating the memory.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Releasing allocated memory

    No, it's definitely a good idea to learn the ins and outs of pointers and memory management, even if you simultaneously learn how to avoid using it more than necessary.

    1) Whether or not I retain more than one pointer to a Node (or in this case, a subtype of node, but that's irrelevant), does it make a difference if I want to free the memory that any one of these pointers is pointing to (and this of course does assume that any other pointers will not be accidentally used in any other point of execution),
    A pointer holds a location in memory. When you call delete on a pointer, the delete operation is done on that bit of memory it points to. If you've got that location stored in several places, it makes zero difference which one you use to access it when calling delete.

    Generally speaking, however, good design will make it clear that one pointer is "primary" and all others are merely conveniences; it wouldn't do to double-free something because you got confused.

    2) when using the delete keyword, do I simply specify delete on the pointer itself, or must i implicitly specify that I want to delete the memory associated to the address that the pointer is pointing to.
    The question doesn't make sense because there is no difference between the value of a pointer and the memory it points to.

  8. #8
    Join Date
    Apr 2007
    Posts
    425

    Re: Releasing allocated memory

    Quote Originally Posted by Lindley
    The question doesn't make sense because there is no difference between the value of a pointer and the memory it points to.
    What I meant by that, was originally I was unsure whether I specifically call delete on the pointer, or implicitly specify to delete the memory at the address directly.

    Code:
    char * a;
    //allocation here
    delete a;
    as opposed to
    Code:
    delete &a;
    I think this has been clarified now, thank you.

    @Zeeshan
    I agree that with any production environment I would 100% use STL whenever available, but more what I was getting at is when I do have to do something more customized (and I surely will), that I can guarantee that my memory is handled properly.

    As for that custom data structure, this is more of a test whether I can populate my own list implementation with vertices which I will be using to populate some models I am going to be drawing in DirectX. It's not a professional effort, just one to get me back into the groove of C++ development, and modify a few graphics projects I have been working on to eventually use my own library for these things.

    I can't really think of a concrete example where I will require to use pointers, but I'm thinking of a few scenarios, a few being:

    1) I am contracted for some Pro Services on a legacy piece of software, and it turns out really that the C++ project was really a mix of C and C++, and it was done with mixtures of malloc / free and new / deletes all using only pointers, as it was a student project.

    2) Writing a very efficient and small state machine (or any other application) that must fit on a small controller, (even though this generally would be done in C), where I can't afford the overhead of using STL and will likely use a custom data structure implemented myself.

    In short, I've had a very luxurious couple of years where I have had Java development jobs, and I am getting a little rusty with remembering the syntax of C++. I have done a bit of C on the side, but not much really, and I think it's about time I get back into it.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Releasing allocated memory

    Quote Originally Posted by Deliverance
    Code:
    char * a;
    //allocation here
    delete a;
    as opposed to
    Code:
    delete &a;
    I think this has been clarified now, thank you.
    Just to make sure: delete &a; isn't meaningful unless a is an object which has been allocated dynamically. Since such objects are almost always referred to by pointers anyway, you'll almost never see this.

    I will be using to populate some models I am going to be drawing in DirectX.
    Ehh.....DX.....[/OpenGL fan]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured