Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

What might happen if I ignore warning?

+1
−2
#include <iostream>
using namespace std;

int linearSearch(int array[], int n, int key){
	for(int i=0;i<=n;i++){
		if(array[i]==key){
			return i;
		}
	}
}

int main() {
  int balance[] = {10,240,120,150,100};
  cout<<linearSearch(balance, sizeof(balance)/sizeof(balance[0]), 150);
  return 0;
} 

When executing the code I got an warning. Should I really care of warning? I got expected output. Although, I was thinking of warning. What might happen if I ignore warning?

test.cpp: In function ‘int linearSearch(int*, int, int)’:
test.cpp:10:1: warning: control reaches end of non-void function [-Wreturn-type]
   10 | }
      | ^

When I add return 0; at end of for loop I don't get the error anymore. The warning was saying that the linearSearch method (function) wasn't returning always. But, can warning crash my applications?

Replying to last edit of Alexei : My question was about any type of warning not only above one. Above one was just an example. Anyway, I got more accurate answer in the thread

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Yes, it can crash, because this leads to undefined behaviour. Try to search for a number that's not i... (2 comments)

2 answers

+4
−0

Warnings most often mean "here is a bug which will likely cause your program to crash or misbehave". They do not mean "here's something that you can optionally fix when you can spare the time". See What must a C compiler do when it finds an error?

In your specific case, ignoring the warning leads to undefined behavior, meaning anything can happen. This is mentioned in the C standard C17 6.9.1/12:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

For example runaway code, crashes caused by stack corruption/wrong calling convention, wrong results calculated on garbage values, code getting incorrectly optimized and parts of it removed etc etc. What is undefined behavior and how does it work?

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Not returning a value on all code paths is error-prone because the function might not return the expected value in the non-covered cases. That is why it is better to explicitly return a value.

As a side note, Languages such as C# and Java prohibit this (compiler error).

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »