![]() |
|
|
|
#1 |
|
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. crystal twix |
|
|
|
|
#2 |
|
Posts: n/a
|
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; > > 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. red floyd |
|
|
|
#3 |
|
Posts: n/a
|
-----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+4kgCgkS2jciV057 fHKMZoMzVT6iNK yjgAnRRhKLN/i+EOEZOX87I6V947BiR9 =V0pC -----END PGP SIGNATURE----- Michael Tsang |
|
|
|
#4 |
|
Posts: n/a
|
> 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'. siddhant3s |
|
|
|
#5 |
|
Posts: n/a
|
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'. This is not always true. It depends of the implementation of your C++. paperab |
|
|
|
#6 |
|
Posts: n/a
|
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 Ian Collins |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
On Nov 3, 3:50 am, Ian Collins <ian-n...@hotmail.com> wrote:
> 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. 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 James Kanze |
|
|
|
#9 |
|
Posts: n/a
|
On Nov 3, 5: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'. > > This is not always true. It depends of the implementation of your C++. Read http://www.research.att.com/~bs/bs_faq2.html#null siddhant3s |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Warning - pump and dumpers sending out trojan pointers | Tester | Computer Support | 2 | 08-19-2007 07:32 PM |
| Mouse pointers? | Gorbag | Computer Support | 1 | 06-29-2005 08:26 PM |
| Newbie looking for pointers | Tom | Wireless Networking | 2 | 10-17-2004 02:43 AM |
| Toolbar 'pointers' are 5's and 6's and 8's??? | fredcromer | Computer Support | 1 | 04-21-2004 08:48 PM |
| Pointers also changing into NUMBERS !!!?? | fredcromer | Computer Information | 1 | 04-21-2004 08:27 PM |