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();
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.
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.Is there a standard way to check if struct member is uninitialized?
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 01:35 PM.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
You cannot initialize a struct without initializing all the members.The problem is, I expect for the members to be initialized before doing anything with said members
If you missed a member, then the compiler will complain about too few initializers in an initializer list.Code:struct foo { int a; char *b; float c; } bar = { 0, NULL, 0.0f };
So you either initialize, or you do not initialize.
ohh...
Not even with this syntax?:
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...Code:foo bar; bar.a = 0; bar.b = "A C-style string";
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
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.
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.
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.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.
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).
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.