Initialization of Structs - Typedefs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mahendra

    Initialization of Structs - Typedefs

    I have the following structs -

    15 typedef struct pSimple {
    16 char *key;
    17 char *val;
    18 } pSimple, *pSimple_ptr;
    19
    20 typedef struct pComplex {
    21 char *name;
    22 int value;
    23 } pComplex, *pComplex_ptr;
    24
    25 typedef struct pNode {
    26 char *nodeId;
    27 struct pSimple *semStats;
    28 struct pComplex *sysStats;
    29 } pNode, *pNode_ptr;

    Now somewhere in my main() i do -

    pNode data;

    and then call generate_data(& data); where i want to initialize the
    "data". Now the question i have is -

    1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
    in stack ? If not what exactly does it does ? Can i just use &data to
    initialize the struct pNode or do i have to allocate heap memory for it ?

    Thanks
    Mahendra
  • Eric Sosman

    #2
    Re: Initialization of Structs - Typedefs

    Barry Schwarz wrote:
    On Mon, 03 Nov 2008 11:54:16 -0500, Eric Sosman <Eric.Sosman@su n.com>
    wrote:
    >
    >Mahendra wrote:
    >>I have the following structs -
    >>[...]
    >> 25 typedef struct pNode {
    >> 26 char *nodeId;
    >> 27 struct pSimple *semStats;
    >> 28 struct pComplex *sysStats;
    >> 29 } pNode, *pNode_ptr;
    >>>
    >>Now somewhere in my main() i do -
    >>>
    >>pNode data;
    >>>
    >>and then call generate_data(& data); where i want to initialize the
    >>"data". Now the question i have is -
    >>>
    >>1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
    >>in stack ? If not what exactly does it does ? Can i just use &data to
    >>initialize the struct pNode or do i have to allocate heap memory for it ?
    > Yes, probably, doesn't matter, yes, no.
    >>
    > Yes, the declaration allocates storage for the thing declared,
    >
    definition and defined instead of declaration and declared
    Given the absence of the `extern' keyword, I stand by
    my nomenclature.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Barry Schwarz

      #3
      Re: Initialization of Structs - Typedefs

      On Mon, 03 Nov 2008 20:41:58 -0500, Eric Sosman
      <esosman@ieee-dot-org.invalidwrot e:
      >Barry Schwarz wrote:
      >On Mon, 03 Nov 2008 11:54:16 -0500, Eric Sosman <Eric.Sosman@su n.com>
      >wrote:
      >>
      >>Mahendra wrote:
      >>>I have the following structs -
      >>>[...]
      >>> 25 typedef struct pNode {
      >>> 26 char *nodeId;
      >>> 27 struct pSimple *semStats;
      >>> 28 struct pComplex *sysStats;
      >>> 29 } pNode, *pNode_ptr;
      >>>>
      >>>Now somewhere in my main() i do -
      >>>>
      >>>pNode data;
      >>>>
      >>>and then call generate_data(& data); where i want to initialize the
      >>>"data". Now the question i have is -
      >>>>
      >>>1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
      >>>in stack ? If not what exactly does it does ? Can i just use &data to
      >>>initialize the struct pNode or do i have to allocate heap memory for it ?
      >> Yes, probably, doesn't matter, yes, no.
      >>>
      >> Yes, the declaration allocates storage for the thing declared,
      >>
      >definition and defined instead of declaration and declared
      >
      Given the absence of the `extern' keyword, I stand by
      >my nomenclature.
      But if extern had been part of the code, it would definitely be a
      declaration and not a definition. And of course it would not have
      allocated storage. That it did allocate storage is one of the clear
      distinctions between declarations and definitions.

      --
      Remove del for email

      Comment

      Working...