zero memory

Discussion in 'C++' started by Christopher Pisz, Apr 5, 2007.

  1. What is the C++ way to zero out memory after calling operator new on a
    struct?

    A constructor is not possible in this case nor is a class, because the
    people using my code c-stlye cast a pointer to the first member of the
    struct to a pointer to the entrire struct later on in the code.
    Christopher Pisz, Apr 5, 2007
    #1
    1. Advertising

  2. Christopher Pisz wrote:
    > What is the C++ way to zero out memory after calling operator new on a
    > struct?


    To zero out what memory? What does "calling operator new on a struct"
    mean? Could you post code instead of English?

    > A constructor is not possible in this case nor is a class, because the
    > people using my code c-stlye cast a pointer to the first member of the
    > struct to a pointer to the entrire struct later on in the code.


    Huh?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 5, 2007
    #2
    1. Advertising

  3. Christopher Pisz

    Default User Guest

    Christopher Pisz wrote:

    > What is the C++ way to zero out memory after calling operator new on
    > a struct?


    Why do you want to? What problem does this solve?

    > A constructor is not possible in this case nor is a class, because
    > the people using my code c-stlye cast a pointer to the first member
    > of the struct to a pointer to the entrire struct later on in the code.


    If it's a POD, you can use memcpy to set the whole thing to
    all-bits-zero. That's not always what you want, and frankly is usually
    a poor design.





    Brian
    Default User, Apr 5, 2007
    #3
  4. Victor Bazarov wrote:
    > Christopher Pisz wrote:
    >
    >>What is the C++ way to zero out memory after calling operator new on a
    >>struct?

    >
    >
    > To zero out what memory? What does "calling operator new on a struct"
    > mean? Could you post code instead of English?
    >
    >
    >>A constructor is not possible in this case nor is a class, because the
    >>people using my code c-stlye cast a pointer to the first member of the
    >>struct to a pointer to the entrire struct later on in the code.

    >
    >
    > Huh?


    I'm guessing but Mr Pisz could probably benefit from knowing what this does:

    struct A
    {
    int X;
    int Y;
    char Z[30];
    // other POD members ...
    };

    A x = A();
    Gianni Mariani, Apr 5, 2007
    #4
  5. "Victor Bazarov" <> wrote in message
    news:ev3rft$9f1$...
    > Christopher Pisz wrote:
    >> What is the C++ way to zero out memory after calling operator new on a
    >> struct?

    >
    > To zero out what memory? What does "calling operator new on a struct"
    > mean? Could you post code instead of English?
    >
    >> A constructor is not possible in this case nor is a class, because the
    >> people using my code c-stlye cast a pointer to the first member of the
    >> struct to a pointer to the entrire struct later on in the code.

    >
    > Huh?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask



    typedef struct _MYSTRUCT
    {
    OVERLAPPED overlapped;
    int mamajama;
    _MYSTRUCT * next;
    } *PMYSTRUCT, MYSTRUCT;

    Foo()
    {
    PMYSTRUCT = new MYSTRUCT;

    // now set all members of the struct to 0
    }


    // later, they do this:
    SomeFunction(OVERLAPPED * overlapped)
    {
    PMYSTRUCT happystruct = (PMYSTRUCT) overlapped;
    int x = happystruct->mamajama;

    //etc etc.
    }
    Christopher Pisz, Apr 6, 2007
    #5
  6. Christopher Pisz

    Ian Collins Guest

    Christopher Pisz wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ev3rft$9f1$...
    >
    >>Christopher Pisz wrote:
    >>
    >>>What is the C++ way to zero out memory after calling operator new on a
    >>>struct?

    >>
    >>To zero out what memory? What does "calling operator new on a struct"
    >>mean? Could you post code instead of English?
    >>
    >>
    >>>A constructor is not possible in this case nor is a class, because the
    >>>people using my code c-stlye cast a pointer to the first member of the
    >>>struct to a pointer to the entrire struct later on in the code.

    >>

    >
    > typedef struct _MYSTRUCT
    > {
    > OVERLAPPED overlapped;
    > int mamajama;
    > _MYSTRUCT * next;
    > } *PMYSTRUCT, MYSTRUCT;
    >
    > Foo()
    > {
    > PMYSTRUCT = new MYSTRUCT;
    >
    > // now set all members of the struct to 0
    > }
    >
    >
    > // later, they do this:
    > SomeFunction(OVERLAPPED * overlapped)
    > {
    > PMYSTRUCT happystruct = (PMYSTRUCT) overlapped;
    > int x = happystruct->mamajama;
    >
    > //etc etc.
    > }
    >

    It looks like someone is very confused between C and C++ idioms.

    --
    Ian Collins.
    Ian Collins, Apr 6, 2007
    #6
  7. Christopher Pisz

    red floyd Guest

    Christopher Pisz wrote:

    >
    >
    > typedef struct _MYSTRUCT


    Ill formed program. Any identifier with a leading underscore, followed
    by an uppercase letter is reserved to the implementation.

    > {
    > OVERLAPPED overlapped;
    > int mamajama;
    > _MYSTRUCT * next;
    > } *PMYSTRUCT, MYSTRUCT;
    >
    > Foo()
    > {
    > PMYSTRUCT = new MYSTRUCT;
    >
    > // now set all members of the struct to 0
    > }
    >
    >
    > // later, they do this:
    > SomeFunction(OVERLAPPED * overlapped)
    > {
    > PMYSTRUCT happystruct = (PMYSTRUCT) overlapped;
    > int x = happystruct->mamajama;
    >
    > //etc etc.
    > }
    >


    Also, as someone else pointed out, this is essentially C, except for the
    use of new.
    red floyd, Apr 6, 2007
    #7
  8. Christopher Pisz wrote:
    ....
    > Foo()
    > {
    > PMYSTRUCT = new MYSTRUCT;

    // doing it the hard way
    >
    > // now set all members of the struct to 0


    PMYSTRUCT = new MYSTRUCT(); // this will initialize all to 0
    ....

    > }
    >

    ....
    Gianni Mariani, Apr 6, 2007
    #8
  9. Christopher Pisz

    Jack Klein Guest

    On 5 Apr 2007 22:06:14 GMT, "Default User" <>
    wrote in comp.lang.c++:

    > Christopher Pisz wrote:
    >
    > > What is the C++ way to zero out memory after calling operator new on
    > > a struct?

    >
    > Why do you want to? What problem does this solve?
    >
    > > A constructor is not possible in this case nor is a class, because
    > > the people using my code c-stlye cast a pointer to the first member
    > > of the struct to a pointer to the entrire struct later on in the code.

    >
    > If it's a POD, you can use memcpy to set the whole thing to

    ^^^^^^

    I know you know that should be memset(). Silly typo.

    > all-bits-zero. That's not always what you want, and frankly is usually
    > a poor design.


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 6, 2007
    #9
  10. Christopher Pisz

    ajk Guest

    On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
    <> wrote:

    >What is the C++ way to zero out memory after calling operator new on a
    >struct?
    >
    >A constructor is not possible in this case nor is a class, because the
    >people using my code c-stlye cast a pointer to the first member of the
    >struct to a pointer to the entrire struct later on in the code.
    >


    so memset() should do the trick

    "the c++ way" would be to have a class and to initialize it in the
    ctor

    e.g.

    class CMYSTRUCT : public MYSTRUCT
    {
    public:
    CMYSTRUCT() {
    memset(&overlapped,0,sizeof(overlapped)); mamajama=0; next=NULL; }
    };

    Foo()
    {
    PMYSTRUCT = new CMYSTRUCT;
    }

    or make the MYSTRUCT a class
    ajk, Apr 6, 2007
    #10
  11. ajk wrote:
    > On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
    > <> wrote:
    >
    >
    >>What is the C++ way to zero out memory after calling operator new on a
    >>struct?
    >>
    >>A constructor is not possible in this case nor is a class, because the
    >>people using my code c-stlye cast a pointer to the first member of the
    >>struct to a pointer to the entrire struct later on in the code.
    >>

    >
    >
    > so memset() should do the trick
    >
    > "the c++ way" would be to have a class and to initialize it in the
    > ctor
    >
    > e.g.
    >
    > class CMYSTRUCT : public MYSTRUCT
    > {
    > public:
    > CMYSTRUCT() {
    > memset(&overlapped,0,sizeof(overlapped)); mamajama=0; next=NULL; }


    You really want to avoid using memset. It will bite you one day.

    Try this:

    CMYSTRUCT() : MYSTRUCT( MYSTRUCT() ) {}

    But your problem does not end there :-(

    > };
    >
    > Foo()
    > {
    > PMYSTRUCT = new CMYSTRUCT;


    In this case, you're calling new on CMYSTRUCT and I expect that you'll
    call delete on a MYSTRUCT. That's undefined. Bad things will happen if
    don't modify that habbit.

    > }
    >
    > or make the MYSTRUCT a class


    MYSTRUCT is a class.



    I just thought of yet another way - this one will create a default
    constructed or zero initialized POD object depending on what type of
    pointer you're trying to assign it to.

    struct InitObj
    {
    template <typename T>
    operator T * ()
    {
    return new T();
    }
    };

    // usage - template automagically figures out which type to new
    PMYSTRUCT * mys = InitObj();

    int * z = InitObj();


    Note the lack of a memset call and note that the code will work for POD
    types as well as non POD types.
    Gianni Mariani, Apr 6, 2007
    #11
  12. ajk wrote:
    > On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
    > <> wrote:
    >
    >> What is the C++ way to zero out memory after calling operator new on a
    >> struct?
    >>
    >> A constructor is not possible in this case nor is a class, because the
    >> people using my code c-stlye cast a pointer to the first member of the
    >> struct to a pointer to the entrire struct later on in the code.
    >>

    >
    > so memset() should do the trick
    >
    > "the c++ way" would be to have a class and to initialize it in the
    > ctor
    >
    > e.g.
    >
    > class CMYSTRUCT : public MYSTRUCT
    > {
    > public:
    > CMYSTRUCT() {
    > memset(&overlapped,0,sizeof(overlapped)); mamajama=0; next=NULL; }
    > };
    >
    > Foo()
    > {
    > PMYSTRUCT = new CMYSTRUCT;
    > }
    >
    > or make the MYSTRUCT a class


    Wait a second. Doesn't adding a c'tor make it non-POD and
    hence unsuitable for a memset()?

    - J.
    Jacek Dziedzic, Apr 6, 2007
    #12
  13. On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz" <>
    wrote:

    >What is the C++ way to zero out memory after calling operator new on a
    >struct?
    >
    >A constructor is not possible in this case nor is a class, because the
    >people using my code c-stlye cast a pointer to the first member of the
    >struct to a pointer to the entrire struct later on in the code.
    >


    Since you're essentially doomed to maintaining this code in C anyway, just
    forget about C++ and treat this as a C struct.

    -dr
    Dave Rahardja, Apr 6, 2007
    #13
  14. "Ian Collins" <> wrote in message
    news:...
    > Christopher Pisz wrote:
    >> "Victor Bazarov" <> wrote in message
    >> news:ev3rft$9f1$...
    >>
    >>>Christopher Pisz wrote:
    >>>
    >>>>What is the C++ way to zero out memory after calling operator new on a
    >>>>struct?
    >>>
    >>>To zero out what memory? What does "calling operator new on a struct"
    >>>mean? Could you post code instead of English?
    >>>
    >>>
    >>>>A constructor is not possible in this case nor is a class, because the
    >>>>people using my code c-stlye cast a pointer to the first member of the
    >>>>struct to a pointer to the entrire struct later on in the code.
    >>>

    >>
    >> typedef struct _MYSTRUCT
    >> {
    >> OVERLAPPED overlapped;
    >> int mamajama;
    >> _MYSTRUCT * next;
    >> } *PMYSTRUCT, MYSTRUCT;
    >>
    >> Foo()
    >> {
    >> PMYSTRUCT = new MYSTRUCT;
    >>
    >> // now set all members of the struct to 0
    >> }
    >>
    >>
    >> // later, they do this:
    >> SomeFunction(OVERLAPPED * overlapped)
    >> {
    >> PMYSTRUCT happystruct = (PMYSTRUCT) overlapped;
    >> int x = happystruct->mamajama;
    >>
    >> //etc etc.
    >> }
    >>

    > It looks like someone is very confused between C and C++ idioms.
    >
    > --
    > Ian Collins.




    It isn't me. I know darn well how I would do it if I didn't have 3rd party
    limitations. The problem is that the c style cast, that they force me to
    use, in turn forces the structure.
    Christopher Pisz, Apr 6, 2007
    #14
  15. Christopher Pisz

    Default User Guest

    Jack Klein wrote:

    > On 5 Apr 2007 22:06:14 GMT, "Default User" <>
    > wrote in comp.lang.c++:


    > > If it's a POD, you can use memcpy to set the whole thing to

    > ^^^^^^
    >
    > I know you know that should be memset(). Silly typo.


    Man! Yes, of course.

    I guess my only defense is that I rarely use memset(), even in C.




    Brian
    Default User, Apr 6, 2007
    #15
  16. Jacek Dziedzic wrote:
    > ajk wrote:

    ....
    >
    > Wait a second. Doesn't adding a c'tor make it non-POD and
    > hence unsuitable for a memset()?


    Yes, but that's not what is happening. memset is being called on a
    member which is a POD.
    Gianni Mariani, Apr 7, 2007
    #16
  17. Christopher Pisz

    ajk Guest

    On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
    <> wrote:

    >In this case, you're calling new on CMYSTRUCT and I expect that you'll
    >call delete on a MYSTRUCT. That's undefined. Bad things will happen if
    >don't modify that habbit.


    why do you expect that? ok i have not added any virtual dtor as was
    not providing a full class. just showing the principle.

    >
    >> }
    >>
    >> or make the MYSTRUCT a class

    >
    >MYSTRUCT is a class.
    >


    technically you are right, although what I meant was to make it a
    "real" class with ctor/dtor etc.

    >
    >
    >I just thought of yet another way - this one will create a default
    >constructed or zero initialized POD object depending on what type of
    >pointer you're trying to assign it to.
    >
    >struct InitObj
    >{
    > template <typename T>
    > operator T * ()
    > {
    > return new T();
    > }
    >};
    >
    >// usage - template automagically figures out which type to new
    >PMYSTRUCT * mys = InitObj();
    >
    >int * z = InitObj();
    >
    >
    >Note the lack of a memset call and note that the code will work for POD
    > types as well as non POD types.


    ok that's in a way elegant, but a bit difficult for maintenance
    programmers to troubleshoot
    ajk, Apr 7, 2007
    #17
  18. ajk wrote:
    > On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
    > <> wrote:

    ....
    >>Note the lack of a memset call and note that the code will work for POD
    >> types as well as non POD types.

    >
    >
    > ok that's in a way elegant, but a bit difficult for maintenance
    > programmers to troubleshoot


    That's one serious cop-out. Arguing to have mediocre engineers is an
    unsupportable argument.


    Let's see which one is more maintainable ....
    ..................
    code in common header file....

    struct InitObj
    {
    template <typename T>
    operator T * ()
    {
    return new T();
    }
    };
    ................


    ................
    application code.....
    PMYSTRUCT mys = InitObj();
    ................

    vs - in every instance ...

    memset(&overlapped,0,sizeof(overlapped)) + other mumbo jumbo with all
    kinds of potential for programmer errors.



    I know what I would like my engineers to maintain.
    Gianni Mariani, Apr 7, 2007
    #18
  19. Christopher Pisz wrote:
    > What is the C++ way to zero out memory after calling operator new on a
    > struct?
    >
    > A constructor is not possible in this case nor is a class, because the
    > people using my code c-stlye cast a pointer to the first member of the
    > struct to a pointer to the entrire struct later on in the code.
    >
    >


    Note that there are a number of win32 structs that must be initialized
    not only with zeros but with a member set to the size of the struct.
    e.g. IMAGEHLP_SYMBOL64

    The code below (checked on gcc) should automatically initialize an
    entire struct to zeros and insert the right value in the "sizeofstruct"
    member if it exists.

    I did have a problem with visual studio on this technique a while back,
    let's hope things have improved.


    // This is used to initialize Win32 structs that contain a
    // "sizeofstruct" structure.

    struct NoMemb { char a[1]; };
    struct Memb_SizeOfStruct { char a[2]; };
    struct Memb_sizeofstruct { char a[3]; };

    template <int w_val>
    struct InitSizeOf;

    template <>
    struct InitSizeOf< sizeof( NoMemb ) >
    {
    template <typename U>
    inline static void ObjInitSel( U & obj )
    {
    }
    };

    template <>
    struct InitSizeOf< sizeof( Memb_SizeOfStruct ) >
    {
    template <typename U>
    inline static void ObjInitSel( U & obj )
    {
    obj.SizeOfStruct = sizeof( obj );
    }
    };

    template <>
    struct InitSizeOf< sizeof( Memb_sizeofstruct ) >
    {
    template <typename U>
    inline static void ObjInitSel( U & obj )
    {
    obj.sizeofstruct = sizeof( obj );
    }
    };

    template <typename T>
    struct InitObject
    {
    private:

    struct xA {};
    struct xB : xA {};

    template <int w_size>
    struct Detect
    {
    };

    public:

    template <typename U>
    inline static Memb_sizeofstruct ObjInitSel(
    U & obj, xB * b, Detect< sizeof(&U::sizeofstruct) > * = 0
    );

    template <typename U>
    inline static Memb_SizeOfStruct ObjInitSel(
    U & obj, xB * b, Detect< sizeof(&U::SizeOfStruct) > * = 0
    );

    template <typename U>
    inline static NoMemb ObjInitSel( U & obj, xA * a );

    inline static void ObjInit( T & obj )
    {
    typedef xB * bp;

    InitSizeOf<sizeof( ObjInitSel(obj, bp()))>::ObjInitSel( obj );
    }
    };

    struct InitStruct
    {

    template <typename T>
    inline operator T ()
    {
    T obj = T();
    InitObject<T>().ObjInit( obj );

    return obj;
    }

    template <typename T>
    inline operator T * ()
    {
    T * obj = new T();

    InitObject<T>().ObjInit( * obj );

    return obj;
    }

    };


    /////////// test code

    struct A
    {
    int a;
    char x[10];
    };


    struct B
    {
    int SizeOfStruct;
    char x[15];
    };


    struct C
    {
    int sizeofstruct;
    char x[22];
    };

    B Bfoo()
    {
    return InitStruct();
    }

    C Cfoo()
    {
    return InitStruct();
    }

    A Afoo()
    {
    return InitStruct();
    }


    int main()
    {

    // make dynamically allocated version
    C * c = InitStruct();

    delete c;

    Bfoo();
    Cfoo();
    Afoo();
    }
    Gianni Mariani, Apr 7, 2007
    #19
  20. Christopher Pisz

    ajk Guest

    On Fri, 06 Apr 2007 22:45:16 -0700, Gianni Mariani
    <> wrote:

    >ajk wrote:
    >> On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
    >> <> wrote:

    >...
    >>>Note the lack of a memset call and note that the code will work for POD
    >>> types as well as non POD types.

    >>
    >>
    >> ok that's in a way elegant, but a bit difficult for maintenance
    >> programmers to troubleshoot

    >
    >That's one serious cop-out. Arguing to have mediocre engineers is an
    >unsupportable argument.
    >


    no, its not an unsuportable argument - coding so that its clear is
    what its about.

    >
    >Let's see which one is more maintainable ....
    >.................
    >code in common header file....
    >
    >struct InitObj
    >{
    > template <typename T>
    > operator T * ()
    > {
    > return new T();
    > }
    >};
    >...............
    >
    >
    >...............
    >application code.....
    >PMYSTRUCT mys = InitObj();
    >...............
    >
    >vs - in every instance ...
    >
    >memset(&overlapped,0,sizeof(overlapped)) + other mumbo jumbo with all
    >kinds of potential for programmer errors.
    >


    mumbo jumbo? lol whatever

    >
    >
    >I know what I would like my engineers to maintain.
    >
    >


    your solution has two drawbacks as it allocates memory on heap:
    it requires whoever uses it to know that memory is allocated second
    second allocating memory on heap just because you want to initialize
    it isn't effective.
    ajk, Apr 8, 2007
    #20
    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. Alan Seunarayan

    RegexAssembly?_0.dll and Zero-touch deployment

    Alan Seunarayan, Jun 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,187
    Alan Seunarayan
    Jun 6, 2004
  2. Zhiqiang Ye
    Replies:
    53
    Views:
    10,220
    Dan Pop
    Jun 28, 2004
  3. Gerard Flanagan
    Replies:
    3
    Views:
    433
    Terry Hancock
    Nov 19, 2005
  4. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    659
    Walter
    Jul 1, 2004
  5. Mark

    zero up memory

    Mark, Jan 23, 2012, in forum: C Programming
    Replies:
    44
    Views:
    819
    James Kuyper
    Mar 14, 2012
Loading...

Share This Page