pointers to structures

Discussion in 'C Programming' started by Sam, Feb 2, 2005.

  1. Sam

    Sam Guest

    Hi everyone

    I'm confused by these declarations:

    struct hostent *he;
    server *s = (server*)malloc(sizeof(server));

    /* in the code *
    he = gethostbyname(name);

    (server is defined in the include as

    typedef struct {
    int fdin, fdout;
    FILE *fin, *fout;
    } server;

    )

    Am I right in thinking that both *he and *s are pointers to structures? In
    that case, how come memory is only allocated for *s and not *he? When the
    members of *he are accessed later on in the code, where does the memory
    come from? What rules govern when you need to malloc and when you don't?

    Many thanks for advice
    Sam
     
    Sam, Feb 2, 2005
    #1
    1. Advertising

  2. Sam wrote:
    > Hi everyone
    >
    > I'm confused by these declarations:
    >
    > struct hostent *he;
    > server *s = (server*)malloc(sizeof(server));

    Don't cast the return of malloc.

    > /* in the code *
    > he = gethostbyname(name);
    >
    > (server is defined in the include as
    >
    > typedef struct {
    > int fdin, fdout;
    > FILE *fin, *fout;
    > } server;
    >
    > )
    >
    > Am I right in thinking that both *he and *s are pointers to structures? In

    Well he and s are the pointers, not *he/*s.

    > that case, how come memory is only allocated for *s and not *he? When the
    > members of *he are accessed later on in the code, where does the memory
    > come from?

    "somewhere". In this case gethostbyname returns a pointer to a static
    buffer.

    (Thus the next time you call gethostbyname the same buffer gets reused,
    so your original pointer doesn't point to the data you originally had..)
    >What rules govern when you need to malloc and when you don't?

    Documenation of the functions you use. If it's your own code,
    (no 3.party functions/apis involved), it's ofcourse up to you how to
    allocate required storage.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Feb 2, 2005
    #2
    1. Advertising

  3. In article <>,
    Sam <> wrote:
    > struct hostent *he;
    > server *s = (server*)malloc(sizeof(server));
    > he = gethostbyname(name);


    >Am I right in thinking that both *he and *s are pointers to structures?


    Yes.

    >In
    >that case, how come memory is only allocated for *s and not *he?


    Either gethostbyname() allocates the memory (by calling malloc()), or
    the pointer it returns is to a static structure.

    -- Richard
     
    Richard Tobin, Feb 2, 2005
    #3
  4. Sam

    Sam Guest

    On Wed, 02 Feb 2005 16:09:45 +0100, Nils O. SelÄsdal wrote:

    > Sam wrote:
    >> Hi everyone
    >>
    >> I'm confused by these declarations:
    >>
    >> struct hostent *he;
    >> server *s = (server*)malloc(sizeof(server));

    > Don't cast the return of malloc.


    OK

    >> /* in the code *
    >> he = gethostbyname(name);
    >>
    >> (server is defined in the include as
    >>
    >> typedef struct {
    >> int fdin, fdout;
    >> FILE *fin, *fout;
    >> } server;
    >>
    >> )
    >>
    >> Am I right in thinking that both *he and *s are pointers to structures? In

    > Well he and s are the pointers, not *he/*s.


    OK

    >> that case, how come memory is only allocated for *s and not *he? When the
    >> members of *he are accessed later on in the code, where does the memory
    >> come from?

    > "somewhere". In this case gethostbyname returns a pointer to a static
    > buffer.
    >
    > (Thus the next time you call gethostbyname the same buffer gets reused,
    > so your original pointer doesn't point to the data you originally had..)


    Ah, I see!

    One further question, for small structures, is it better to do this:

    server *s = malloc(sizeof(server));
    s->member = etc.

    or this:

    server s;
    s.member = etc.

    ?

    >>What rules govern when you need to malloc and when you don't?

    > Documenation of the functions you use. If it's your own code, (no
    > 3.party functions/apis involved), it's ofcourse up to you how to
    > allocate required storage.
     
    Sam, Feb 2, 2005
    #4
  5. Sam

    Richard Bos Guest

    Sam <> wrote:

    > One further question, for small structures, is it better to do this:
    >
    > server *s = malloc(sizeof(server));
    > s->member = etc.
    >
    > or this:
    >
    > server s;
    > s.member = etc.


    The size of the struct hardly matters, unless your structs types are
    many kilobytes large - rare, and rarely a good idea.
    For normal structs, small, middling and large, the former has the
    disadvantage that you must remember to free() the memory, but the
    advantage that it's more flexible; you can, for example pass s back to
    the function that called this one. This is often used in constructor
    functions. Of course, that calling function does then have the duty to
    call free(). You can also use malloc() to allocate memory for more than
    one struct at a time, for example, without having to know at compile
    time how many structs you want. You can't do that with the second kind
    of code, unless you have a C99 compiler and can use variable-length
    arrays.
    The latter has the advantage that it's simple, and the disadvantage that
    it's less flexible. You can't do all these complicated things, but if
    you only need the one struct, it's definitely less hassle. The only
    other disadvantage is that implementations are only required to support
    automatic objects below a certain size, but that doesn't apply to small
    or large structs, only to gargantuan ones.

    Richard
     
    Richard Bos, Feb 2, 2005
    #5
  6. Sam

    CBFalconer Guest

    Sam wrote:
    >
    > I'm confused by these declarations:
    >
    > struct hostent *he;
    > server *s = (server*)malloc(sizeof(server));
    >
    > /* in the code *
    > he = gethostbyname(name);
    >
    > (server is defined in the include as
    >
    > typedef struct {
    > int fdin, fdout;
    > FILE *fin, *fout;
    > } server;


    So are we (confused). struct hostent is never defined, neither is
    gethostbyname. Potential errors are masked by the foolish cast of
    malloc. This smells of off-topic non-standard lack of portability,
    and probably should be asked on a group dealing with your
    particular system and OS. Possibly comp.unix.programmer

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Feb 2, 2005
    #6
  7. In article <>,
    CBFalconer <> wrote:
    >This smells of off-topic non-standard lack of portability,
    >and probably should be asked on a group dealing with your
    >particular system and OS. Possibly comp.unix.programmer


    Oh come on. The code uses non-standard libraries, but his problem is
    obviously - as he states explicitly - that he doesn't know when he has
    to use malloc() to allocate space.

    Try to find an interpretation that makes someone's article reasonable,
    rather than one that makes it unreasonable.

    -- Richard
     
    Richard Tobin, Feb 2, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    424
  2. DSKR

    Pointers to 'Data Structures' in C

    DSKR, Jun 24, 2003, in forum: C Programming
    Replies:
    31
    Views:
    10,111
    Giuseppe
    Jul 3, 2003
  3. tweak
    Replies:
    14
    Views:
    2,798
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    733
    Emmanuel Delahaye
    Sep 24, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    696
Loading...

Share This Page