Why "control reaches end of non-void function"? - gcc

I'm cross-compiling using ARM/GNU C Compiler:6.3.1 with optimizations -O0. A simple function like this:
static uint8_t xyzzy(void) {
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
}
results in an error message:
Error control reaches end of non-void function [-Werror=return-type]
Is it really the case that the compiler doesn't know that one of those return statements must always be executed? Or is it me that is confused? And what is a recommended fix?
(P.S.: No, this is not a duplicate of Control reach end of non-void function, since I do have an else as the last clause.)

Simply add a return statement at the end of your function. GCC considers that your function doesn't have a return statement.
static uint8_t xyzzy(void)
{
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
return 0; // This will never be reached
}
You can get rid of unnecessary {}s and the elses:
static uint8_t xyzzy(void)
{
if (<predicate one>)
return 1;
if (<predicate two>)
return 2;
return 4; // Executed if none of the above conditions are met.
}

Related

SIGSEGV Fault in infix to postfix using stack

When I tried making if condition to while loop to remove more than one operator from stack in bracket (f+g/h) here output should be (fgh/+) but i am not able to run the code with while loop my output is coming (fgh/) due to if condition , how do I put while loop without SIGSEGV, im getting run-time error SIGSEGV ?
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to convert an infix expression to a postfix expression.
string infixToPostfix(string s)
{
// Your code here
stack<char> op;
string res;
int i=0;
while(i<s.length()){
if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z' ){
res.push_back(s[i]);
cout<<res<<" ";
}
else if(s[i]=='(')
op.push(s[i]);
else if(s[i]==')'){
if(op.top()!='('){ //here SIGSEGV I want this in while loop not if statement
res.push_back(s[i]);
op.pop();
cout<<res<<" ";
}
op.pop();
}
else {
if(op.empty())
op.push(s[i]);
else if(precedence(s[i])>precedence(op.top()))
op.push(s[i]);
else if(precedence(s[i])<precedence(op.top())){
while(precedence(s[i])<precedence(op.top())){
res.push_back(op.top());
op.pop();
}
op.push(s[i]);
}
else{
res.push_back(op.top());
op.pop();
op.push(s[i]);
}
}
i++;
}
return res;
}
int precedence(char a) //precedence function
{
if (a == '^')
return 3;
else if (a == '*' || a == '/')
return 2;
else if (a == '+' || a == '-')
return 1;
else if (a == '(' || a == ')')
return 0;
}
};
int main(){
int t;
t=1;
cin.ignore(INT_MAX, '\n');
while(t--){
string exp;
cin>>exp;
Solution ob;
cout<<ob.infixToPostfix(exp)<<endl;
}
return 0;
}

Translate recursive function calls into cost-efficient representation

I am trying to use some static analysis tools to check a program with extensive usage of recursive calls. Conceptually, it is something like this:
int counter = 0;
int access = 0;
extern int nd (); // a nondeterministic value
void compute1();
void compute2();
int get()
{
static int fpa[2] = {2, 2}; // each function can be called for twice
int k = nd() % 2;
if (fpa[k] > 0) {
fpa[k]--;
return k+1;
}
else
return 0;
}
void check_r(int* x) {
if (x == &counter) {
__VERIFIER_assert(!(access == 2));
access = 1;
}
}
void check_w(int* x) {
if (x == &counter) {
__VERIFIER_assert((access == 0));
access = 2;
}
}
void schedule() {
for (int i = 0; i < 5; i++) {
int fp = get();
if (fp == 0)
return;
elif (fp == 1)
compute1();
elif (fp == 2)
compute2();
}
}
void compute1()
{
// some computations
...
schedule(); // recursive call
check_w(&counter); // check write access
...
}
void compute2()
{
// some computations
...
schedule(); //recursive call
check_r(&counter);
...
}
int main()
{
schedule();
return 0;
}
My tentative tests show that due to the recursive call, the static analysis becomes too slow to terminate.
While in principle, I can somehow rewrite the recursive call into a switch statement or so, but the problem is that before the recursive call schedule, compute1 and compute2 functions have performed nontrivial amount of computations already, and it is difficult to save the program context for further usage.
I have been trapped to optimize this cases for a few days, but just cannot come up with a even ad-hoc solution. Could anyone please provide some comments and suggestions to get rid of the recursive call here? Thank you so much.
To me it looks like all the schedule function is doing is deciding whether to call compute1 or compute2 and all the get is doing is ensuring a single function is never called more than twice. I don't think the recursive call from compute to schedule is necessary then since there is never more than two calls. The recursion here seem to imply that every time we can successfully call one of the compute functions we wan't another chance to call compute again
void schedule() {
int chances = 1;
for (int i = 0; i < 5 || chances > 0; i++) {
int fp = get();
if (fp == 0){
chances--;
if(chances < 0)
chances = 0;
continue;
}
elif (fp == 1){
compute1(); chances++;
}
elif (fp == 2){
compute2(); chances++;
}
}
}
void compute1()
{
// some computations
...
//schedule(); remove
check_w(&counter); // check write access
...
}
void compute2()
{
// some computations
...
//schedule(); remove
check_r(&counter);
...
}
This code is a bit confusing so please clarify if I made any incorrect assumptions

