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.
Dereferencing a NULL pointer.
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.
}
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-----
I am *not* saying anything against but, I write int* p=0; as it remind
me that in C++ 'NULL' is 'zer0'.
This is not always true. It depends of the implementation of your C++.
If you get into the habit of declaring and initialising pointers at the
point of first use, the discussion is moot.
--
Ian Collins
> > > 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
> > 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