|
-
February 15th, 2008, 02:07 AM
#1
How to delete a pointer
Having a little trouble deleting a pointer, returns only jumbled up numbers...
Code:
void deletefn(nametype *head)
{
int option = 0;
nametype *currptr, *deleteptr;
currptr=head;
while(option!=1 && currptr != NULL)
{
cout<<"\nCurrent product is: "<<currptr->description<<endl;
cout<<"Press 1 to delete this product\n "
<<" 0 to continue: ";
cin>>option;
if(option != 1) currptr=currptr->link;}
if(option == 1)
{
currptr = deleteptr;
delete deleteptr;
deleteptr = NULL;
}
else{cout<<"Could not add node to the link."<<endl;}
}//End of deletefn
-
February 15th, 2008, 02:17 AM
#2
Re: How to delete a pointer
 Originally Posted by motorizedmedia
Having a little trouble deleting a pointer, returns only jumbled up numbers...
I only see a single function that returns void.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Regards,
Paul McKenzie
-
February 15th, 2008, 03:07 AM
#3
Re: How to delete a pointer
I don't know, what you are trying to achieve (your code is horribly indented and a function called deletefn that prints out a message "Could not add node to the link." does not make much sense in my mind), but deleteptr remains uninitialized throughout your function, so running delete on it might not have the desired effect.
Most compilers issue warnings in case variables are used uninitialized. Either you did not read them or you should check your compiler settings.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 15th, 2008, 03:54 AM
#4
Re: How to delete a pointer
deleteptr.
You are effcetively doing something like the following..
Code:
Member *deleteptr;
anotherptr = deleteptr; // here deleteptr is uninitialized dangling pointer
delete deleteptr;// deleting a uninitialized pointer invoking undefined behaviour
anotherptr->SomeMethod();//calling a method on a deleted pointer which was an uninitialized pointer// Ofcourse undefined behaviour...
Dont forget to rate my post if you find it useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|