Basic interview question on pointers

P

pauldepstein

From a website:

25. Is there any problem with the following:
char *a=NULL; char& p = *a;?
The result is undefined. You should never do this. A reference must
always refer to some object.



I'm a bit puzzled. I would have said there are two problems. One is
mentioned on the website. However, isn't there another problem --
that it's wrong to dereference a null pointer?


Thanks,

Paul Epstein
 
S

Stefan Ram

25. Is there any problem with the following:
char *a=NULL; char& p = *a;?
However, isn't there another problem --
that it's wrong to dereference a null pointer?

One can not know whether »NULL« is a null pointer.
The following should be a legal C++ translation unit:

#define NULL "NULL"
int main(){ char *a=NULL; char& p = *a; }
 
T

Triple-DES

From a website:

25.    Is there any problem with the following:
char *a=NULL; char& p = *a;?
The result is undefined. You should never do this. A reference must
always refer to some object.

I'm a bit puzzled.  I would have said there are two problems.  One is
mentioned on the website.  However, isn't there another problem --
that it's wrong to dereference a null pointer?

Thanks,

Paul Epstein

If that website is the one I think it is, be very careful. Some of the
answers are fairly accurate, others are inaccurate (like this), and
some are just plain wrong.

See Q18 for an example of the latter.
Question: Can a constructor throw a exception? How to handle the error
when the constructor fails?
Answer: The constructor never throws a error.

This is blatantly false.
 
J

James Kanze

One can not know whether »NULL« is a null pointer.
The following should be a legal C++ translation unit:
#define NULL "NULL"
int main(){ char *a=NULL; char& p = *a; }

But only as long as no standard headers are included.
Realistically, you should be able to assume that NULL is the one
defined in <stddef.h>.
 
S

Stefan Ram

James Kanze said:
But only as long as no standard headers are included.

I did not see any include directives in the OP.
Realistically, you should be able to assume that NULL is the one
defined in <stddef.h>.

In the »real world« (when given code to review or to
refactor) I have a complete compilation unit
and can see which include directives are used.

This shows that not only in Usenet questions but
also in interviews, it is better to use an SSCCE.

http://sscce.org/
 
K

Kai-Uwe Bux

Stefan said:
I did not see any include directives in the OP.


In the »real world« (when given code to review or to
refactor) I have a complete compilation unit
and can see which include directives are used.

This shows that not only in Usenet questions but
also in interviews, it is better to use an SSCCE.

http://sscce.org/

Actually, it doesn't show that an SSCCE is better for interview questions.
Using a snippet may invite some smart aleck to answer. This, you want to
avoid on usenet; but in an interview situation, such an answer can provide
useful information to the interviewer.


Best

Kai-Uwe Bux
 
J

James Kanze

I did not see any include directives in the OP.
In the »real world« (when given code to review or to
refactor) I have a complete compilation unit
and can see which include directives are used.

Only directly. If you include <vector>, you don't know whether
NULL is defined or not.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top