Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


int main()
{
int *p=new int;
*p=10;
del p;
cout<<*p;
*p= 60;
cout<<*p;
}
what will be the output & why?

Answers were Sorted based on User's Feedback



int main() { int *p=new int; *p=10; del p; cout<<*p; ..

Answer / sethuu

First output will print some garbage value because you
delete the object p and the you print.

The second one will print value 60.

Is This Answer Correct ?    11 Yes 3 No

int main() { int *p=new int; *p=10; del p; cout<<*p; ..

Answer / jaroosh

There will be an error because its "del" instead of "delete".
In this particular program, you might actually get away with
using p after delete, but lets look what happens here :

int *p=new int;
*p=10;
cout << "ADDRESS OF P is " << p << endl;
delete p;
int * x= new int(3);
cout << "ADDRESS OF X=" << *x << " is " << x << endl;
*p = 10; //We think we got away with deleting p so why not
//still use it!
cout << "VALUE OF X : " << *x; //Here is why...

now, though its basically a policy of compiler, here is what
probably WILL happen in the output (memory addresses are
exemplary):
ADDRESS OF P is 0x272740
ADDRESS OF X=3 is 0x272740
VALUE OF X : 10
Now this is totally NOT what we would like our program to
do, and this is because though delete WILL mostly call
objects destructors and sometimes even clear memory, but p
is STILL pointing to that location, to which compiler
assumes it is safe to allocate variable x!
So now we end up having ONE memory storage and two pointers
p and x pointing to it.
This is why though it will not crash your compilation and
probably you can get away with no runtime errors, this leads
to serious troubles and as a rule NEVER use pointers after
you deleted their storage.

Is This Answer Correct ?    7 Yes 1 No

int main() { int *p=new int; *p=10; del p; cout<<*p; ..

Answer / hemang

There will be error because del is used instead of delete. in c++ delete is the keyword not del. delete is used to delete an object.

So program will not run....

Is This Answer Correct ?    6 Yes 0 No

int main() { int *p=new int; *p=10; del p; cout<<*p; ..

Answer / anu

I think error will come bcoz memory allocated to *P is
already deleted d it will point to nowhere now....

Is This Answer Correct ?    3 Yes 5 No

Post New Answer

More C Interview Questions

what is the differnce between AF_INET and PF_INET?

5 Answers   Systems Plus, Wipro,


what are the difference between ANSI C and Let Us c and Turbo C

4 Answers   LG Soft,


What is atoi and atof in c?

0 Answers  


What is function prototype in c with example?

0 Answers  


What is the difference between malloc() and calloc() function in c language?

0 Answers  


How does C++ help with the tradeoff of safety vs. usability?

1 Answers  


What does typedef struct mean?

0 Answers  


wat is the difference between array and pointer?

4 Answers   Wipro,


what is the meaning of java that is (J A V A) full form of JAVA

71 Answers   AKS University, Bhel, BNL, BPO, HCL, Peacecon,


what does keyword ‘extern’ mean in a function declaration?

1 Answers   Emerson,


Do string constants represent numerical values?

0 Answers  


while running a program, i got the msg that press return key to exit.what that mean in C as there are no such options as far i know.

1 Answers   TCS,


Categories