How to find out if an int has no value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Pavlidis

    #16
    Re: How to find out if an int has no value

    Richard <richardhengeve ld@hotmail.com> writes:
    [color=blue]
    > Nicolas Pavlidis <pavnic@sbox.tu graz.at> wrote in news:2r318iF13t vdiU2@uni-
    > berlin.de:[/color]

    [..]
    [color=blue][color=green]
    > > How can a point be empty? As said by Joona scalar Types always have
    > > values, but you're responsible that they get initialized correctly.
    > >
    > > Or do you want to check if the point is (0,0)?
    > >[/color]
    > I want to make a fail save for myself. So, if I'd wrote
    >
    > Point p;
    >
    > somewhere without initializing it, I could check it whit my[/color]

    Ok.
    IMHO it is good style to write "constructorfun ctions", and geting
    variables only over this functions. For example:

    Point newPoint(int x, int y);

    or better:
    Point *newPoint(int x, int y);

    So you can never get problems with uninitialized values, because you're
    simulating a constructor.

    With this way also unnecessary extraflags can be avoided.

    Kind regards,
    Nicolas

    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | pavnic@sbox.tug raz.at | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|

    Comment

    • Barry Schwarz

      #17
      Re: How to find out if an int has no value

      On 18 Sep 2004 14:49:06 GMT, Richard <richardhengeve ld@hotmail.com>
      wrote:
      [color=blue]
      >I've really tried searching for this before I thought of bother you guys
      >with this question. It's such a simple thing, but it's driving me nuts!
      >Is it possible to check if an int (or any other type) has no value.
      >something like:
      >
      >int i;
      >..
      >..
      >..
      >if (i==NULL){
      > ......
      >}
      >
      >I know this doesn't work and I know NULL is a pointer. Is there any way
      >this can be done?
      >TIA.[/color]

      I think you are really asking if it is possible to determine if a
      variable has been initialized. Unless you have another variable which
      keeps track of such a thing the answer is no. The reason is that the
      value stored in an uninitialized variable is indeterminant and any
      attempt to evaluate such a value (even simply passing it to a
      function) invokes undefined behavior.

      Keep in mind that static and file scope variables are always
      initialized at the point of definition (either explicitly or by
      default) and automatic variables may be initialized at that point or
      later by assignment or various functions such as memcpy. Also,
      variables allocated by malloc are not initialized while those
      allocated by calloc are set to all bits zero which may or may not be a
      valid value. Existing variables "relocated" by realloc are unchanged
      while new ones created by realloc are not initialized. (Any that are
      deallocated by free or realloc don't exist anymore so they don't enter
      the discussion.)


      <<Remove the del for email>>

      Comment

      • Ben Pfaff

        #18
        Re: How to find out if an int has no value

        Richard <richardhengeve ld@hotmail.com> writes:
        [color=blue]
        > I've got this:
        >
        > typdef struct
        > {
        > int x;
        > int y;
        > } Point;
        >
        > // I now want to make a function that checks if a Point is empty[/color]

        Based on your later articles I see that you really want to check
        whether Point is uninitialized. Well, there's no fail-safe way
        to do that, but you can do pretty well by adding a "checksum"
        member to the struct, like this:

        typedef struct {
        int x;
        int y;
        int checksum;
        } Point;

        The `checksum' value should depend on `x' and `y' using some kind
        of fixed formula, e.g. `checksum = x + y + 555;' The exact
        formula isn't too important, but there are a couple of
        considerations to keep in mind. First, it should be difficult
        for `checksum' to be accidentally correct. That means that, for
        example, the case of (x, y, checksum) = (0, 0, 0) should *not* be
        valid, and the same goes for other cases where the entire
        structure is filled with a single byte value. (This is the
        reason for the addition of 555 in my suggested formula.
        Otherwise (0, 0, 0) would be valid.) I'm sure you can think of
        some other considerations.

        Then, whenever you update a Point, update the `checksum' member
        as well. Whenever you examine a Point, verify that `checksum' is
        correct. When you free a Point, zero `checksum'.

        This technique is not entirely fail-safe, of course. First, if
        you free a Point without clearing the checksum[*], and then reuse
        the same memory later without reinitializing it, it will check
        out okay. Second, use of uninitialized values technically
        provokes undefined behavior in C. You could get around that by
        treating Point and `checksum' as unsigned char arrays, but it's
        probably not worth it.
        [*] I don't necessary mean via the free() function. There are
        other ways to allocate and free objects.
        --
        "The fact that there is a holy war doesn't mean that one of the sides
        doesn't suck - usually both do..."
        --Alexander Viro

        Comment

        • Ben Pfaff

          #19
          Re: How to find out if an int has no value

          Michael Mair <mairRemove_for _mailinG@ians.u ni-stuttgart.de> writes:
          [color=blue]
          > In order to get a subtle enough kind of safety, create a typedef'ed
          > type only for pointers to points. If you don't have the type, you
          > cannot inadvertently create one uninitialized ;-)[/color]

          A typedef does not create any kind of type safety. If that's not
          what you mean, you'll have to elaborate.
          --
          "...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
          in the uncertainty principle. Which of course makes the above equivalent to
          Schrodinger's pointer..."
          --Anthony McDonald

          Comment

          • Michael Mair

            #20
            Re: How to find out if an int has no value

            >>In order to get a subtle enough kind of safety, create a typedef'ed[color=blue][color=green]
            >>type only for pointers to points. If you don't have the type, you
            >>cannot inadvertently create one uninitialized ;-)[/color]
            >
            > A typedef does not create any kind of type safety. If that's not
            > what you mean, you'll have to elaborate.[/color]

            Right, reusing the stuff from before:

            struct GaussPlaneCoord inate {
            ....
            };

            typedef struct GaussPlaneCoord inate *CoP;


            My point was that usually people are lazy and will rather use
            CoP points = GetGPC(5);
            than
            struct GaussPlaneCoord inate points[5];
            just because of the easyness of the "intended way":

            If the intended way to use the coordinates is to get a pointer and
            "create" the coordinate it points to by allocating and initializing
            it and you have a function doing both, there is a good chance that
            someone -- even yourself late in the night -- conveniently forgets
            about that. If there is a typedef for "struct GaussPlaneCoord inate",
            then it is easier to do so. If there is not, and you have a convenient,
            short typedef for "struct GaussPlaneCoord inate *", chances are that
            the lazy programmer will go for the typedef'ed type he knows.
            (Sounds far-fetched, I know, but I have had an experience quite some
            time ago which led me to recode a module and remove all potentially
            dangerous typedefs because the people using the stuff for their
            applications liked to use, e.g. STRUCT instead of STRUCT *, when
            doing something "just quickly", never caring about control words and
            the effect when passing the stuff to functions. As it was, it worked
            99% of the time and I could not track the misbehavior down to their
            code for a long time but checked the stuff I had inherited from
            someone else.
            When I had removed everything they liked to abuse, the "inexplicab le
            error" rate went down dramatically, even though they still could
            have circumvented it by using the very unhandy names of the original
            structures; but as the intended way was now easier to use, they were
            too lazy...)


            --Michael

            Comment

            • Gregory Pietsch

              #21
              Re: How to find out if an int has no value

              Richard <richardhengeve ld@hotmail.com> wrote in message news:<Xns9568AC A83E6EErichardh engeveldhotm@19 4.134.2.2>...[color=blue]
              > Joona I Palaste <palaste@cc.hel sinki.fi> wrote in
              > news:cihhuo$jqa $1@oravannahka. helsinki.fi:
              >[color=green]
              > > Richard <richardhengeve ld@hotmail.com> scribbled the following:[color=darkred]
              > >> I've really tried searching for this before I thought of bother you
              > >> guys with this question. It's such a simple thing, but it's driving
              > >> me nuts! Is it possible to check if an int (or any other type) has no
              > >> value. something like:[/color]
              > >
              > > Scalar types, such as ints, always have values. Therefore your
              > > question does not make sense.
              > >[/color]
              >
              > Okay, than I should be more spesific.
              > I've got this:
              >
              > typdef struct
              > {
              > int x;
              > int y;
              > } Point;
              >
              > // I now want to make a function that checks if a Point is empty
              >
              > int pointEmpty(Poin t p)
              > {
              > return (p /*isEmpty*/ ? 1 : 0)
              > }
              >
              > How should I do that?
              > TIA[/color]


              One way: add a value to the structure:

              typedef struct {
              int x, y, is_empty;
              } Point;

              then the implementation of the function is easy:

              int pointEmpty(Poin t p)
              {
              return (p.is_empty != 0);
              }

              Just remember to initialize the value somehow.

              Gregory Pietsch

              Comment

              • Richard

                #22
                Re: How to find out if an int has no value

                Nicolas Pavlidis <pavnic@sbox.tu graz.at> wrote in
                news:2r35t8F14s pplU1@uni-berlin.de:
                [color=blue]
                > Richard <richardhengeve ld@hotmail.com> writes:
                >[color=green]
                >> Nicolas Pavlidis <pavnic@sbox.tu graz.at> wrote in
                >> news:2r318iF13t vdiU2@uni- berlin.de:[/color]
                >
                > [..]
                >[color=green][color=darkred]
                >> > How can a point be empty? As said by Joona scalar Types always have
                >> > values, but you're responsible that they get initialized correctly.
                >> >
                >> > Or do you want to check if the point is (0,0)?
                >> >[/color]
                >> I want to make a fail save for myself. So, if I'd wrote
                >>
                >> Point p;
                >>
                >> somewhere without initializing it, I could check it whit my[/color]
                >
                > Ok.
                > IMHO it is good style to write "constructorfun ctions", and geting
                > variables only over this functions. For example:
                >
                > Point newPoint(int x, int y);
                >
                > or better:
                > Point *newPoint(int x, int y);
                >
                > So you can never get problems with uninitialized values, because
                > you're simulating a constructor.
                >
                > With this way also unnecessary extraflags can be avoided.
                >
                > Kind regards,
                > Nicolas
                >[/color]

                I like 'simulating a constructor'. I didn't know that it was good c
                practice to simulate oo-programming. Thanks!

                Kind regards,
                Richard

                Comment

                • Emmanuel Delahaye

                  #23
                  Re: How to find out if an int has no value

                  Richard wrote on 19/09/04 :[color=blue]
                  > I like 'simulating a constructor'. I didn't know that it was good c
                  > practice to simulate oo-programming. Thanks![/color]

                  It's very a common practice.

                  --
                  Emmanuel
                  The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                  The C-library: http://www.dinkumware.com/refxc.html

                  "C is a sharp tool"

                  Comment

                  Working...