Is there a standard way to check if struct member is uninitialized?

This is a discussion on Is there a standard way to check if struct member is uninitialized? within the C++ Programming forums, part of the General Programming Boards category; Is there a standard way to check if struct member is uninitialized? Maybe something like this?: Code: if (STRUCT_OBJECT_NAME.member_name == ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Is there a standard way to check if struct member is uninitialized?

    Is there a standard way to check if struct member is uninitialized?

    Maybe something like this?:

    Code:
    if (STRUCT_OBJECT_NAME.member_name == null)
      do_nothing();
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Is there a standard way to check if struct member is uninitialized?
    Yes. Initialize said members. It sounds kind of paradoxical, but no one said that initialized variables had to be usable variables from the beginning. The point of initialization is so that a variable contains values you expect from the beginning.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Yes. Initialize said members. It sounds kind of paradoxical, but no one said that initialized variables had to be usable variables from the beginning. The point of initialization is so that a variable contains values you expect from the beginning.
    The problem is, I expect for the members to be initialized before doing anything with said members, but I want to guard against getting a struct object which has certain members unitialized accidentally, just in case. Note that I'm using a class object to deal with the struct object.
    This is just to fool-proof my class.
    Last edited by Programmer_P; 02-26-2011 at 12:35 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Programmer_P View Post
    The problem is, I expect for the members to be initialized before doing anything with said members, but I want to guard against getting a struct object which has certain members unitialized accidentally, just in case. Note that I'm using a class object to deal with the struct object.
    So why not initialize all members in the constructor? If it's a pointer, you can initialize to NULL. If not, then it may be possible you'll have to add a boolean value to indicate wether a member is set.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The problem is, I expect for the members to be initialized before doing anything with said members
    You cannot initialize a struct without initializing all the members.
    Code:
    struct foo {
      int a;
      char *b;
      float c;
    } 
    bar = { 0, NULL, 0.0f };
    If you missed a member, then the compiler will complain about too few initializers in an initializer list.

    So you either initialize, or you do not initialize.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    You cannot initialize a struct without initializing all the members.
    Code:
    struct foo {
      int a;
      char *b;
      float c;
    } 
    bar = { 0, NULL, 0.0f };
    If you missed a member, then the compiler will complain about too few initializers in an initializer list.

    So you either initialize, or you do not initialize.
    ohh...
    Not even with this syntax?:

    Code:
    foo bar;
    bar.a = 0;
    bar.b = "A C-style string";
    I wasn't talking about initializer lists, btw, just to clarify what I meant by "initialize". I just meant give the members some content, i.e. since said members are vectors, I just wanted to check if they had objects added to them. But then I realized, all I had to do was check if said vector was empty. But I still would like to know if a standard method exists which allows you to check if a struct member (no matter what the type is) has been given content or not. But I guess not. Oh well...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by EVOEx View Post
    So why not initialize all members in the constructor? If it's a pointer, you can initialize to NULL. If not, then it may be possible you'll have to add a boolean value to indicate wether a member is set.
    Because it was just meant to be a simple "S_html_attr" struct, which is operated on by my C_html4_attributes class. I did not want to tie the struct down to a specific set of supported html attributes. Instead, I define the set in C_html4_attributes, and use objects of S_html_attr as members of that class.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    foo bar;
    bar.a = 0;
    bar.b = "A C-style string";

    This is assignment, not initialization. Initialization is part of a declaration so either foo has a constructor which initializes all the members -- including by way of the annoying "default initialization" which is like no initialization, or you do it the C way.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    foo bar;
    bar.a = 0;
    bar.b = "A C-style string";

    This is assignment, not initialization. Initialization is part of a declaration so either foo has a constructor which initializes all the members -- including by way of the annoying "default initialization" which is like no initialization, or you do it the C way.
    Well, whatever the proper coding term is, that's what I meant.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    There is no way to check this. Any value (like NULL for pointers) can be result of memory allocation. Just initialize them in the constructor.
    Because it was just meant to be a simple "S_html_attr" struct, which is operated on by my C_html4_attributes class. I did not want to tie the struct down to a specific set of supported html attributes. Instead, I define the set in C_html4_attributes, and use objects of S_html_attr as members of that class.
    Sorry, but we have no idea what your own structs do in your own code.
    I never put signature, but I decided to make an exception.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by kmdv View Post
    There is no way to check this. Any value (like NULL for pointers) can be result of memory allocation. Just initialize them in the constructor.

    Sorry, but we have no idea what your own structs do in your own code.
    Ok, just checking. I found another way to do what I wanted to do.

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by kmdv View Post
    Sorry, but we have no idea what your own structs do in your own code.
    Well, technically, it doesn't "do" anything. It is just a storage struct.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by Programmer_P View Post
    Ok, just checking. I found another way to do what I wanted to do.
    Really? What were you actually trying to achieve?

    On your original question, there is no standard way to detect an uninitialised variable (be it a struct member or not). Even accessing the value of such a variable gives undefined behaviour (which means, since comparison involves retrieving the value, that comparing the value with anything does not work either).
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    Really? What were you actually trying to achieve?
    I have a member function of my C_html4_attributes class, called "getAttr" which returns an S_html_attr. One of its parameters requires an attribute value to be passed. Inside the function, I check to see if the attribute value passed exists inside the supported attribute values vector container member of the struct "S_html_attr". I was wanting to not return anything if the supported attribute values member of the specified attribute had not been set yet. That is what prompted starting this thread.

    But like already mentioned, I later realized I could always just check to see if the supported attribute values container member of the specified attribute was empty, by calling the empty() function of the vector. So that is what I'm actually doing.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by Programmer_P View Post
    ...I was wanting to not return anything if the supported attribute values member of the specified attribute had not been set yet...
    Throw an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21