Thread: Question about delete pointer

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    14

    Question about delete pointer

    Hi everyone..
    How can i delete a pointer in a member function which return that pointer
    for example:

    Code:
    Node<T>* Rpointer(T item)
    {
    	Node<T> *t11;
    	t11=head;
    	while( t11 != NULL && t11->data != item)
    	{
    		t11=t11->next;
    	}
    	if(t11==NULL)
    	{	cout<<"Not Found";
    		delete t11;
    		return 0;
    	}
    	return t11;
    }
    I hope you answer me

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Are you sure you should delete the pointer? Because you're deleting "head"; which isn't allocated in this function.
    If you're sure, simply write "delete ptr;", where ptr is the pointer you want to delete. What exactly are you having trouble with?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    14
    The trouble is where can i type delete ptr
    after return t11 or before it??
    I think the both cases are wrong, I tried in the second case but the program hung maybe because it delete the ptr then retutn it...?
    Last edited by almawajdeh; 11-23-2010 at 11:06 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by almawajdeh
    The trouble is where can i type delete ptr
    after return t11 or before it??
    I think the both cases are wrong, I tried in the second case but the program hung maybe because it delete the ptr then retutn it...?
    It is definitely wrong to delete t11 after the return statement since that statement would then be unreachable. It would definitely be wrong to delete t11 just before the return statement since the caller would then be returned a pointer to an object that no longer exists.

    What is this member function supposed to do? I gather that it is supposed to find an object that is equal to item. Perhaps what you want to do is to save a pointer to the next element, then delete t11, and then cause t11 to point to that next element so as to return t11.
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    After outputting Not Found, there is no point in deleting t11 because you only output that if t11 is NULL, therefore it would always be doing delete NULL, which is pointless.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    14
    So, what is the solution?
    I didn't understand what laserlight meant
    and thanks iMalc for the note

    But, Do we have to delete the static pointers or the deletion for Dynamic pointers only?
    Last edited by almawajdeh; 11-25-2010 at 04:23 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by almawajdeh View Post
    So, what is the solution?
    I didn't understand what laserlight meant
    and thanks iMalc for the note

    But, Do we have to delete the static pointers or the deletion for Dynamic pointers only?
    You only have to/are allowed to delete pointers that are dynamically allocated with new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on overloading the operator delete...
    By Darkinyuasha1 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2009, 11:50 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  4. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  5. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM