NULL value?

Discussion in 'C++' started by Vicent Giner-Bosch, Feb 7, 2011.

  1. Hello.

    I have a very basic question for you. I hope you can help me.

    I have a variable with a customized data type (a struct, in fact).
    There are some moments where the variable exists but does not still
    contain anything.

    Which is the best way to manage that? I mean, I want to detect when
    that variable has not been updated to anything yet.

    For me, the most natural option would be initializing that variable to
    "NULL" or something similar, but I don't know if such a concept exists
    in C++ or not.

    As it is a struct, I can't use "zero" for that —or can I?

    Which is the most usual approach in this case of matters, when
    programming in C++??

    Thank you very much in advance for your answers!

    --
    Vicent ^^
     
    Vicent Giner-Bosch, Feb 7, 2011
    #1
    1. Advertising

  2. Vicent Giner-Bosch

    cppquester Guest

    On 7 Feb., 17:21, Vicent Giner-Bosch <> wrote:
    > Hello.
    >
    > I have a very basic question for you. I hope you can help me.
    >
    > I have a variable with a customized data type (a struct, in fact).
    > There are some moments where the variable exists but does not still
    > contain anything.
    >
    > Which is the best way to manage that? I mean, I want to detect when
    > that variable has not been updated to anything yet.
    >
    > For me, the most natural option would be initializing that variable to
    > "NULL" or something similar, but I don't know if such a concept exists
    > in C++ or not.
    >
    > As it is a struct, I can't use "zero" for that —or can I?
    >
    > Which is the most usual approach in this case of matters, when
    > programming in C++??
    >
    > Thank you very much in advance for your answers!
    >
    > --
    > Vicent ^^


    Often it is a practical apporach to have one field of the struct set
    to an invalid value and then check for this.
    (e. g. myStruct.ID == -1 while valid values for ID start with 0)
    The invalid value could be set in the default constructor.

    Cheers,
    Marc
     
    cppquester, Feb 7, 2011
    #2
    1. Advertising


  3. > Often it is a practical apporach to have one field of the struct set
    > to an invalid value and then check for this.
    > (e. g. myStruct.ID == -1 while valid values for ID start with 0)
    > The invalid value could be set in the default constructor.
    >
    > Cheers,
    > Marc


    The problem is I am not using constructors or classes, just
    structures.

    In my case, I have something like this:


    // My Struct is something defined in a header file :

    MyStruct ms ;

    // ... here there is some code not involving ms ...

    do {

    // The first time we enter here, the content in ms doesn't mean
    anything

    // Then, in this do-while loop we perform some operations on ms,
    // so the next time we enter here, ms will have a proper value

    } while (something) ; // end do




    Maybe I can define something equivalent to "-1", but using the struct
    (I mean, something like {-1, -1, -1}, or similar)

    Any other approach??

    --
    Vicent
     
    Vicent Giner-Bosch, Feb 7, 2011
    #3
  4. Vicent Giner-Bosch

    Paul Guest


    >"Vicent Giner-Bosch" <> wrote in message
    >news:...
    >Hello.


    >For me, the most natural option would be initializing that variable to
    >"NULL" or something similar, but I don't know if such a concept exists
    >in C++ or not.


    >As it is a struct, I can't use "zero" for that —or can I?


    Yes you can initialise a struct like so:
    /*******************/
    struct foo{
    char x;
    int y;
    };
    foo my_pod = {0};
    /******************/

    I think you need to access one of its data member to do the logic i.e:
    /******************/
    if(my_pod.x) //This should work
    if(my_pod) //This won't work
    /*****************/


    Alternatively you could use a null initialised pointer to struct. Whatever
    suits your needs.

    HTH
     
    Paul, Feb 7, 2011
    #4
  5. Vicent Giner-Bosch

    Paul Guest

    Paul, Feb 7, 2011
    #5
  6. Paul <> wrote:
    > I think you need to access one of its data member to do the logic i.e:
    > /******************/
    > if(my_pod.x) //This should work
    > if(my_pod) //This won't work
    > /*****************/


    It's rather hypocritical how you insult all the people in this
    newsgroup while you yourself have no idea what you are talking about,
    and have to resort to expressions like "I think you need to", and
    "this should work".

    What exactly is the situation where if(my_pos.x) "should work"? To
    see if the object has been initialized properly? What if it has been
    initialized properly to zero?
     
    Juha Nieminen, Feb 7, 2011
    #6
  7. Vicent Giner-Bosch <> writes:

    > // My Struct is something defined in a header file :
    >
    > MyStruct ms ;
    >
    > // ... here there is some code not involving ms ...
    >
    > do {
    >
    > // The first time we enter here, the content in ms doesn't mean
    > // anything
    >
    > // Then, in this do-while loop we perform some operations on ms,
    > // so the next time we enter here, ms will have a proper value
    >
    > } while (something) ; // end do


    Refactor your code as:

    // Place here the code of the first iteration
    // ms doesn't mean anything useful, so it's not yet declared

    MyStruct ms = ...; // construct and initialize your object

    while (something) {

    // Here is the regular body of the loop
    // using and probably updating ms

    }

    Yes, some code is duplicated. But that code would anyway be peppered
    with tests on the "availability" of ms. If this is really a problem,
    think again about your algorithm.

    BTW, there is no essential difference between a struct and a class. You
    can have a constructor in a class.

    -- Alain.
     
    Alain Ketterlin, Feb 7, 2011
    #7
  8. Vicent Giner-Bosch

    Paul Guest

    "Juha Nieminen" <> wrote in message
    news:4d502a8a$0$2845$...
    > Paul <> wrote:
    >> I think you need to access one of its data member to do the logic i.e:
    >> /******************/
    >> if(my_pod.x) //This should work
    >> if(my_pod) //This won't work
    >> /*****************/

    >
    > It's rather hypocritical how you insult all the people in this
    > newsgroup while you yourself have no idea what you are talking about,
    > and have to resort to expressions like "I think you need to", and
    > "this should work".
    >
    > What exactly is the situation where if(my_pos.x) "should work"? To
    > see if the object has been initialized properly? What if it has been
    > initialized properly to zero?
    >

    Your intent is unclear.
     
    Paul, Feb 7, 2011
    #8
  9. Vicent Giner-Bosch

    Jorgen Grahn Guest

    On Mon, 2011-02-07, Vicent Giner-Bosch wrote:
    >
    >> Often it is a practical apporach to have one field of the struct set
    >> to an invalid value and then check for this.
    >> (e. g. myStruct.ID == -1 while valid values for ID start with 0)
    >> The invalid value could be set in the default constructor.

    >
    > The problem is I am not using constructors or classes, just
    > structures.


    Then start using them. You have no excuse not to.

    > Maybe I can define something equivalent to "-1", but using the struct
    > (I mean, something like {-1, -1, -1}, or similar)


    What I use varies a lot with the problem, but I often end up with a
    member function 'bool Foo::valid() const' or 'bool Foo::empty()
    const'. Sometimes there's a corresponding flag beneath, but usually
    you don't need to add one -- there is already some obviously invalid
    state for the type, for example that -1 of yours.

    Also see boost::eek:ptional.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 7, 2011
    #9
  10. Vicent Giner-Bosch

    James Kanze Guest

    On Feb 7, 4:21 pm, Vicent Giner-Bosch <> wrote:

    > I have a very basic question for you. I hope you can help me.


    > I have a variable with a customized data type (a struct, in fact).
    > There are some moments where the variable exists but does not still
    > contain anything.


    > Which is the best way to manage that? I mean, I want to detect when
    > that variable has not been updated to anything yet.


    > For me, the most natural option would be initializing that variable to
    > "NULL" or something similar, but I don't know if such a concept exists
    > in C++ or not.


    > As it is a struct, I can't use "zero" for that —or can I?


    > Which is the most usual approach in this case of matters, when
    > programming in C++??


    The fallible idiom is more or less standard for this, but only
    if you have no "normal" invalid value; you don't see Fallible
    used with a pointer or reference type, for example. (In the
    case of a reference, of course, the fallible type for a
    reference is a pointer.)

    --
    James Kanze
     
    James Kanze, Feb 8, 2011
    #10
    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. Kivak Wolf
    Replies:
    2
    Views:
    10,095
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,234
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    5
    Views:
    26,919
    Mike Schilling
    Mar 29, 2006
  4. moonhk
    Replies:
    5
    Views:
    555
    moonhk
    Oct 11, 2006
  5. Col
    Replies:
    1
    Views:
    636
    peter koch
    Apr 21, 2006
Loading...

Share This Page