Dangling pointer quiz question

P

__PPS__

Hello everybody

in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"

My answer was:
"dangling pointer is a pointer that points to some area of ram that's
not reserved by the program. Accessing memory through such pointer is
likely to result in core dump (memory access violation)"

This was one of the two questions in a 10-min quiz (I was 5 mins late
for this quiz :)

This question is marked based on 3, I got 2 for this question and
correction was that dangling prt is pointer that "..points to invalid
memory". Is this reasonable to ask for 3/3 for my answer?

I got 2/7 for the second question, but I'll fix that problem - there
I'm 100% sure it must be 7/7 ...
 
G

Greg Comeau

in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"

My answer was:
"dangling pointer is a pointer that points to some area of ram that's
not reserved by the program. Accessing memory through such pointer is
likely to result in core dump (memory access violation)"

This was one of the two questions in a 10-min quiz (I was 5 mins late
for this quiz :)

This question is marked based on 3, I got 2 for this question and
correction was that dangling prt is pointer that "..points to invalid
memory". Is this reasonable to ask for 3/3 for my answer?

First, it's worth pointing(!) out that this is not any sort
of official Standard C++ term. That said, you're answer only
laid out one type of invalid memory, but it could include even
memory that is in the program, etc. So, if you got 2/3, be happy.
 
G

Greg Comeau

First, it's worth pointing(!) out that this is not any sort
of official Standard C++ term. That said, you're answer only
laid out one type of invalid memory, but it could include even
memory that is in the program, etc. So, if you got 2/3, be happy.

BTW, normally for a pointer to dangle, it normally would have
previously pointed to valid memory. Note that valid memory is
not only memory that the program has valid access to but also
that the pointer previously had valid access to. For instance,
the return value of new, or malloc, but which was then delete'd,
or freed(). And hence the pointer is said to "dangle", since it
need not be reset to something valid, or purposely invalid (like
the null pointer). It seems to have muddled over the years to
include "any invalid pointer", even ones not dangling.
 
A

Alf P. Steinbach

* Greg Comeau:
need not be reset to something valid, or purposely invalid (like
the null pointer).

Well, the null pointer is certainly valid for the purposes of copying the
pointer value and comparing it to other pointer values wrt. equality.

Unfortunately the standard does not fully define the terms invalid and valid
for pointers, giving only one example of invalid (pointing to deallocated
storage), and vaguely implying another (indeterminate pointer value).

As a working definition I prefer to consider the nullpointer a valid pointer
(as well as e.g. pointer to one past end of array), because that way one can
say simply that _any_ use of an invalid pointer is -- probably! --
Undefined Behavior (it would be nice if the standard was clear). For the
kinds of pointer this definition says are valid pointers there are then levels
of what you can do: no dereferencing (nullpointer), dereference but don't do
anything that requires an actual object (one past end of array, destroyed but
not deallocated object, zero size new'ed array), compare by equality but not
otherwise when using built-in operators (pointer to single object), and so on.

If we need a general term for a pointer that actually points to a useable
object -- and there is no term for it as far as I know, it certainly isn't
covered by general validity -- I propose we call them RealGood pointers.
Then those that are not RealGood are Un-RealGood, or URG, pointers. ;-) An
URG pointer can be valid or invalid, and if it's invalid, all uses are
(probably!) Undefined Behavior.

Otherwise one would have to get into gray-zones of validity.
 
G

Greg Comeau

* Greg Comeau:

Well, the null pointer is certainly valid for the purposes of copying the
pointer value and comparing it to other pointer values wrt. equality.

Unfortunately the standard does not fully define the terms invalid and valid
for pointers, giving only one example of invalid (pointing to deallocated
storage), and vaguely implying another (indeterminate pointer value).

As a working definition I prefer to consider the nullpointer a valid pointer
(as well as e.g. pointer to one past end of array), because that way one can
say simply that _any_ use of an invalid pointer is -- probably! --
Undefined Behavior (it would be nice if the standard was clear). For the
kinds of pointer this definition says are valid pointers there are then levels
of what you can do: no dereferencing (nullpointer), dereference but don't do
anything that requires an actual object (one past end of array, destroyed but
not deallocated object, zero size new'ed array), compare by equality but not
otherwise when using built-in operators (pointer to single object), and so on.

If we need a general term for a pointer that actually points to a useable
object -- and there is no term for it as far as I know, it certainly isn't
covered by general validity -- I propose we call them RealGood pointers.
Then those that are not RealGood are Un-RealGood, or URG, pointers. ;-) An
URG pointer can be valid or invalid, and if it's invalid, all uses are
(probably!) Undefined Behavior.

Otherwise one would have to get into gray-zones of validity.

I agree that a null pointer is a valid pointer, at least as
far as null pointerness is concerned, and indeed it can be
copied, tested, etc. Quite so. I proably should have said
"purposely invalid" to acknowledge that, and that it does not
point at any valid memory. Hopefully that weasels me out of this :)
And let's leave void * out of eveything. More importantly,
I guess some invalid URGs can be a valid pointers it would seem :)
 
A

Alf P. Steinbach

* Greg Comeau:
* Alf P. Steinbach:


I agree that a null pointer is a valid pointer, at least as
far as null pointerness is concerned, and indeed it can be
copied, tested, etc. Quite so. I proably should have said
"purposely invalid" to acknowledge that, and that it does not
point at any valid memory. Hopefully that weasels me out of this :)
And let's leave void * out of eveything. More importantly,
I guess some invalid URGs can be a valid pointers it would seem :)

Uh, well. Again. ;-) I meant for RealGood/URG and valid/invalid to largely
orthogonal concepts, except that RealGood implies valid. An invalid URG is
then just a pointer that is both URG (not RealGood) and invalid. Hence it is
invalid.

