home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!news.encore.com!csar!foxtail!sdd.hp.com!decwrl!decwrl!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c++
- Subject: Re: Explanation for "Null pointer assignment"
- Message-ID: <541@ulogic.UUCP>
- Date: 4 Nov 92 19:46:39 GMT
- References: <Bww82p.HIw@peora.sdc.ccur.com>
- Organization: negligable
- Lines: 54
-
- In article <Bww82p.HIw@peora.sdc.ccur.com> tran@peora.sdc.ccur.com (Nhan Tran) writes:
- >
- >I am using Borland C++ 2.0. When my program finish, it has a run-time error
- >message "Null pointer assignment". According to the manual,
- >
- > "Null pointer assignment" is displayed to inform you that
- > (most likely) a value was stored to an uninitialized pointer.
- > The program may appear to work properly in all other respects;
- > however, this is a serious bug ...
- >
- >I am puzzled by this. If I declare a pointer without initialzing it, then
- >the program will assign some value to that pointer (by using it) at run-time.
-
- I read the other answers you got, and noone actually seemed to directly
- address your question about "the program will assign...", so here goes.
-
- A: Not necessarily. If I declare:
-
- char *p;
-
- all I have is a pointer to somewhere. Perhaps initialized to NULL,
- perhaps not. If you are counting on "the program" to assign a value
- to that "by using it" you are either under a misapprehension, or I
- misunderstand your statement. You MUST at some point in time point
- that pointer to some specific place:
-
- p = malloc(81); /* dynamically allocated memory */
- p = &some_other_var; /* address of some other var */
- p = some_other_ptr; /* a copy of another pointer */
- p = (char *) 0xA000; /* hardware dependant address */
-
- >What is wrong with that? Or the above explanation implies something else?
-
- If you do NOT assign some sort of legitimate value to the pointer
- you have declared, and then attepmt to use it:
-
- *p = 'X';
-
- you could be writing ANYWHERE. This gets you a "memory protection fault"
- on machines that support such things directly. A "NULL pointer assignment"
- assignment on machines that a) init pointers to NULL, and b) check for
- such things (as described in a parallel answer). On machines that don't
- do either of these your 'X' could wind up almost anywhere in memory
- with effects that may not be discovered until the most inconvenient time...
-
-
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Lovely to see you again my friend, |
- walk along with me to the next bend. | -Richard Hartman
- Tell us what you've seen | hartman@uLogic.COM
- in far away forgotten lands, |
- Where empires have turned back to sand. |
- -Justin Hayward |
-