🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.misc
Article #2531

Re: difference between STUFF *stuff & STUFF stuff[]???

#2531
From: Tom W. Brown
Date: Wed, 06 Jan 1993 22:42
56 lines
3055 bytes
In article <1993Jan6.084522.25921@emr1.emr.ca>, jagrant@emr1.emr.ca (John Grant) writes:
|> So, can someone tell me the difference between the following 2
|> statements:
|> 	extern STUFF *stufftable;
|> 	extern STUFF stufftable[];
|> with respect to the code generated when I reference stufftable, i.e.:
|> 	stufftable[i].xxx=...
|> and explain why it is so dangerous here, but it's perfectly ok
|> to use a pointer or a [] in a function declaration.

extern STUFF *stufftable;
   This declares 'stufftable' to be a variable of type "pointer to STUFF".
   There will be a region of the data segment large enough to hold this
   pointer reserved for the variable.  The variable stufftable is a reference
   to the location in memory that in turn contains an address in the heap
   where a STUFF object (or array of STUFF objects) has been created.

      stufftable              /-----------\
      /---------\             |           |
      |      o--|-----------> |   STUFF   |
      \---------/             |           |
                              \-----------/


extern STUFF stufftable[];
   This declares 'stufftable' to be a variable of type "array of STUFF".
   There will be a region of the data segment large enough to hold some
   number of contiguous STUFF objects.  The variable stufftable is a
   reference to the first of these contiguous STUFF objects.

      stufftable
     /-----------\/----------\/-----------\/----/
     |           ||          ||           ||    \
     |   STUFF   ||   STUFF  ||   STUFF   ||    / . . .
     |           ||          ||           ||    \
     \-----------/\----------/\-----------/\----/


So, if stufftable is declared in the second way but defined in the first
the code is going to think the actual STUFF objects lie in the data segment
where the pointer is rather than somewhere off in the heap.

For function declarations and calls you don't have this problem; a pointer
argument supplies the value in the pointer (the address of the heap based
array), an array argument is in a value context and is turned into the 
address of its first element and therefore both look the same to the 
function.

Hope this helps (or, more to the point, that I haven't misspoken myself :-)


----------------------------------------------------------------------------
Tom Brown               |  "She turned me into a newt...
PE Nelson Systems       |                                  ... I got better"
twbrown@pe-nelson.com   |                    Monty Python and the Holy Grail
----------------------------------------------------------------------------
Technical Details
Message-ID: <728@lax.lax.pe-nelson.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!lax.pe-nelson.com!lax!twbrown
References: <1993Jan6.084522.25921@emr1.emr.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!lax.pe-nelson.com!lax!twbrown
From: twbrown@PE-Nelson.COM (Tom W. Brown)
Newsgroups: comp.os.ms-windows.programmer.misc
Subject: Re: difference between STUFF *stuff & STUFF stuff[]???
Message-ID: <728@lax.lax.pe-nelson.com>
Date: 6 Jan 93 22:42:34 GMT
References: <1993Jan6.084522.25921@emr1.emr.ca>
Sender: news@lax.pe-nelson.com
Organization: PE-Nelson
Lines: 56