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
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 |
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 |
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 |
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 |
Do array subscripts always start with zero?
How we can set and clear bit in a byte using macro function?
Difference between Shallow copy and Deep copy?
Where can I get an ansi-compatible lint?
What is linear search?
I have a function which accepts, and is supposed to initialize,a pointer, but the pointer in the caller remains unchanged.
What are structure members?
what is the output? #define fun(a,b,t) (g ##t=(a),(a)=(b),(b)=g##t) float gfloat; main() { float a=1.12,b=3.14; fun (a,b,float); printf("na=%4.2f,b=%4.2f",a,b); } A)Error in Defining Macro B)a=1.12,b=3.14 C)a=3.14,b=1.12 D)None of the Above
3 Answers Accenture, Infosys, Wipro,
In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between 1 and N.Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.
Write a program to find the smallest and largest element in a given array in c language
Can you mix old-style and new-style function syntax?
What is the use of pointers in C?
0 Answers Impetus, Motorola, Tavant Technologies, Virtusa,