Sudoku Backtracking

Below is my solve method. When I call it in my main method, nothing happens and all the succeeding it is not executed but no error is reported by eclipse.
public boolean solve(int r, int c){
if(c>8){
c=0;
r++;
}
if(r>8){
return true;
}
while(table[r][c].value!=0){
c++;
if(c>8){
c=-0;
r++;
}
if(r>8){
return true;
}
}
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
solve(r,c);
}
}
table[r][c].value=0;
return false;
}
Will this algorithm backtrack? If not, why?
It looks like a logical error and hence eclipse does not report anything.
In for loop section of your code, you should have something like this
for(int k=1;k<10;k++){
if(table[r][c].checkRow(k)&&table[r][c].checkCol(k)&&table[r][c].checkCube(k)){
table[r][c].value=k;
if(solve(r,c)){
return true;
}
table[r][c].value=0;
}
}
In your case you are un-assigning the table outside the for loop, which prevents the code from backtracking.
Here is my code for solving sudoku. Hope it helps.

warning: deprecated conversion from string constant to ‘char*’

I have the warnings from the gcc compiler showing the const char issue.
How to get rid of the warnings?
Thanks,
Michael
char * str_convert(int op) {
/*returns the string corresponding to an operation opcode. Used for screen output.*/
if(op == PLUS) {
return "plus";
}
else if (op == MULT) {
return "mult";
}
else if (op == SUBS) {
return "subs";
}
else if (op == MOD) {
return "mod";
}
else if (op == ABS) {
return "abs";
}
else if (op == MAX) {
return "max";
}
else if (op == MIN) {
return "min";
}
else {
return NULL;
}
}
I think the fix is adding const to the return type (to prevent modification of the contents).
I'd also change the if cascade to switch / case, but that's unrelated to the problem.
const char * str_convert(int op) {
/*returns the string corresponding to an operation opcode. Used for screen output.*/
switch (op) {
case ABS: return "abs";
case MAX: return "max";
case MIN: return "min";
case MOD: return "mod";
case MULT: return "mult";
case PLUS: return "plus";
case SUBS: return "subs";
default: return NULL;
}
}
You may also wish to consider using templated 'op' value as the compiler will replace the jump table it will use for the the switch statement implementation, evaluated at runtime, with a compile time evaluated version that calls on of N functions, depending on the template value.
template <int op>
const char * str_convert(void)
{
/*returns the string corresponding to an operation opcode. Used for screen output.*/
switch (op)
{
case ABS: return "abs";
case MAX: return "max";
case MIN: return "min";
case MOD: return "mod";
case MULT: return "mult";
case PLUS: return "plus";
case SUBS: return "subs";
default: return NULL;
}
}

for loop Arduino/C

My aim is to create a for loop that iterates through numbers and once it reaches the maximum, it stops printing. So far I managed to create a piece of code that stops printing the x but it keeps printing zeroes. How can I stop Serial.print() function to be executed once the iteration reached the maximum value?
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
void loop(){
for(x=0;x<8;x++){
Serial.println(x);
delay(300);
if(x==7){
f = true;
}
if(f){
break;
}
}
}
Something like below should serve. Btw, I like to name my vars something meaningful to avoid potential confusion and make the code more intelligible.
(In general you are better off posting questions to the Arduino forum. More traffic and more knowledgeable/helpful people == more likelihood of getting an answer.)
int current;
int limit;
boolean complete;
void setup(){
Serial.begin(9600);
current = 0;
limit = 8;
complete = false;
}
void loop(){
if (!complete){
while (true){
Serial.println(current);
current++;
if (current >= limit){
complete = true;
break;
}
delay(300);
}
}
}
The key is the word loop - that function is called repetitively!
If you want something to happen once, do it in setup(), or (as another answer suggests), have a flag to keep track of the fact that you've done it already.
Another way (since x is global) would be:
void loop() {
if (x < 8) {
Serial.println(x);
x++;
}
}
or, getting rid of the global variable:
void loop() {
static int x = 0;
if (x < 8) {
Serial.println(x);
x++;
}
}
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
Very similar to your code, just put the print statement in the if statement and you're set.
void loop(){
for(x=0;x<8;x++){
if(!f) {
Serial.println(x);
}
delay(300);
if(x==7){
f = true;
}
}
}

Resources