is const necessary in eg int compar(const void *, const void *)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbeauty@gmail.c0m

    is const necessary in eg int compar(const void *, const void *)

    Is the keyword const necessary in the comparison function in qsort and
    bsearch?

    int (*compar)(const void *, const void *)

    If the pointer cannot be dereferenced why worry if the pointed object
    will be modified?
  • Ian Collins

    #2
    Re: is const necessary in eg int compar(const void *, const void*)

    lovecreatesbeau ty@gmail.c0m wrote:
    Is the keyword const necessary in the comparison function in qsort and
    bsearch?
    >
    int (*compar)(const void *, const void *)
    >
    If the pointer cannot be dereferenced why worry if the pointed object
    will be modified?
    Why can't the pointer be dereferenced?

    --
    Ian Collins

    Comment

    • Peter Nilsson

      #3
      Re: is const necessary in eg int compar(const void *, const void *)

      "lovecreatesbea ...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
      Is the keyword const necessary in the comparison function
      in qsort and bsearch?
      Not strictly necessary, but prudent.
      int (*compar)(const void *, const void *)
      >
      If the pointer cannot be dereferenced why worry if the
      pointed object will be modified?
      When the pointer(s) are converted to the appropriate object
      type, they certainly can be dereferenced, and usually are.

      --
      Peter

      Comment

      • lovecreatesbeauty@gmail.c0m

        #4
        Re: is const necessary in eg int compar(const void *, const void *)

        On Oct 15, 10:55 am, Peter Nilsson <ai...@acay.com .auwrote:
        "lovecreatesbea ...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
        Is the keyword const necessary in the comparison function
        in qsort and bsearch?
        >
        Not strictly necessary, but prudent.
        >
        int (*compar)(const void *, const void *)
        >
        If the pointer cannot be dereferenced why worry if the
        pointed object will be modified?
        >
        When the pointer(s) are converted to the appropriate object
        type, they certainly can be dereferenced, and usually are.
        >
        Thank you.

        I ignored this, the const qualifier on the original pointers will
        still require the same qulifier to be applied on those appropriate
        object.

        Comment

        • Richard Heathfield

          #5
          Re: is const necessary in eg int compar(const void *, const void *)

          lovecreatesbeau ty@gmail.c0m said:
          Is the keyword const necessary in the comparison function in qsort and
          bsearch?
          >
          int (*compar)(const void *, const void *)
          Yes.
          If the pointer cannot be dereferenced why worry if the pointed object
          will be modified?
          The pointer *can* be dereferenced after conversion to the appropriate type.
          For example, look at all the dereferencing going on here:

          #include <time.h>

          int cmp_tm(const void *vleft, const void *vright)
          {
          const struct tm *left = vleft;
          const struct tm *right = vright;
          int diff =
          (left->tm_year right->tm_year) - (left->tm_year < right->tm_year);
          if(0 == diff)
          {
          diff =
          (left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
          }
          if(0 == diff)
          {
          diff =
          (left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
          }
          /* hour, min, sec similarly */

          return diff;
          }


          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • lovecreatesbeauty@gmail.c0m

            #6
            Re: is const necessary in eg int compar(const void *, const void *)

            On Oct 15, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
            lovecreatesbea. ..@gmail.c0m said:
            If the pointer cannot be dereferenced why worry if the pointed object
            will be modified?
            >
            The pointer *can* be dereferenced after conversion to the appropriate type.
            For example, look at all the dereferencing going on here:
            >
            Yes, thanks. I got it.
              if(0 == diff)
              {
                diff =
                (left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
              }
              if(0 == diff)
              {
                diff =
                (left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
              }
            >
            why don't you put your code together and create another same selection
            block :)

            Comment

            • Richard Heathfield

              #7
              Re: is const necessary in eg int compar(const void *, const void *)

              lovecreatesbeau ty@gmail.c0m said:
              On Oct 15, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
              >lovecreatesbea ...@gmail.c0m said:
              If the pointer cannot be dereferenced why worry if the pointed object
              will be modified?
              >>
              >The pointer *can* be dereferenced after conversion to the appropriate
              >type. For example, look at all the dereferencing going on here:
              >>
              >
              Yes, thanks. I got it.
              >
              >if(0 == diff)
              >{
              >diff =
              >(left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
              >}
              >if(0 == diff)
              >{
              >diff =
              >(left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
              >}
              >>
              >
              why don't you put your code together and create another same selection
              block :)
              I haven't the faintest idea what you're talking about.

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Nate Eldredge

                #8
                Re: is const necessary in eg int compar(const void *, const void *)

                "lovecreatesbea uty@gmail.c0m" <lovecreatesbea uty@gmail.comwr ites:
                On Oct 15, 10:55 am, Peter Nilsson <ai...@acay.com .auwrote:
                >"lovecreatesbe a...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
                Is the keyword const necessary in the comparison function
                in qsort and bsearch?
                >>
                >Not strictly necessary, but prudent.
                >>
                int (*compar)(const void *, const void *)
                >>
                If the pointer cannot be dereferenced why worry if the
                pointed object will be modified?
                >>
                >When the pointer(s) are converted to the appropriate object
                >type, they certainly can be dereferenced, and usually are.
                >>
                >
                Thank you.
                >
                I ignored this, the const qualifier on the original pointers will
                still require the same qulifier to be applied on those appropriate
                object.
                Hmm, but wait a minute. I think your original point was valid.

                For a function like

                void foo(const int *p) { /* ... */ }

                the compiler will complain if you attempt to assign to, or otherwise
                modify, *p. So the compiler helps you make good on the promise you're
                making to the caller, that *p will not be modified; this is the whole
                point of const. You could circumvent this by assigning to *(int *)p,
                but at least the fact that you had to introduce a cast serves as warning
                that you might be doing something wrong.

                But for a function like

                void bar(const void *p) { /* ... */ }

                where p is known to point to an int, you *have* to cast p before you can
                do much with it. And if you're going to cast it, you could just as
                easily cast it to `int *' as to `const int *', and the compiler will (in
                my tests) remain silent either way. So the help you get from using
                const is much less.

                Now if we also have

                void qux(void *);

                and `bar' attempts to call `qux(p)', the compiler does whine. (This in
                fact is exactly the whine that occurs if you pass a function taking
                `void *' arguments to `qsort'.) But certainly a lot of the protection
                has been lost. One could argue that this makes the use of
                `const void *' sort of pointless, but if nothing else it provides some
                documentation, I suppose.

                Comment

                • Richard Heathfield

                  #9
                  Re: is const necessary in eg int compar(const void *, const void *)

                  Nate Eldredge said:

                  <snip>
                  But for a function like
                  >
                  void bar(const void *p) { /* ... */ }
                  >
                  where p is known to point to an int, you *have* to cast p before you can
                  do much with it.
                  Not so:

                  #include <stdio.h>

                  void bar(const void *p)
                  {
                  const int *pi = p;
                  printf("%d\n", *pi);
                  } /* look, ma, no casts */

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • James Kuyper

                    #10
                    Re: is const necessary in eg int compar(const void *, const void*)

                    lovecreatesbeau ty@gmail.c0m wrote:
                    On Oct 15, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    >lovecreatesbea ...@gmail.c0m said:
                    >>If the pointer cannot be dereferenced why worry if the pointed object
                    >>will be modified?
                    >The pointer *can* be dereferenced after conversion to the appropriate type.
                    >For example, look at all the dereferencing going on here:
                    >>
                    >
                    Yes, thanks. I got it.
                    >
                    > if(0 == diff)
                    > {
                    > diff =
                    > (left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
                    > }
                    > if(0 == diff)
                    > {
                    > diff =
                    > (left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
                    > }
                    >>
                    >
                    why don't you put your code together and create another same selection
                    block :)
                    If the value of diff was 0 at the first if(), then diff gets
                    recalculated, in which case it might be non-0 for the second if(), so
                    you can't combine the two blocks into a single if() block.

                    Comment

                    • Eric Sosman

                      #11
                      Re: is const necessary in eg int compar(const void *, const void*)

                      Richard Heathfield wrote:
                      lovecreatesbeau ty@gmail.c0m said:
                      >
                      >Is the keyword const necessary in the comparison function in qsort and
                      >bsearch?
                      >>
                      >int (*compar)(const void *, const void *)
                      >
                      Yes.
                      Well, either "yes" or "no" depending on what "necessary" means.

                      Yes, it is "necessary, " because qsort() and bsearch() specify
                      `const' as part of the signature of the comparison function they
                      call. If you try to pass a pointer to a function with a different
                      signature, the compiler will complain and may reject your code. So
                      `const' is "necessary" if you want to be sure the code compiles.

                      No, it is not "necessary, " as shown by the fact that qsort() and
                      bsearch() existed and worked just fine before `const' was invented.
                      (Of course, they weren't exactly qsort() and bsearch() as we now
                      know them, because their declarations differed from today's versions.
                      Still, they were work-alikes and were used just as today's are.)
                      Without `const' and without prototypes to put it in, the primordial
                      functions required the programmer to "get it right" without the help
                      of compiler complaints to warn when something was wrong. However,
                      it was possible to "get it right" even without assistance, and in
                      that sense `const' was not a "necessary" addition to the functions'
                      signatures.

                      If your code is correct, you can remove every `const' and it
                      will still be correct and its meaning will be unchanged (imagine
                      that the removal also magically affects the Standard library).
                      The value of `const' is that when you add it "correctly" it can
                      change run-time errors to compile-time errors.

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • Richard Heathfield

                        #12
                        Re: is const necessary in eg int compar(const void *, const void *)

                        Eric Sosman said:
                        Richard Heathfield wrote:
                        >lovecreatesbea uty@gmail.c0m said:
                        >>
                        >>Is the keyword const necessary in the comparison function in qsort and
                        >>bsearch?
                        >>>
                        >>int (*compar)(const void *, const void *)
                        >>
                        >Yes.
                        >
                        Well, either "yes" or "no" depending on what "necessary" means.
                        >
                        Yes, it is "necessary, " because qsort() and bsearch() specify
                        `const' as part of the signature of the comparison function they
                        call.
                        Right.
                        If you try to pass a pointer to a function with a different
                        signature, the compiler will complain and may reject your code. So
                        `const' is "necessary" if you want to be sure the code compiles.
                        Right again.
                        No, it is not "necessary, " as shown by the fact that qsort() and
                        bsearch() existed and worked just fine before `const' was invented.
                        My answer was written in the context of standard C.

                        <snip>

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Antoninus Twink

                          #13
                          Re: is const necessary in eg int compar(const void *, const void *)

                          On 15 Oct 2008 at 8:45, Richard Heathfield wrote:
                          I'm of the opinion that it's always possible, and usually preferable,
                          to avoid casting.
                          As usual, your blind dogmatism leads you to talk nonsense.

                          A couple of trivial examples that come quickly to mind:

                          1) When passing a NULL pointer to variadic functions, you need an
                          explicit cast, e.g.
                          s = mystrconcat("he llo", " ", "world", "!", (char *) NULL);

                          2) If you have a type that you know is an integer type but is typedef'd
                          in a system-specific header, then to portably print it you need a cast
                          to the widest integer type:

                          opaque_integer_ type i = somefunction(42 );
                          printf("The answer is %llu\n", (unsigned long long) i);

                          Comment

                          • Richard Tobin

                            #14
                            Re: is const necessary in eg int compar(const void *, const void *)

                            In article <slrngfc37b.e4q .nospam@nospam. invalid>,
                            Antoninus Twink <nospam@nospam. invalidwrote:
                            >1) When passing a NULL pointer to variadic functions, you need an
                            >explicit cast, e.g.
                            >s = mystrconcat("he llo", " ", "world", "!", (char *) NULL);
                            I'm sure someone will point out how it *can* be done without a cast,
                            but of course a cast is the natural way to do it.
                            >2) If you have a type that you know is an integer type but is typedef'd
                            >in a system-specific header, then to portably print it you need a cast
                            >to the widest integer type:
                            >
                            >opaque_integer _type i = somefunction(42 );
                            >printf("The answer is %llu\n", (unsigned long long) i);
                            Alternatively the implementor can provide a macro for a format string,
                            as C99 does (PRIdMAX etc).

                            -- Richard



                            --
                            Please remember to mention me / in tapes you leave behind.

                            Comment

                            • Eric Sosman

                              #15
                              Re: is const necessary in eg int compar(const void *, const void*)

                              Richard Heathfield wrote:
                              Eric Sosman said:
                              >
                              >Richard Heathfield wrote:
                              >>lovecreatesbe auty@gmail.c0m said:
                              >>>
                              >>>Is the keyword const necessary in the comparison function in qsort and
                              >>>bsearch?
                              >>>>
                              >>>int (*compar)(const void *, const void *)
                              >>Yes.
                              > Well, either "yes" or "no" depending on what "necessary" means.
                              >>
                              > Yes, it is "necessary, " because qsort() and bsearch() specify
                              >`const' as part of the signature of the comparison function they
                              >call.
                              >[...]
                              > No, it is not "necessary, " as shown by the fact that qsort() and
                              >bsearch() existed and worked just fine before `const' was invented.
                              >
                              My answer was written in the context of standard C.
                              ... which does not define "necessary. " My answer tried to
                              address two of the meanings the O.P might have intended: "Necessary"
                              because That's Just The Way It Is, or "necessary" for a deeper reason.
                              Was the Committee obliged to add `const' to qsort() and bsearch()?
                              I think not. But since they chose to do so, programmers *are* now
                              obliged to use them in the manner the Committee chose.

                              Contrast this with another design decision the Committee made:
                              "How many arguments should qsort() take?" Since the Committee was
                              charged to codify existing practice, the answer "four" was forced
                              upon them; the necessity arose externally and the Committee passed it
                              along. But making the comparison function's parameters `const' was
                              something the Committee did of its own volition, not something they
                              did because it was "necessary. "

                              --
                              Eric.Sosman@sun .com

                              Comment

                              Working...