In Xcode, I found semantic issue in the code snippet
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++
add a comment |
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++
2
(a) it’s just a warning and (b) one of your comparisons is redundant
– Paul R
Nov 25 '18 at 9:23
add a comment |
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++
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++
c++
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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 - 0
⇒0
In case
a > b
:(a > b) - (a < b)
⇒1 - 0
⇒1
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
add a comment |
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;
}
add a comment |
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);
}
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 - 0
⇒0
In case
a > b
:(a > b) - (a < b)
⇒1 - 0
⇒1
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
add a comment |
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 - 0
⇒0
In case
a > b
:(a > b) - (a < b)
⇒1 - 0
⇒1
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
add a comment |
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 - 0
⇒0
In case
a > b
:(a > b) - (a < b)
⇒1 - 0
⇒1
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 - 0
⇒0
In case
a > b
:(a > b) - (a < b)
⇒1 - 0
⇒1
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Nov 25 '18 at 9:34
answered Nov 25 '18 at 9:25
Matthieu BrucherMatthieu Brucher
16k32141
16k32141
add a comment |
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 25 '18 at 9:47
AcornAcorn
5,68811238
5,68811238
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 11:11
iPiratiPirat
1,542818
1,542818
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
(a) it’s just a warning and (b) one of your comparisons is redundant
– Paul R
Nov 25 '18 at 9:23