No, you missed the point. To resolve your confusion let's ask
another question:
What is the difference between dangling pointer
and wild pointer? Both can point to memory that is/not accessible
by program code.
Do you really think that I made
Yes, you've missed the point.
Yes means I made completely wrong answer, is that what you meant? Be
clear
Or you're saying that I gave difenition to wild pointer
well, in
some schools same things can be called differently, so let's call all
of them dangling to avoid further confusion...
The thing is that I agree that there are many cases/ways to create
dangling pointers, but you don't seem to agree with my answer that
pointer pointing to memory unreserved by the process is dangling.
int *foo(){
int x;
return &x;
}
returns a dangling pointer, but the returned address is likely to be
somewhere within plogram's instruction's block, in this case my answer
is wrong
-OR-
int *b =new int;
int *a =0x554345;
a is certanly a dangling pointer
, but there is a small chance that
a==b
As Greg pointed out, ther's no precice
No, but that is common term.
Problem is that all pointers point to somewhere.
Even NULL pointer can point to somewhere.
but NULL pointer is not dangling
There are implementations where every pointer value refers to accessible
memory.
Of course.
I seem to made a point against myself here
I have show that pointer
pointing to reserved memory is dangling, forget about this example
OR
int *i= new int;
delete i;
char *c= new char;
? i==c ?, also very likely, but from my definition i became dangling
after when I deleted i.
Yes.
except that pointed object was replaced by other object or
nothing is there any more (in that case it can point to memory
that is not accesible by the code any more).
As we all now "delete" and "free" are generators of dangling pointers,
but there are other cases as well.
example:
static char* buf = new char[sizeof(int)];
int *i = new(buf) int(10);
// .....
int *j = i;
*j = 42;
// ......
// and somewhere in code someone writes
int* k = new (buf) int(2);
// .......
j is now dangling pointer, points to wrong int.
then comes this assert
assert(*j == 42); // and assert fails
// programmer wonders why doesn't this work
There's nothing wrong, it should assert, if placement new creates
int(2) at buf. Interesting thing in this case we will have to delete
buf three times now
What is the difference between this example and delete new cycle?
It is actually what happens in implementation
of new and delete, just simplified
PS. I forgot to mention the other part of the question that I felt
wasn't important. It was: "Explain in less thatn 5 short sentences What
dang..."
It's just beginners class, no complicated stuff necessary to get 3/3
for this question - just give right answer
Well simply explained, let's say Merry had telephone number
111-2222-333. For some reason Merry moved out to other place,
and you can't call her with that number anymore. Let's say when
you call that number now Mark answers the phone.
Such number is called dangling pointer.
Greetings, Bane.
But what happens if no one picks up? or you hear that the number is not
in service? doesn't that also mean a dangling pointer. You say I'm
completely wrong because I missed the point. Someone in this thread has
pointed out that my answer wasn't full, so 2/3 is ok for my answer,
what you are saying is that it should be 0/3 cause: "Do you really
think that I made completely wrong answer ?!?!?" - "Yes,...", and
completely wrong means 0/8
Thanks you everybody for your answers, in case someone asks me, I'll be
able to give a better answer on this question