English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For instance, in C++:

int *x;
int *y = new int;
*y=16;
x=y;
delete y;
cout<
what will be printed out (nothing or 16)? and why? thanks a lot

2007-10-05 07:41:18 · 8 answers · asked by Anonymous in Computers & Internet Programming & Design

well yeah. i guess, i made a mistake while typing..
cout<<*x< so not the address but value that this address holds

2007-10-05 08:15:08 · update #1

8 answers

Asuming that the cout line is cout << *x << endl:
======================================
The right answer to this question is: undefined. If the particluar memory space still contains 16, it may print 16. But if any other program has now snacthed it and used it for some other purpose, it may be that value. It may be garbage, it may crash, it may punch you on the nose with a hand coming out of the monitor.

The right advice for people who try such things: use C++ the right way. Respect the flexibility that pointers allow you to have. Do not try to access memory that you have not been assigned or you have deleted. What you are trying to do is interacting with a dead person. The ghost might get you!

And yes, I forgot to mention that some compilers do set the value to zero, as one of the answers above states it will be zero. That answer is strictly compiler specific. And such a compiler is too gentlemanly I would say. You should try to write code in such a way that most if not all good compilers run it smoothly.

2007-10-05 08:58:02 · answer #1 · answered by Koushik Biswas 3 · 0 1

It will print out the memory location of y because you assigned the y to int x, but y is empty.

2007-10-05 07:48:04 · answer #2 · answered by see0emily0play 3 · 0 1

the problem is that on the time whilst this gadget tries to delete the dynamic memory that ptr factors to, ptr does not in fact factor to dynamic memory. In somewhat greater intensity: once you write the string literal "try", the compiler instruments aside a static area of memory and places that string in there. once you tell ptr to point obtainable, ptr in basic terms factors to that memory (which subsequently reasons the 5 bytes you in basic terms allotted to be leaked, it fairly is a bad component yet considering the fact which you're in basic terms doing it as quickly because it won't harm this gadget). Then once you tell this gadget to delete the dynamic memory that ptr factors to, the laptop shows that ptr factors to a static area of memory, which won't be deleted. So it crashes, it fairly is precisely what we'd anticipate it to do. If what you incredibly needed to do grew to become into to repeat the contents of the static string "text cloth" into the hot memory you allotted (and then unfastened that dynamic memory), the approach of do it fairly is somewhat diverse. seem up applications inclusive of 'strcpy()' and 'memcpy()' for greater counsel.

2016-10-21 03:39:18 · answer #3 · answered by ? 4 · 0 0

0 is printed due to null pointer assignment

2007-10-05 10:13:00 · answer #4 · answered by Shariq (http://coinsindia.info) 5 · 0 0

The answer is 0

If you comment out the line
delete y;

then the answer is 16.

2007-10-05 08:27:18 · answer #5 · answered by AnalProgrammer 7 · 0 1

The correct answer is it's undefined, because you're accessing memory that's been deallocated.

2007-10-05 22:08:03 · answer #6 · answered by tm314159 2 · 0 0

I compiled your code and ran it and I got an outcome of: 0x32460

I'm not sure why this is because I'm new to C++ (and programming in general).

2007-10-05 08:01:33 · answer #7 · answered by mustangmanbiffy 3 · 0 1

it wont print anything,
the results will pretty much just be nothing,
you should download dev-c++

2007-10-05 07:49:02 · answer #8 · answered by g0ds_s0n_666 2 · 0 3

fedest.com, questions and answers