473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dangling pointer quiz question

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

Oct 15 '05 #1
20 6547
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
__PPS__ <i-*********@yande x.ru> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #2
In article <di**********@p anix2.panix.com >,
Greg Comeau <co****@comeauc omputing.com> wrote:
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
__PPS__ <i-*********@yande x.ru> wrote:
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.


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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #3
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 15 '05 #4
In article <43************ ***@news.indivi dual.net>,
Alf P. Steinbach <al***@start.no > wrote:
* 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.

Unfortunatel y 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 :)
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #5
* Greg Comeau:
* Alf P. Steinbach:
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.


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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 15 '05 #6

"__PPS__" <i-*********@yande x.ru> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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.
Oct 16 '05 #7

Branimir Maksimovic wrote:
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.


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

Oct 16 '05 #8
* __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.or g/wiki/Dangling_pointe r> (who takes on the job?).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 16 '05 #9

"__PPS__" <i-*********@yande x.ru> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .

Branimir Maksimovic wrote:
> 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.


"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.

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.


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.

Oct 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3073
by: Aravind | last post by:
I would like to know in what manner dangling pointers affect the security of a application developed using C++.What are the loopholes that are created by dangling pointers and how they could be exploited by hackers?. Aravind
11
1928
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
2
2630
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
1968
by: Richard | last post by:
My experience has always been that you're SOL when trying to safely detect and stop references to dangling memory (non-null pointers to free'ed blocks) at runtime (C99, Linux). Maybe somebody clever has worked this out, though? (Apologies to those who find the question off topic for CUP or CLC)
4
7459
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of questions and answers to cycle through until the last question? Also, how can I give him the score after the last question. Thank you in advance. DAL. ...
1
4415
by: sekhar_ps | last post by:
if we store some value at the place in memory which void pointer references then we increment void pointer this leads to dangling pointer?can any one explain whats the reasons for dangling pointer
0
2990
by: dradhakr | last post by:
I need to put 5 question only,that's randomly in flash,how can i write script in flash <?xml version="1.0"?> <!DOCTYPE quiz > <quiz> <title>The Quiz</title> <items> <item> <question>In which continent is the country Japan located?</question> <answer correct="y">Asia</answer>
5
6612
by: madhusagar79 | last post by:
I got one issue to find the Dangling Pointer. Is there any way to find the Dangling Pointer?
1
2469
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) { char *a,*b,*c; a = (char *)malloc(40); b = a; c...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.