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; Originally Posted by Elysia Throw an exception. Possibly. But he would have arrived at that conclusion himself if "not returning ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by Elysia View Post
    Throw an exception.
    Possibly. But he would have arrived at that conclusion himself if "not returning anything" meant avoiding a return instruction altogether.

    To draw a parallel, searches do not throw exceptions for a failed search, but they also could be written to not return "anything" if anything means a usable iterator.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Experience has taught me that typically for anything failed, an exception is what is wanted, since you usually want to skip a body of code.
    So I usually go by the rule: if something fails, throw an exception.

    Otherwise add an "is there" function.
    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.

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Possibly. But he would have arrived at that conclusion himself if "not returning anything" meant avoiding a return instruction altogether.

    To draw a parallel, searches do not throw exceptions for a failed search, but they also could be written to not return "anything" if anything means a usable iterator.
    Yeah, that's the logic I was originally thinking in. By "not returning anything", I really meant not return anything useful, as in return a S_html_attr with none of its members assigned anything.

    However, Elysia's suggestion sounds like a much better approach, so I think I'll use that instead.
    Last edited by Programmer_P; 02-26-2011 at 05:43 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, I am going to throw an exception. However, how do I end the program from a function other than int main()?
    What standard function can I call, that will work in any OS?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    You are asking the wrong question. The question you should be asking is where do I handle the exception? The obvious answer to that is in main if you are simply going to terminate your program. Be sure to include detailed information about the error in the exception you throw.
    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.

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    You are asking the wrong question. The question you should be asking is where do I handle the exception? The obvious answer to that is in main if you are simply going to terminate your program. Be sure to include detailed information about the error in the exception you throw.
    Well, that may not be the best approach, I'm thinking, as I'm writing a set of classes for coding the Html language (I guess this would be called a library?), and I want to "package" everything together if I can. int main() will be in my actual program which uses this stuff, and I don't want to have to worry about specifics inside the set of classes, such as exceptions class functions throw (and then again, is an exception thrown inside of a class even visible inside int main()??). Also, I may want to write multiple programs using this library, and that's another reason why I don't want to concern myself with handling exceptions of class functions inside int main() functions.

    Of course, if I'm thinking wrong again, feel free to correct me.
    Last edited by Programmer_P; 02-26-2011 at 06:36 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    And you should be asking if it is an exception at all.

    http://www.drdobbs.com/184401836

    I wouldn't use exceptions for this problem because it doesn't sound like one exceptions will solve. You've described a failed search to me and there are ways to deal with that.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    And you should be asking if it is an exception at all.

    http://www.drdobbs.com/184401836

    I wouldn't use exceptions for this problem because it doesn't sound like one exceptions will solve. You've described a failed search to me and there are ways to deal with that.
    Actually, there's quite a bit of difference from a failed seach and a failed getAttr to me.
    A failed search means said thing was not found, which is not an error because that is valid that that should happen: said object being searched in does not have to have the thing being searched for, otherwise there would be no reason to search now, would there?
    A failed getAttr means that said attribute value did not exist in the specified attribute's supported attribute values.
    Because you should only pass an attribute value which exists in attribute's supported attribute values, you failed to call the function properly, and therefore an exception is thrown. Of course, obviously, I don't HAVE to do it that way, but that would make it crystal clear to the caller that what he tried to pass was not supported. A proper call to C_html4_attributes::getAttr() should only occur after a S_html_attr_value is first created (and/or obtained by calling C_html4_attributes::getSupportedAttrValues() and then iterating through the vector<S_html_attr_value> returned) and its members assigned values that match the values of an S_html_attr_value supported by the attribute.
    Last edited by Programmer_P; 02-26-2011 at 07:30 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    You knowing what to do is the important thing.

    Actually, there's quite a bit of difference from a failed seach and a failed getAttr to me.
    Like kmdv said, I am not aware of the significance of your own code. I wanted you to know whether this was really an exception or not, rather than using exceptions because Elysia said so.

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    You knowing what to do is the important thing.



    Like kmdv said, I am not aware of the significance of your own code. I wanted you to know whether this was really an exception or not, rather than using exceptions because Elysia said so.
    I know that. I was just stating the reason why I decided to throw an exception (and it wasn't just because Elysia said to do it, it was because it seemed like a good idea, based on what my getAttr function is supposed to do).

    Thanks for the help, everyone.

    P.S. My above question about how to end the program from a function other than main() still stands.

    EDIT: I guess exit() will work.
    Last edited by Programmer_P; 02-26-2011 at 08:16 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #26
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Technically you're supposed to use the STL with exceptions in mind. main() could look like this:
    Code:
    try {
       vector<string> elements;
       elements.push_back(string("one"));
       elements.push_back(string("two"));
       elements.push_back(string("three"));
       
       // code ...
    }
    catch (bad_alloc &loadError)
    {
       cerr << loadError.what() << endl;
       return 0;
    }
    // other catches
    
    // no-fail code here
    return 0; 
    
    // verily, return 0 is guaranteed to work ;)
    The moral: If you are writing a library, you need to provide documentation of your exceptions, so that people who use it (including yourself) will know what to catch. It is the user's responsibility to write this kind of code, not the supplier's.

  12. #27
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Technically you're supposed to use the STL with exceptions in mind. main() could look like this:
    Code:
    try {
       vector<string> elements;
       elements.push_back(string("one"));
       elements.push_back(string("two"));
       elements.push_back(string("three"));
       
       // code ...
    }
    catch (bad_alloc &loadError)
    {
       cerr << loadError.what() << endl;
       return 0;
    }
    // other catches
    
    // no-fail code here
    return 0; 
    
    // verily, return 0 is guaranteed to work ;)
    The moral: If you are writing a library, you need to provide documentation of your exceptions, so that people who use it (including yourself) will know what to catch. It is the user's responsibility to write this kind of code, not the supplier's.
    Maybe...but how do you throw exceptions from/inside a class object to a catch(er) in int main() ? Are the exceptions automatically visisble to main()?
    Last edited by Programmer_P; 02-26-2011 at 08:34 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #28
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Ah, well exception's unwind the call stack.

    So if you end up throwing an exception, from push_back() it might be caught where push_back() was used. If not there, then in the function that called that function, even if it was an object method. If not in the object method, then the object is destroyed when it has to go further into the call stack... until ultimately, main(). Now, either main() catches the exception or the program terminates badly with what's called an unhandled exception.

    So writing exception safe classes is important for the users of your class: your class should always meet one of the guarantees elaborated on by the link I put. And you should make sure that you have a working destructor so that the call stack unwinding does not leak your object's memory.

  14. #29
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Ah, well exception's unwind the call stack.

    So if you end up throwing an exception, from push_back() it might be caught where push_back() was used. If not there, then in the function that called that function, even if it was an object method. If not in the object method, then the object is destroyed when it has to go further into the call stack... until ultimately, main(). Now, either main() catches the exception or the program terminates badly with what's called an unhandled exception.
    Would it be considered bad coding style to throw and catch the object's own exceptions inside the object's member functions anyway? That way, int main() does not have to catch any exceptions (or at least, not from my library)?
    So writing exception safe classes is important for the users of your class: your class should always meet one of the guarantees elaborated on by the link I put. And you should make sure that you have a working destructor so that the call stack unwinding does not leak your object's memory.
    How does it do that?
    And what would I need to destroy other than "new" objects of my class (which I don't have any of, at this time anyway) in my destructor?
    I read the whole link you gave me (which, btw, confirmed to me that I should throw an exception for a failed getAttr), but it doesn't answer that question.
    Last edited by Programmer_P; 02-26-2011 at 08:44 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #30
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by Programmer_P View Post
    Would it be considered bad coding style to throw and catch the object's own exceptions inside the object's member functions anyway? That way, int main() does not have to catch any exceptions (or at least, not from my library)?
    If you do that, you better rethrow it, else exceptions are no better than error codes. An exception the caller is not responsible for may as well not be an exception at all. And I know what you want.

    Quote Originally Posted by Programmer_P View Post
    Because you should only pass an attribute value which exists in attribute's supported attribute values, you failed to call the function properly, and therefore an exception is thrown. Of course, obviously, I don't HAVE to do it that way, but that would make it crystal clear to the caller that what he tried to pass was not supported.

    Quote Originally Posted by Programmer_P View Post
    How does it do that?
    It can depend on where you throw. For example, if you throw from a constructor, the destructors of data members will be called, but you could very easily leak memory from newed pointers. Usually you have to catch and rethrow the exception in order to clean up such things. And there are probably more places in your code than just the constructor with those circumstances, where you need to rethrow.

    Quote Originally Posted by Programmer_P View Post
    And what would I need to destroy other than "new" objects of my class (which I don't have any of, at this time anyway) in my destructor?
    It's part of the exception feature. Since a function's call stack is going to be unwound, it is your responsibility to make sure all destructors are called, (i.e. that all memory is reclaimed) in order to not leak memory. Provide a working no-fail destructor.

    Quote Originally Posted by Programmer_P View Post
    I read the whole link you gave me (which, btw, confirmed to me that I should throw an exception for a failed getAttr), but it doesn't answer that question.
    Annoyingly, everything you need to know about exceptions will not be in one place. You basically have to know everything you can about exceptions (which is a textbook chapter topic) before you use them smartly. They are not a drag and drop feature.

Page 2 of 3 FirstFirst 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, 03:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Replies: 2
    Last Post: 04-19-2008, 01: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, 04:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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