Elegant way to clear a struct ?

Discussion in 'C Programming' started by Nils Emil P. Larsen, Jun 29, 2003.

  1. Hello

    I have a pointer to a struct or an array of structs and I know the
    lenght of the struct or array of structs.

    I need a elegant way to clear it with zeroes.

    Is it legal just to do something like

    Mystruct *mystruct; // Mystruct is a typedef

    for (i=0; i < sizeof(Mystruct); i++)
    *(mystruct+i) = 0;

    or do I have to clear each individual members ?

    Thank you
    Nils Emil P. Larsen
    Nils Emil P. Larsen, Jun 29, 2003
    1. Advertisements

  2. If the struct contains nothing but integer (and character) elements,
    you can just do
    memset(mystruct, 0, array_length * sizeof(Mystruct))
    Otherwise, use
    const Mystruct null_mystruct; /* will be initialized with nulls */
    for (i = 0; i < array_length; i++)
    mystruct = null_mystruct;

    BTW, note that ANSI C (or ISO C89) do not accept '//' comments. Only
    C++ and C99 do.
    Hallvard B Furuseth, Jun 29, 2003
    1. Advertisements

  3. That looks rather confused to me. Very confused, in fact.

    Given that mystruct points to an array of N Mystruct objects, you can do

    Mystruct blank = {0};

    for(i = 0; i < N; i++)
    mystruct = blank;
    Richard Heathfield, Jun 29, 2003
  4. Sorry, that should be 'static const null_mystruct', unless
    null_mystruct has file scope.
    Hallvard B Furuseth, Jun 29, 2003
  5. Nils Emil P. Larsen

    Kevin Easton Guest

    What basis do you have for that comment? Did you perhaps mean either:

    const Mystruct null_mystruct = { 0 };


    static const Mystruct null_mystruct;


    - Kevin.
    Kevin Easton, Jun 29, 2003
  6. Nils Emil P. Larsen

    Kevin Easton Guest

    What use is an int variable called "null_mystruct" in this context? :)

    - Kevin.
    Kevin Easton, Jun 29, 2003
  7. 'length'. In fact, you mean 'size'.
    Mystruct *mystruct = malloc (sizeof *mystruct);

    if (mystruct)
    static const Mystruct z = {0};

    *mystruct = z;
    mystruct = 0;

    Assuming 'mystruct' was initialized with an the address of an (allocated)
    bloc of data, none of the two forms is legal.
    Emmanuel Delahaye, Jun 29, 2003
  8. This won't do it, since *(mystruct+i) is mystruct.
    Depending on the types of members a Mystruct has, memset() might do it
    for you.
    Martin Ambuhl, Jun 29, 2003
  9. Nils Emil P. Larsen

    Simon Biber Guest

    When you add i to mystruct you are constructing another
    pointer to Mystruct, which points to the i-th successive
    Mystruct after the one that mystruct actually points to.

    See how confusing it is to have variables and types with
    names differentiated only by case? That's not a good idea.

    So, what does sizeof(Mystruct) have to do with successive
    Mystructs after the one pointed to by mystruct? If you
    wanted to address each *byte* of the structure, you have
    to cast the pointer to (unsigned char *). You would be
    in essence just recreating what is done for you by memset.
    But that is not a portable way to zero out most types.

    This looks to me like a place where C99's compound literals
    would be useful. Example code follows.

    #include <stdlib.h>

    typedef struct {int a; char *p;} Mystruct;

    int main(void)
    Mystruct *foo = malloc(sizeof *foo);
    *foo = (Mystruct){0};

    Mystruct bar[42];
    for(size_t i = 0; i < sizeof bar / sizeof *bar; i++)
    bar = (Mystruct){0};

    return 0;

    This code was tested on GCC 3.2 with command line
    gcc -std=c99 -pedantic -Wall -W -O2 zerostruct.c -o zerostruct
    Simon Biber, Jun 30, 2003
  10. Either drop sizeof char altogether, or replace it with sizeof *st.temp, or
    at least put () around char.
    It will leak the memory you assigned. Solution: free the memory first.

    Richard Heathfield, Jun 30, 2003
  11. That statement should either be at file scope, or it should be
    static const Mystruct null_mystruct;

    This time I got it right, I think:)
    That's fine.
    Oh my gosh. You could, but don't. BTW, it should have been
    *( const Mystruct *) &null_struct;
    since you should not cast away const when you don't need to.
    Hallvard B Furuseth, Jul 1, 2003
  12. Nils Emil P. Larsen

    Kevin Easton Guest

    It was neither static nor file scope (because the next line was a
    statement, which can't appear at file scope).

    - Kevin.
    Kevin Easton, Jul 7, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.