home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15813 < prev    next >
Encoding:
Text File  |  1992-11-04  |  2.6 KB  |  65 lines

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