Home > Software engineering >  Beginner questions, how to delete the entire new object?
Beginner questions, how to delete the entire new object?

Time:09-26

# include & lt; iostream>
The class Stringbad
{
Private:
Int na;
Int nb.
Public:
Stringbad (int a, int b);
Void the show ();
};
//////////////////////////////////////
Void Stringbad: : show ()
{
Cout<" Na="& lt; Cout<" Nb="& lt; }
//////////////////////////////////////
Stringbad: : Stringbad (int a, int b)
{
Na=a;
Nb=b;
}

/////////////////////////////////////
Int main (int arg c, char * argv [])
{
using namespace std;
Stringbad * p=new Stringbad (5, 8);//can you tell me how to delete off
p-> Show (); The whole object p//with the new assignment?
Coutdelete p;//this na can only delete members, why?
Cout<" The delete p "& lt; p-> Show ();

cin.get();
cin.get();
return 0;
}

C - run results of free
Na=5
Nb=8

The delete p
Na=0
Nb nb=0=8//way to ah, why?

CodePudding user response:

Have found the reason, the compiler,
VC6 running result is:
Na=5
Nb=8

The delete p
Na=- 572662307
Nb=- 572662307
Fully in line with expectations
constantly trial and error, is a good way to learn c + +,

CodePudding user response:

# include & lt; iostream>
The class Stringbad
{
Private:
Int na;
Int nb.
Public:
Stringbad (int a, int b);
Void the show ();
};
//////////////////////////////////////
Void Stringbad: : show ()
{
Cout<" Na="& lt; Cout<" Nb="& lt; }
//////////////////////////////////////
Stringbad: : Stringbad (int a, int b)
{
Na=a;
Nb=b;
}

/////////////////////////////////////
Int main (int arg c, char * argv [])
{
using namespace std;
Stringbad * p=new Stringbad (5, 8);//can you tell me how to delete off
p-> Show (); The whole object p//with the new assignment?
Coutdelete p;//this na can only delete members, why?
Cout<" The delete p "& lt; p-> Show (); //p has been deleted, here it is illegal to call

cin.get();
cin.get();
return 0;
}

CodePudding user response:

"This sentence can only delete members na, why?"
The delete not remove na nb!

CodePudding user response:

Is so delete don't believe that the compiler

CodePudding user response:

After the delete, just released, the address in a short period of time using the address, no other programs before the pointer to the address or location,
Generally use the delete after all need the pointer to null

CodePudding user response:

reference 5 floor qq_30350197 reply:
after the delete, just released, the address in a short period of time when using the address, no other programs before the pointer to the address or location,
Generally use the delete after all need the pointer to an empty

Prevent wild pointer, good habits,

CodePudding user response:

using namespace std;
Stringbad * p=new Stringbad (5, 8);//can you tell me how to delete off
p-> Show ();//use new memory allocated Stringbad object is, p is a pointer to the block of memory
Coutdelete p;//have to release the memory. But the pointer will not automatically empty, at this point p is a pointer to the wild (concept would say) below should be combined with p=NULL;
Cout<" The delete p "& lt; p-> Show ();

cin.get();
cin.get();
return 0;
}

C - run results of free
Na=5
Nb=8

The delete p
Na=0
Nb=8 no Aaron na nb//how much is the value of the is wrong, wild pointer error bring unpredictable...
  • Related