In Xcode, I found semantic issue in the code snippet












2















The program tried to compile, but I found out that the error displays, 'control may reach end of non-void function'. I suppose how to do with if condition after putting the return compare. I have been figuring out how to solve this issue.



   int compare(const void *a, const void *b)
{
if (*(int *)a < *(int *)b)
return -1;
if (*(int *)a == *(int *)b)
return 0;
if (*(int *)a > *(int *)b)
return 1;
}









share|improve this question


















  • 2





    (a) it’s just a warning and (b) one of your comparisons is redundant

    – Paul R
    Nov 25 '18 at 9:23
















2















The program tried to compile, but I found out that the error displays, 'control may reach end of non-void function'. I suppose how to do with if condition after putting the return compare. I have been figuring out how to solve this issue.



   int compare(const void *a, const void *b)
{
if (*(int *)a < *(int *)b)
return -1;
if (*(int *)a == *(int *)b)
return 0;
if (*(int *)a > *(int *)b)
return 1;
}









share|improve this question


















  • 2





    (a) it’s just a warning and (b) one of your comparisons is redundant

    – Paul R
    Nov 25 '18 at 9:23














2












2








2








The program tried to compile, but I found out that the error displays, 'control may reach end of non-void function'. I suppose how to do with if condition after putting the return compare. I have been figuring out how to solve this issue.



   int compare(const void *a, const void *b)
{
if (*(int *)a < *(int *)b)
return -1;
if (*(int *)a == *(int *)b)
return 0;
if (*(int *)a > *(int *)b)
return 1;
}









share|improve this question














The program tried to compile, but I found out that the error displays, 'control may reach end of non-void function'. I suppose how to do with if condition after putting the return compare. I have been figuring out how to solve this issue.



   int compare(const void *a, const void *b)
{
if (*(int *)a < *(int *)b)
return -1;
if (*(int *)a == *(int *)b)
return 0;
if (*(int *)a > *(int *)b)
return 1;
}






c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 9:21









CarlitosCarlitos

112




112








  • 2





    (a) it’s just a warning and (b) one of your comparisons is redundant

    – Paul R
    Nov 25 '18 at 9:23














  • 2





    (a) it’s just a warning and (b) one of your comparisons is redundant

    – Paul R
    Nov 25 '18 at 9:23








2




2





(a) it’s just a warning and (b) one of your comparisons is redundant

– Paul R
Nov 25 '18 at 9:23





(a) it’s just a warning and (b) one of your comparisons is redundant

– Paul R
Nov 25 '18 at 9:23












4 Answers
4






active

oldest

votes


















7














The 'control may reach end of non-void function' shouldn't be detected as there is actually not such possibility. May be, the data flow analyze doesn't detect this properly.



However, there is a much nicer alternative to implement this and it will solve your issue as well:



int compare(const void *a, const void *b)
{
return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
}


Btw. the performance of this code is better as you don't need any branches anymore.



A small MCVE for demonstration:



#include <iostream>

int compare(const void *a, const void *b)
{
return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
}

int main()
{
int a = 1, b = 2;
std::cout << "compare(&a, &b): " << compare(&a, &b) << 'n';
std::cout << "compare(&b, &a): " << compare(&b, &a) << 'n';
std::cout << "compare(&a, &a): " << compare(&a, &a) << 'n';
return 0;
}


Output:



compare(&a, &b): -1
compare(&b, &a): 1
compare(&a, &a): 0


Live Demo on coliru



I must admit that somebody “forced” me to this nice comparison trick when I answered



SO: Sorting an array of integers in alternate fashion using qsort function..



This is the explanation I gave there:




How it works:



In case a < b: (a > b) - (a < b)0 - 1-1



In case a == b: (a > b) - (a < b)0 - 00



In case a > b: (a > b) - (a < b)1 - 01







share|improve this answer





















  • 2





    Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

    – StoryTeller
    Nov 25 '18 at 9:36



















3














Don't use C-style casts in C++.



As you know that you covered all conditions, the last test is redundant:



