C struct as class member variable, zero "init" data in class constructor
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C struct as class member variable, zero "init" data in class constructor

  1. #1
    Join Date
    Aug 2009
    Posts
    439

    C struct as class member variable, zero "init" data in class constructor

    Hello,

    So, at my work we use a static analysis tool and it is pointing out some uninitialized variables. One of which is a class member variable which is a C struct. Now, that variable is already declared in the header file for the class.

    Currently in the class constructor I am doing:
    Code:
    memset( &thestruct, 0, sizeof( thestruct ) );
    Is there a C++ way to do this? I Googled this but all I really found was:
    Code:
    StructDef thestruct = {};
    Which doesn't really apply here. Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    Re: C struct as class member variable, zero "init" data in class constructor

    In C++ you usually implement a (default and probably some other) ctor(s) in your structure and do the default and other initialization within the ctor(s)
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2009
    Posts
    439

    Re: C struct as class member variable, zero "init" data in class constructor

    True, but I am dealing with a C struct and so wasn't quite sure.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: C struct as class member variable, zero "init" data in class constructor

    Are you compiling the file as a 'C' file or a 'C++' file?

    Viggy

  5. #5
    Join Date
    Aug 2009
    Posts
    439

    Re: C struct as class member variable, zero "init" data in class constructor

    I'd have to say C++ because we use both C and C++ files when we build the executable.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: C struct as class member variable, zero "init" data in class constructor

    Try adding a constructor to the struct. If you compile with C++ the compilation will succeed but in C it will not.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: C struct as class member variable, zero "init" data in class constructor

    Quote Originally Posted by Alterah View Post
    Is there a C++ way to do this? I Googled this but all I really found was:
    Code:
    StructDef thestruct = {};
    Which doesn't really apply here. Thanks.
    In an initializer list, you could use a function that returns an initialized instance of the struct. Something like
    Code:
    template <class T>
    T MakeZeroInstance()
    {
        T t = {};
        return t;
    }
    
    MyClass::MyClass()
    : myStruct(MakeZeroInstance<MyStruct>())
    {
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Oct 2008
    Posts
    1,131

    Re: C struct as class member variable, zero "init" data in class constructor

    you don't need to write a ctor, just use value-initialization that for a C-struct implies zero-initialization:

    Code:
    struct A{ /* ... */ };
    struct B{ A a; B():a(){} }; // a is zero-initialized, eg: a's scalar members are set to zero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center