home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / mswindo / programm / misc / 4661 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.9 KB  |  68 lines

  1. Path: sparky!uunet!lax.pe-nelson.com!lax!twbrown
  2. From: twbrown@PE-Nelson.COM (Tom W. Brown)
  3. Newsgroups: comp.os.ms-windows.programmer.misc
  4. Subject: Re: difference between STUFF *stuff & STUFF stuff[]???
  5. Message-ID: <728@lax.lax.pe-nelson.com>
  6. Date: 6 Jan 93 22:42:34 GMT
  7. References: <1993Jan6.084522.25921@emr1.emr.ca>
  8. Sender: news@lax.pe-nelson.com
  9. Organization: PE-Nelson
  10. Lines: 56
  11.  
  12. In article <1993Jan6.084522.25921@emr1.emr.ca>, jagrant@emr1.emr.ca (John Grant) writes:
  13. |> So, can someone tell me the difference between the following 2
  14. |> statements:
  15. |>     extern STUFF *stufftable;
  16. |>     extern STUFF stufftable[];
  17. |> with respect to the code generated when I reference stufftable, i.e.:
  18. |>     stufftable[i].xxx=...
  19. |> and explain why it is so dangerous here, but it's perfectly ok
  20. |> to use a pointer or a [] in a function declaration.
  21.  
  22. extern STUFF *stufftable;
  23.    This declares 'stufftable' to be a variable of type "pointer to STUFF".
  24.    There will be a region of the data segment large enough to hold this
  25.    pointer reserved for the variable.  The variable stufftable is a reference
  26.    to the location in memory that in turn contains an address in the heap
  27.    where a STUFF object (or array of STUFF objects) has been created.
  28.  
  29.       stufftable              /-----------\
  30.       /---------\             |           |
  31.       |      o--|-----------> |   STUFF   |
  32.       \---------/             |           |
  33.                               \-----------/
  34.  
  35.  
  36. extern STUFF stufftable[];
  37.    This declares 'stufftable' to be a variable of type "array of STUFF".
  38.    There will be a region of the data segment large enough to hold some
  39.    number of contiguous STUFF objects.  The variable stufftable is a
  40.    reference to the first of these contiguous STUFF objects.
  41.  
  42.       stufftable
  43.      /-----------\/----------\/-----------\/----/
  44.      |           ||          ||           ||    \
  45.      |   STUFF   ||   STUFF  ||   STUFF   ||    / . . .
  46.      |           ||          ||           ||    \
  47.      \-----------/\----------/\-----------/\----/
  48.  
  49.  
  50. So, if stufftable is declared in the second way but defined in the first
  51. the code is going to think the actual STUFF objects lie in the data segment
  52. where the pointer is rather than somewhere off in the heap.
  53.  
  54. For function declarations and calls you don't have this problem; a pointer
  55. argument supplies the value in the pointer (the address of the heap based
  56. array), an array argument is in a value context and is turned into the 
  57. address of its first element and therefore both look the same to the 
  58. function.
  59.  
  60. Hope this helps (or, more to the point, that I haven't misspoken myself :-)
  61.  
  62.  
  63. ----------------------------------------------------------------------------
  64. Tom Brown               |  "She turned me into a newt...
  65. PE Nelson Systems       |                                  ... I got better"
  66. twbrown@pe-nelson.com   |                    Monty Python and the Holy Grail
  67. ----------------------------------------------------------------------------
  68.