int compare(const void *a, const void *b)
{
int a1 = *static_cast<const int *>(a);
int b1 = *static_cast<const int *>(b);
if (a1 < b1)
return -1;
if (a1 == b1)
return 0;
return 1;
}





share|improve this answer

































    0














    If you don't know what is the actual type of the objects behind those pointers, you may want to do:



    #include <cstring>

    int f(const void *a, const void *b)
    {
    int ai;
    int bi;
    std::memcpy(&ai, a, sizeof(int));
    std::memcpy(&bi, b, sizeof(int));
    return (bi < ai) - (ai < bi);
    }





    share|improve this answer































      0














      The main point here is that the compiler can’t recognize that your conditions cover all possible cases. => if none of your conditions is met, the function ends without a return statement. You should use if - else if - else here:



      if (…) { … }
      else if (…) { … }
      else { … }


      This will help compiler understand your code better. And you drop once of the conditions.



      Avoiding branches is a good idea, usually at the price of loosing readability. Check out @Scheff’s answer for that.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466152%2fin-xcode-i-found-semantic-issue-in-the-code-snippet%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        7














        The 'control may reach end of non-void function' shouldn't be detected as there is actually not such possibility. May be, the data flow analyze doesn't detect this properly.



        However, there is a much nicer alternative to implement this and it will solve your issue as well:



        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }


        Btw. the performance of this code is better as you don't need any branches anymore.



        A small MCVE for demonstration:



        #include <iostream>

        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }

        int main()
        {
        int a = 1, b = 2;
        std::cout << "compare(&a, &b): " << compare(&a, &b) << 'n';
        std::cout << "compare(&b, &a): " << compare(&b, &a) << 'n';
        std::cout << "compare(&a, &a): " << compare(&a, &a) << 'n';
        return 0;
        }


        Output:



        compare(&a, &b): -1
        compare(&b, &a): 1
        compare(&a, &a): 0


        Live Demo on coliru



        I must admit that somebody “forced” me to this nice comparison trick when I answered



        SO: Sorting an array of integers in alternate fashion using qsort function..



        This is the explanation I gave there:




        How it works:



        In case a < b: (a > b) - (a < b)0 - 1-1



        In case a == b: (a > b) - (a < b)0 - 00



        In case a > b: (a > b) - (a < b)1 - 01







        share|improve this answer





















        • 2





          Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

          – StoryTeller
          Nov 25 '18 at 9:36
















        7














        The 'control may reach end of non-void function' shouldn't be detected as there is actually not such possibility. May be, the data flow analyze doesn't detect this properly.



        However, there is a much nicer alternative to implement this and it will solve your issue as well:



        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }


        Btw. the performance of this code is better as you don't need any branches anymore.



        A small MCVE for demonstration:



        #include <iostream>

        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }

        int main()
        {
        int a = 1, b = 2;
        std::cout << "compare(&a, &b): " << compare(&a, &b) << 'n';
        std::cout << "compare(&b, &a): " << compare(&b, &a) << 'n';
        std::cout << "compare(&a, &a): " << compare(&a, &a) << 'n';
        return 0;
        }


        Output:



        compare(&a, &b): -1
        compare(&b, &a): 1
        compare(&a, &a): 0


        Live Demo on coliru



        I must admit that somebody “forced” me to this nice comparison trick when I answered



        SO: Sorting an array of integers in alternate fashion using qsort function..



        This is the explanation I gave there:




        How it works:



        In case a < b: (a > b) - (a < b)0 - 1-1



        In case a == b: (a > b) - (a < b)0 - 00



        In case a > b: (a > b) - (a < b)1 - 01







        share|improve this answer





















        • 2





          Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

          – StoryTeller
          Nov 25 '18 at 9:36














        7












        7








        7







        The 'control may reach end of non-void function' shouldn't be detected as there is actually not such possibility. May be, the data flow analyze doesn't detect this properly.



        However, there is a much nicer alternative to implement this and it will solve your issue as well:



        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }


        Btw. the performance of this code is better as you don't need any branches anymore.



        A small MCVE for demonstration:



        #include <iostream>

        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }

        int main()
        {
        int a = 1, b = 2;
        std::cout << "compare(&a, &b): " << compare(&a, &b) << 'n';
        std::cout << "compare(&b, &a): " << compare(&b, &a) << 'n';
        std::cout << "compare(&a, &a): " << compare(&a, &a) << 'n';
        return 0;
        }


        Output:



        compare(&a, &b): -1
        compare(&b, &a): 1
        compare(&a, &a): 0


        Live Demo on coliru



        I must admit that somebody “forced” me to this nice comparison trick when I answered



        SO: Sorting an array of integers in alternate fashion using qsort function..



        This is the explanation I gave there:




        How it works:



        In case a < b: (a > b) - (a < b)0 - 1-1



        In case a == b: (a > b) - (a < b)0 - 00



        In case a > b: (a > b) - (a < b)1 - 01







        share|improve this answer















        The 'control may reach end of non-void function' shouldn't be detected as there is actually not such possibility. May be, the data flow analyze doesn't detect this properly.



        However, there is a much nicer alternative to implement this and it will solve your issue as well:



        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }


        Btw. the performance of this code is better as you don't need any branches anymore.



        A small MCVE for demonstration:



        #include <iostream>

        int compare(const void *a, const void *b)
        {
        return (*(const int*)b < *(const int*)a) - (*(const int*)a < *(const int*)b);
        }

        int main()
        {
        int a = 1, b = 2;
        std::cout << "compare(&a, &b): " << compare(&a, &b) << 'n';
        std::cout << "compare(&b, &a): " << compare(&b, &a) << 'n';
        std::cout << "compare(&a, &a): " << compare(&a, &a) << 'n';
        return 0;
        }


        Output:



        compare(&a, &b): -1
        compare(&b, &a): 1
        compare(&a, &a): 0


        Live Demo on coliru



        I must admit that somebody “forced” me to this nice comparison trick when I answered



        SO: Sorting an array of integers in alternate fashion using qsort function..



        This is the explanation I gave there:




        How it works:



        In case a < b: (a > b) - (a < b)0 - 1-1



        In case a == b: (a > b) - (a < b)0 - 00



        In case a > b: (a > b) - (a < b)1 - 01








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 9:46

























        answered Nov 25 '18 at 9:26









        ScheffScheff

        7,96821325




        7,96821325








        • 2





          Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

          – StoryTeller
          Nov 25 '18 at 9:36














        • 2





          Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

          – StoryTeller
          Nov 25 '18 at 9:36








        2




        2





        Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

        – StoryTeller
        Nov 25 '18 at 9:36





        Compilers also know to look for this idiom to optimize, which is neat (godbolt.org/z/l80TDj).

        – StoryTeller
        Nov 25 '18 at 9:36













        3














        Don't use C-style casts in C++.



        As you know that you covered all conditions, the last test is redundant:



        int compare(const void *a, const void *b)
        {
        int a1 = *static_cast<const int *>(a);
        int b1 = *static_cast<const int *>(b);
        if (a1 < b1)
        return -1;
        if (a1 == b1)
        return 0;
        return 1;
        }





        share|improve this answer






























          3














          Don't use C-style casts in C++.



          As you know that you covered all conditions, the last test is redundant:



          int compare(const void *a, const void *b)
          {
          int a1 = *static_cast<const int *>(a);
          int b1 = *static_cast<const int *>(b);
          if (a1 < b1)
          return -1;
          if (a1 == b1)
          return 0;
          return 1;
          }





          share|improve this answer




























            3












            3








            3







            Don't use C-style casts in C++.



            As you know that you covered all conditions, the last test is redundant:



            int compare(const void *a, const void *b)
            {
            int a1 = *static_cast<const int *>(a);
            int b1 = *static_cast<const int *>(b);
            if (a1 < b1)
            return -1;
            if (a1 == b1)
            return 0;
            return 1;
            }





            share|improve this answer















            Don't use C-style casts in C++.



            As you know that you covered all conditions, the last test is redundant:



            int compare(const void *a, const void *b)
            {
            int a1 = *static_cast<const int *>(a);
            int b1 = *static_cast<const int *>(b);
            if (a1 < b1)
            return -1;
            if (a1 == b1)
            return 0;
            return 1;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 9:34

























            answered Nov 25 '18 at 9:25









            Matthieu BrucherMatthieu Brucher

            16k32141




            16k32141























                0














                If you don't know what is the actual type of the objects behind those pointers, you may want to do:



                #include <cstring>

                int f(const void *a, const void *b)
                {
                int ai;
                int bi;
                std::memcpy(&ai, a, sizeof(int));
                std::memcpy(&bi, b, sizeof(int));
                return (bi < ai) - (ai < bi);
                }





                share|improve this answer




























                  0














                  If you don't know what is the actual type of the objects behind those pointers, you may want to do:



                  #include <cstring>

                  int f(const void *a, const void *b)
                  {
                  int ai;
                  int bi;
                  std::memcpy(&ai, a, sizeof(int));
                  std::memcpy(&bi, b, sizeof(int));
                  return (bi < ai) - (ai < bi);
                  }





                  share|improve this answer


























                    0












                    0








                    0







                    If you don't know what is the actual type of the objects behind those pointers, you may want to do:



                    #include <cstring>

                    int f(const void *a, const void *b)
                    {
                    int ai;
                    int bi;
                    std::memcpy(&ai, a, sizeof(int));
                    std::memcpy(&bi, b, sizeof(int));
                    return (bi < ai) - (ai < bi);
                    }





                    share|improve this answer













                    If you don't know what is the actual type of the objects behind those pointers, you may want to do:



                    #include <cstring>

                    int f(const void *a, const void *b)
                    {
                    int ai;
                    int bi;
                    std::memcpy(&ai, a, sizeof(int));
                    std::memcpy(&bi, b, sizeof(int));
                    return (bi < ai) - (ai < bi);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 9:47









                    AcornAcorn

                    5,68811238




                    5,68811238























                        0














                        The main point here is that the compiler can’t recognize that your conditions cover all possible cases. => if none of your conditions is met, the function ends without a return statement. You should use if - else if - else here:



                        if (…) { … }
                        else if (…) { … }
                        else { … }


                        This will help compiler understand your code better. And you drop once of the conditions.



                        Avoiding branches is a good idea, usually at the price of loosing readability. Check out @Scheff’s answer for that.






                        share|improve this answer




























                          0














                          The main point here is that the compiler can’t recognize that your conditions cover all possible cases. => if none of your conditions is met, the function ends without a return statement. You should use if - else if - else here:



                          if (…) { … }
                          else if (…) { … }
                          else { … }


                          This will help compiler understand your code better. And you drop once of the conditions.



                          Avoiding branches is a good idea, usually at the price of loosing readability. Check out @Scheff’s answer for that.






                          share|improve this answer


























                            0












                            0








                            0







                            The main point here is that the compiler can’t recognize that your conditions cover all possible cases. => if none of your conditions is met, the function ends without a return statement. You should use if - else if - else here:



                            if (…) { … }
                            else if (…) { … }
                            else { … }


                            This will help compiler understand your code better. And you drop once of the conditions.



                            Avoiding branches is a good idea, usually at the price of loosing readability. Check out @Scheff’s answer for that.






                            share|improve this answer













                            The main point here is that the compiler can’t recognize that your conditions cover all possible cases. => if none of your conditions is met, the function ends without a return statement. You should use if - else if - else here:



                            if (…) { … }
                            else if (…) { … }
                            else { … }


                            This will help compiler understand your code better. And you drop once of the conditions.



                            Avoiding branches is a good idea, usually at the price of loosing readability. Check out @Scheff’s answer for that.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 25 '18 at 11:11









                            iPiratiPirat

                            1,542818




                            1,542818






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466152%2fin-xcode-i-found-semantic-issue-in-the-code-snippet%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'