Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointers - *p++

2 views
Skip to first unread message

crystal twix

unread,
Nov 1, 2009, 5:24:48 PM11/1/09
to
Hi. I'm trying to understand what my professor says when he refers to
declaring a pointer, and seeing the operator precedence and seeing
what happens when we do things like

p++
*p++
(*p)++ etc,

but he starts out with this declaration
int *p = (int *)0;

Although my compiler does not complain, when I try to do something
like
cout << *p;

I get a Bus Error on the console (I'm using Xcode). What am I doing
wrong here? Thanks.

red floyd

unread,
Nov 1, 2009, 6:27:00 PM11/1/09
to

Dereferencing a NULL pointer.

Daniel T.

unread,
Nov 1, 2009, 7:19:59 PM11/1/09
to
crystal twix <jonwong...@gmail.com> wrote:

A pointer that contains the value '0' is a special case. It basically
means that it points to nothing and it is an error to dereference it.
This is not an error that can be caught at compile time in the general
case, so your program is notifying you of the error during runtime.

int main() {
int* nothing = 0;
int* something = new int(10);

cout << *something << '\n'; // prints "10".
cout << *nothing << '\n'; // undefined behavior, if you are lucky
// your program will crash.
}

Michael Tsang

unread,
Nov 2, 2009, 9:40:45 AM11/2/09
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

crystal twix wrote:

> Hi. I'm trying to understand what my professor says when he refers to
> declaring a pointer, and seeing the operator precedence and seeing
> what happens when we do things like
>
> p++
> *p++
> (*p)++ etc,
>
> but he starts out with this declaration
> int *p = (int *)0;

Better to write "int *p=NULL;" instead


>
> Although my compiler does not complain, when I try to do something
> like
> cout << *p;

p is a pointer object. As the value of p is NULL, dereferencing it becomes
an object that does not exist. The output operation tries to take the value
in the object, however, as the object does not exist, the behaviour is
undefined.


>
> I get a Bus Error on the console (I'm using Xcode). What am I doing
> wrong here? Thanks.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkru724ACgkQG6NzcAXitM+4kgCgkS2jciV057fHKMZoMzVT6iNK
yjgAnRRhKLN/i+EOEZOX87I6V947BiR9
=V0pC
-----END PGP SIGNATURE-----

siddhant3s

unread,
Nov 2, 2009, 1:44:50 PM11/2/09
to

> Better to write "int *p=NULL;" instead

I am *not* saying anything against but, I write int* p=0; as it remind
me that in C++ 'NULL' is 'zer0'.

paperab

unread,
Nov 2, 2009, 7:38:20 PM11/2/09
to

This is not always true. It depends of the implementation of your C++.

Ian Collins

unread,
Nov 2, 2009, 10:50:43 PM11/2/09
to
crystal twix wrote:
> Hi. I'm trying to understand what my professor says when he refers to
> declaring a pointer, and seeing the operator precedence and seeing
> what happens when we do things like
>
> p++
> *p++
> (*p)++ etc,
>
> but he starts out with this declaration
> int *p = (int *)0;

If you get into the habit of declaring and initialising pointers at the
point of first use, the discussion is moot.

--
Ian Collins

James Kanze

unread,
Nov 3, 2009, 5:57:40 AM11/3/09
to
On Nov 3, 12:38 am, paperab <ab9380...@yahoo.co.uk> wrote:
> On Nov 2, 6:44 pm, siddhant3s <siddhan...@gmail.com> wrote:

> > > Better to write "int *p=NULL;" instead

> > I am *not* saying anything against but, I write int* p=0; as
> > it remind me that in C++ 'NULL' is 'zer0'.

Yes. It's a style issue. I too prefer NULL, but I understand
people who prefer 0. Either is "idiomatic". The original:
int *p = (int*)0;
isn't.

> This is not always true. It depends of the implementation of
> your C++.

It's not necessarily true that NULL expands to the exact string
0, but it must expand to a null pointer constant, i.e. an
integral constant expression evaluating to 0. (It's interesting
that a null pointer constant is not allowed to be a pointer.) 0
is by far the most frequent solution, and 0L was also prevelant
at one time. At least one compiler uses something like
__builtin_null, so that it can warn if NULL is used as an
integer; this is by far the best solution.

--
James Kanze

James Kanze

unread,
Nov 3, 2009, 5:58:58 AM11/3/09
to

> > p++
> > *p++
> > (*p)++ etc,

He did. He initialized it with a null pointer, then proceeded
with a number of operations which aren't legal on null pointers.

--
James Kanze

siddhant3s

unread,
Nov 3, 2009, 1:35:18 PM11/3/09
to
0 new messages