Cheers,

- Alf


PS: Is this distinction really useful, do you think?
 
B

Branimir Maksimovic

__PPS__ said:
Hello everybody

in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"

My answer was:
"dangling pointer is a pointer that points to some area of ram that's
not reserved by the program. Accessing memory through such pointer is
likely to result in core dump (memory access violation)"

Almost completely false.
Dangling pointer usually points to valid memory in program code,
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 :)

That is about it.

Greetings, Bane.
 
P

__PPS__

Branimir said:
Almost completely false.

"Almost completely false" means that dangling pointer is a pointer that
points to some area of ram that IS reserved by the program. Accessing
memory through such pointer is NOT likely to result in core dump
(memory access violation)
that's how I understand your sentence. Do you really think that I made
completely wrond answer ?!?!? As Greg pointed out, ther's no precice
definition of dangling pointer in c++, don't forget about that - many
thing could be dangling pointer.

Dangling pointer usually points to valid memory in program code,

I don't agree with you, even if you try google, you'll see that
dangling pointers are mostly called as pointers that point nowhere.
Well, you could be right, cause on most of the implementations if you
new an object and delete it and then new an object of the same class
again, it will most likely be constructed on the place of the
previously deleted one, and both pointers will point to the same new
object:
X *a = new X();
delete a;
X *b = new X();
? a==b ?, is a now dangling?

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.


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 :)


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 :)
 
B

Branimir Maksimovic

__PPS__ said:
"Almost completely false" means that dangling pointer is a pointer that
points to some area of ram that IS reserved by the program. Accessing
memory through such pointer is NOT likely to result in core dump
(memory access violation)
that's how I understand your sentence.

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
completely wrond answer ?!?!?

Yes, you've missed the point.

As Greg pointed out, ther's no precice
definition of dangling pointer in c++, don't forget about that - many
thing could be dangling pointer.

No, but that is common term.
I don't agree with you, even if you try google, you'll see that
dangling pointers are mostly called as pointers that point nowhere.

Problem is that all pointers point to somewhere.
Even NULL pointer can point to somewhere.
There are implementations where every pointer value refers to accessible
memory.
Well, you could be right, cause on most of the implementations if you
new an object and delete it and then new an object of the same class
again, it will most likely be constructed on the place of the
previously deleted one, and both pointers will point to the same new
object:
X *a = new X();
delete a;
X *b = new X();
? a==b ?, is a now dangling?

Of course.
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.
 
A

Alf P. Steinbach

* __PPS__:
in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"

My answer was:
"dangling pointer is a pointer that points to some area of ram that's
not reserved by the program. Accessing memory through such pointer is
likely to result in core dump (memory access violation)"

A dangling pointer is a pointer value that was valid and has become invalid.

The key is the _transition_ from valid to invalid.

It's not defined in the C++ standard and has little to do with C++ except the
frequency with which dangling pointers occur in C++ programs. ;-)

The area of memory pointed to can still be reserved by the program, and in
fact that's most often the case.

For a lot of erronous details but also some enlightening ones, see <url:
http://en.wikipedia.org/wiki/Dangling_pointer> (who takes on the job?).
 
B

Branimir Maksimovic

Alf P. Steinbach said:
* __PPS__:

A dangling pointer is a pointer value that was valid and has become
invalid.

The key is the _transition_ from valid to invalid.

It's not defined in the C++ standard and has little to do with C++ except
the
frequency with which dangling pointers occur in C++ programs. ;-)

The area of memory pointed to can still be reserved by the program, and in
fact that's most often the case.

For a lot of erronous details but also some enlightening ones, see <url:
http://en.wikipedia.org/wiki/Dangling_pointer> (who takes on the job?).
Heh, they say that dangling pointer is wild pointer. That's true in some
sense, but wild pointer is unitialized pointer and dangling pointer is
initialized
one :)

Greetings, Bane.
 
B

Branimir Maksimovic

Alf P. Steinbach said:
* __PPS__:

A dangling pointer is a pointer value that was valid and has become
invalid.

The key is the _transition_ from valid to invalid.

It's not defined in the C++ standard and has little to do with C++ except
the
frequency with which dangling pointers occur in C++ programs. ;-)

The area of memory pointed to can still be reserved by the program, and in
fact that's most often the case.

For a lot of erronous details but also some enlightening ones, see <url:
http://en.wikipedia.org/wiki/Dangling_pointer> (who takes on the job?).

http://en.wikipedia.org/wiki/Wild_pointer

Look at this contradictions of definitons :) They have to decide what is
what.

Greetings, Bane.
 
P

__PPS__

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 :)
 
P

__PPS__

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


Hey, that was infinity sign, don't know why google flipped it :)
 
B

Branimir Maksimovic

__PPS__ said:
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.

I said almost completely false.
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

int *a = reinterpret_cast<int*>(0x554345);

No, a is not dangling pointer. You've initialized it with specific value
which you expect leads to non const integer object. This may be correct
or incorrect, but implementation allows you to that :)
There is nothing dangling here.

you have to do
but NULL pointer is not dangling

You said nowhere?
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.

Of course.

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

No, I'm saying that you were almost completely false, but I would give
you 3 since there is no definition of dangling pointer in C++.
So any answer is valid regarding C++. :)
Thanks you everybody for your answers, in case someone asks me, I'll be
able to give a better answer on this question :)

Greetings, Bane.
 
G

Greg Comeau

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. :)

Come on. Everybody knows that's called being stood up.
 
B

Branimir Maksimovic

Greg Comeau said:
A wild pointer is a pointer that is wild,
unitialized or not.

So dangling pointer is wild pointer but wild pointer is not necessarily
dangling pointer? Ok, I used term wild pointer just for unitialized ones.

Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top