zeroing structure of char pointers

Discussion in 'C Programming' started by Aaron Walker, Feb 29, 2004.

  1. Aaron Walker

    Aaron Walker Guest

    I was just wondering, if I have:

    struct my_struct {
    char *str1;
    char *str2;
    };

    and then elsewhere do:

    struct my_struct *ms = calloc(1, sizeof(struct my_struct));

    Would that be the same as doing:

    struct my_struct *ms = malloc(sizeof(struct my_struct));
    ms->str1 = ms->str2 = NULL;


    Thanks,
    Aaron
     
    Aaron Walker, Feb 29, 2004
    #1
    1. Advertising

  2. [FAQ] Re: zeroing structure of char pointers

    On Sun, 29 Feb 2004, Aaron Walker wrote:
    >
    > I was just wondering, if I have:
    >
    > struct my_struct {
    > char *str1;
    > char *str2;
    > };
    >
    > and then elsewhere do:
    >
    > struct my_struct *ms = calloc(1, sizeof(struct my_struct));
    >
    > Would that be the same as doing:
    >
    > struct my_struct *ms = malloc(sizeof(struct my_struct));
    > ms->str1 = ms->str2 = NULL;


    Nope. In the first case, you're allocating memory for a struct,
    and then 'calloc' is initializing all the allocated bits to zero.
    In the second case, you're allocating memory for a struct, and then
    initializing ms->str1 to NULL and ms->str2 to NULL.
    So there are two key differences:

    1. NULL != all bits zero. This is a FAQ.
    http://www.eskimo.com/~scs/C-faq/q7.31.html
    As a corollary, structs may have padding bits, which would not get
    initialized to anything in the second case (but would receive 'zero'
    bits in the first, along with every other bit). This is also a FAQ,
    but is much less relevant to your actual problem.

    2. The second snippet invokes undefined behavior if 'malloc'
    returns NULL (failure to allocate enough memory). The first snippet
    is perfectly well-defined in all cases -- but, as above, it won't do
    what you're expecting it to do.

    Read the FAQ; there's lots of important stuff there.

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Feb 29, 2004
    #2
    1. Advertising

  3. Aaron Walker

    Mac Guest

    On Sun, 29 Feb 2004 02:44:40 +0000, Aaron Walker wrote:

    > I was just wondering, if I have:
    >
    > struct my_struct {
    > char *str1;
    > char *str2;
    > };
    >
    > and then elsewhere do:
    >
    > struct my_struct *ms = calloc(1, sizeof(struct my_struct));
    >
    > Would that be the same as doing:
    >
    > struct my_struct *ms = malloc(sizeof(struct my_struct));
    > ms->str1 = ms->str2 = NULL;
    >


    Not necessarily. The portable solution is to assign NULL to all pointers.

    >
    > Thanks,
    > Aaron


    Mac
     
    Mac, Feb 29, 2004
    #3
  4. In 'comp.lang.c', "Mac" <> wrote:

    >> I was just wondering, if I have:
    >>
    >> struct my_struct {
    >> char *str1;
    >> char *str2;
    >> };
    >>
    >> and then elsewhere do:
    >>
    >> struct my_struct *ms = calloc(1, sizeof(struct my_struct));
    >>
    >> Would that be the same as doing:
    >>
    >> struct my_struct *ms = malloc(sizeof(struct my_struct));
    >> ms->str1 = ms->str2 = NULL;


    > Not necessarily. The portable solution is to assign NULL to all pointers.


    Of course, you meant 'a' portable solution. Here is an alternative :

    struct my_struct *ms = malloc (sizeof *ms);

    if (ms != NULL)
    {
    /* clear the struct */
    static const struct my_struct z = {0};
    *ms = z;

    /* the rest of the code... */
    }

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Feb 29, 2004
    #4
    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. richard

    zeroing an array

    richard, Sep 5, 2003, in forum: C Programming
    Replies:
    20
    Views:
    862
    Tom Zych
    Sep 7, 2003
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    774
    Martin Ambuhl
    Oct 26, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,099
    Ian Collins
    May 9, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    695
  5. rsine

    zeroing in on a listbox item

    rsine, Sep 2, 2003, in forum: ASP General
    Replies:
    2
    Views:
    109
    Bob Barrows
    Sep 2, 2003
Loading...

Share This Page