fundamental memory clearing question

Discussion in 'C++' started by Stefan =?UTF-8?B?TsOkd2U=?=, Nov 2, 2005.

  1. Jason wrote:

    >
    > Hi.
    >
    > I have a structure that was used in a previous iteration of a loop, and I
    > want to clear out all the structures values that the previous iteration
    > assigned without assigning every variable to be 0.
    >
    > I've thought of a few ways to do this, but they are all roundabout...
    > Does anyone know of a better way to do this?
    >
    > Heres a sample idea that I came up with.
    >
    >
    > void clear_the_structure() // no parameters because structure is extern
    > {
    > for (i=0; i < sizeof(mystructure); i++) // for each byte in
    > structure's memory
    > {
    > ((unsigned char)mystructure) = 0; // set this byte to null
    > }
    > }


    Please, don't tell us more about 'mystructure'...
    Is it a POD ?


    Stefan
    --
    Stefan =?UTF-8?B?TsOkd2U=?=, Nov 2, 2005
    #1
    1. Advertising

  2. Stefan =?UTF-8?B?TsOkd2U=?=

    Jason Guest

    Hi.

    I have a structure that was used in a previous iteration of a loop, and I
    want to clear out all the structures values that the previous iteration
    assigned without assigning every variable to be 0.

    I've thought of a few ways to do this, but they are all roundabout...
    Does anyone know of a better way to do this?

    Heres a sample idea that I came up with.


    void clear_the_structure() // no parameters because structure is extern
    {
    for (i=0; i < sizeof(mystructure); i++) // for each byte in
    structure's memory
    {
    ((unsigned char)mystructure) = 0; // set this byte to null
    }
    }
    Jason, Nov 2, 2005
    #2
    1. Advertising

  3. * Jason:
    >
    >
    > I have a structure that was used in a previous iteration of a loop, and I
    > want to clear out all the structures values that the previous iteration
    > assigned without assigning every variable to be 0.


    Make the structure local to the loop, and zero-initialize it.

    while( stillLooping )
    {
    Structure s = Structure();
    ...
    }

    which can be shorter if Structure is a POD.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 2, 2005
    #3
  4. Stefan =?UTF-8?B?TsOkd2U=?=

    lpw Guest

    "Jason" <> wrote in message
    news:...
    >
    > Hi.
    >
    > I have a structure that was used in a previous iteration of a loop, and I
    > want to clear out all the structures values that the previous iteration
    > assigned without assigning every variable to be 0.
    >
    > I've thought of a few ways to do this, but they are all roundabout...
    > Does anyone know of a better way to do this?
    >
    > Heres a sample idea that I came up with.
    >
    >
    > void clear_the_structure() // no parameters because structure is extern
    > {
    > for (i=0; i < sizeof(mystructure); i++) // for each byte in
    > structure's memory
    > {
    > ((unsigned char)mystructure) = 0; // set this byte to null
    > }
    > }
    >
    >


    memset(&mystructure, 42, sizeof(mystructure));

    You did say you don't want to set every variable to zero...
    lpw, Nov 2, 2005
    #4
  5. Stefan =?UTF-8?B?TsOkd2U=?=

    Jason Guest

    My main concern was that the structure is going to change in about 20
    minutes when I start coding it again, and I didn't want to have to go in and
    modify this line of code to accomidate every member variable every time I
    add one.

    The code that you gave me, lpw, will work, I think. memset is exactly what
    I was looking for.

    out of curiosity, why did you choose 42 in your sample code? was it an
    arbitrary number? or does it have some intrinsic value?


    me = c++ newbie




    "lpw" <> wrote in message
    news:...
    >
    > "Jason" <> wrote in message
    > news:...
    >>
    >> Hi.
    >>
    >> I have a structure that was used in a previous iteration of a loop, and I
    >> want to clear out all the structures values that the previous iteration
    >> assigned without assigning every variable to be 0.
    >>
    >> I've thought of a few ways to do this, but they are all roundabout...
    >> Does anyone know of a better way to do this?
    >>
    >> Heres a sample idea that I came up with.
    >>
    >>
    >> void clear_the_structure() // no parameters because structure is
    >> extern
    >> {
    >> for (i=0; i < sizeof(mystructure); i++) // for each byte in
    >> structure's memory
    >> {
    >> ((unsigned char)mystructure) = 0; // set this byte to null
    >> }
    >> }
    >>
    >>

    >
    > memset(&mystructure, 42, sizeof(mystructure));
    >
    > You did say you don't want to set every variable to zero...
    >
    >
    Jason, Nov 2, 2005
    #5
  6. * Jason:
    > [top-posting]


    Please don't top-post in this group, read the FAQ.


    > My main concern was that the structure is going to change in about 20
    > minutes when I start coding it again, and I didn't want to have to go in and
    > modify this line of code to accomidate every member variable every time I
    > add one.
    >
    > The code that you gave me, lpw, will work, I think. memset is exactly what
    > I was looking for.


    Don't use memset.

    It's dangerous and inefficient.

    Let C++ do the work for you, e.g. see my previous reply.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 2, 2005
    #6
  7. Jason wrote:
    > out of curiosity, why did you choose 42 in your sample code? was it an
    > arbitrary number? or does it have some intrinsic value?


    He chose 42 because you said you did not want it set to 0, that, and it
    is THE answer.
    Josh Mcfarlane, Nov 2, 2005
    #7
  8. Stefan =?UTF-8?B?TsOkd2U=?=

    lpw Guest

    > Don't use memset.
    >
    > It's dangerous and inefficient.


    Busted.

    *bows head in shame*
    lpw, Nov 2, 2005
    #8
  9. Stefan =?UTF-8?B?TsOkd2U=?=

    Guest

    is it the efficient way to do it instead of memset? The local obj will
    be constructed/destructed again and again?
    , Nov 2, 2005
    #9
  10. Stefan =?UTF-8?B?TsOkd2U=?=

    Guest

    Could you pl explain why memset is "dangerous and inefficient" for
    struct of data?
    , Nov 2, 2005
    #10
  11. Stefan =?UTF-8?B?TsOkd2U=?=

    lpw Guest

    <> wrote in message
    news:...
    > Could you pl explain why memset is "dangerous and inefficient" for
    > struct of data?


    "Dangerous" because it might overwrite the vtable (if a vtable exists).
    "Inefficient", well, I think that's highly implementation-dependent and,
    therefore, arguable.
    lpw, Nov 2, 2005
    #11
  12. Stefan =?UTF-8?B?TsOkd2U=?=

    Guest

    I am thinking that I was talking about POD, and I realized that pointer
    type may be the problem if the mem allocated need to be released before
    reset/re-allocate.
    , Nov 2, 2005
    #12
  13. * :
    > is it the efficient way to do it instead of memset? The local obj will
    > be constructed/destructed again and again?


    You have two cases.

    One, it's a simple structure, a POD, in which case "constructed" amounts
    to clearing it in the most efficient way the compiler knows, and
    "destructed" amounts to nothing at all.

    Two, it's not a simple structure, in which case memset is incorrect,
    Undefined Behavior, and may cause Serious Havoc.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 2, 2005
    #13
  14. "Alf P. Steinbach" <> wrote in message
    news:...
    :* Jason:
    : >
    : >
    : > I have a structure that was used in a previous iteration of a loop,
    and I
    : > want to clear out all the structures values that the previous
    iteration
    : > assigned without assigning every variable to be 0.
    :
    : Make the structure local to the loop, and zero-initialize it.
    :
    : while( stillLooping )
    : {
    : Structure s = Structure();

    How about the alternative:
    Structure s = {0};

    To me this C-inherited notation for zero-initialization expresses
    clearly that Structure is a simple aggregate of values that will
    be initialized to zero. So I tend to prefer it.

    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Nov 3, 2005
    #14
  15. * Ivan Vecerina:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    > :* Jason:
    > : >
    > : >
    > : > I have a structure that was used in a previous iteration of a loop,
    > and I
    > : > want to clear out all the structures values that the previous
    > iteration
    > : > assigned without assigning every variable to be 0.
    > :
    > : Make the structure local to the loop, and zero-initialize it.
    > :
    > : while( stillLooping )
    > : {
    > : Structure s = Structure();
    >
    > How about the alternative:
    > Structure s = {0};
    >
    > To me this C-inherited notation for zero-initialization expresses
    > clearly that Structure is a simple aggregate of values that will
    > be initialized to zero. So I tend to prefer it.


    Yes, that's what I wrote. ;-) "can be shorter if Structure is a POD."

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 3, 2005
    #15
    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. Praveen
    Replies:
    3
    Views:
    553
    Praveen
    Apr 24, 2005
  2. Gary

    FUNDAMENTAL QUESTION 1:

    Gary, Nov 29, 2003, in forum: Java
    Replies:
    3
    Views:
    472
    pete kirkham
    Nov 29, 2003
  3. Gary

    FUNDAMENTAL QUESTION 2:

    Gary, Nov 29, 2003, in forum: Java
    Replies:
    3
    Views:
    411
    Bjorn Abelli
    Nov 29, 2003
  4. Casper Bang
    Replies:
    51
    Views:
    1,189
  5. Replies:
    8
    Views:
    2,455
    Maneesh
    Jul 10, 2006
Loading...

Share This Page