No Return Statement In Function Returning Non-void - Error =return-type
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or error non void function should return a value posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss no return statement in function returning non-void c++ Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes return-statement with no value in function returning 'int' a minute: Sign up Why does flowing off the end of a non-void function without returning a value not produce a compiler error? up vote 112 down vote favorite 39 Ever since I realized many years ago, that this doesn't return with a value in function returning void enabled by default produce an error by default, (in gcc at least) I've always wondered why? I understand that you can issue compiler flags to produce a warning, but shouldn't it always be an error? Why does it make sense for a non-void function not returning value to be valid? An example as requested in the comments: #include
No Return Statement In Function Returning Non-void [-wreturn-type]
g++ share|improve this question edited Mar 22 at 3:42 M.M 72.4k662131 asked Oct 22 '09 at 21:21 Catskul 5,94485389 8 Alternatively, I treat all warnings however trivial like errors, and I activate all the warnings I can (with local deactivation if necessary... but then it's clear in the code why). –Matthieu M. Oct 23 '09 at 10:33 3 -Werror=return-type will treat just that warning as an error. I just ignored the warning and the couple of minutes of frustration tracking down an invalid this pointer lead me here and to this conclusion. –jozxyqk Nov 4 '13 at 7:46 add a comment| 7 Answers 7 active oldest votes up vote 120 down vote accepted C99 and C++ standards don't require functions to return a value. The missing return statement in a value-returning function will be defined (to return 0) only in the main function. The rationale includes that checking if every code path returns a value is quite difficult, and a return value could be set with embedded assembler or other tricky methods. From C++11 draft: § 6.6.3/2 Flowing off the end of a function [...] results in undefined behavior in a value-returning function. § 3.6.1/5 If control reaches the end of main without encountering a return statement, the effect is that of executing return 0; Note that the behaviour described in C++ 6.6.3/2 is not the same in C. gcc will give you a warning if you call it wit
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
C++ Function Without Return Value
site About Us Learn more about Stack Overflow the company Business Learn more no return in function returning non-void eclipse about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x c++ int function without return Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up why is http://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no this warning ( no return , in function returning non-void)? up vote 0 down vote favorite void *rastertask() { struct sched_param sparm; memset(&sparm, 0, sizeof(sparm)); sparm.sched_priority = 10; /* 0 = lowest, 99 = highest */ sched_setscheduler( 0 /* pid, 0 ==> this process */, SCHED_RR /* policy */, &sparm); unsigned int n_loop; for(n_loop=0;;n_loop++) { struct timespec ts_start, ts_end; clock_gettime(CLOCK_REALTIME, &ts_start); TASK1(Task2ms_Raster); /* gets called http://stackoverflow.com/questions/23856895/why-is-this-warning-no-return-in-function-returning-non-void every 2ms */ if( (n_loop % 5) == 0) { TASK2(Task10ms_Raster); /* get called every 5 * 2ms = 10ms */ } if( (n_loop % 50) == 0) { TASK3(Task100ms_Raster); /* get called every 50 * 2ms = 100ms */ } if( (n_loop % 250) == 0 ) { /* reset loop counter when smallest common * multiple of timing grid has been reached */ n_loop = 0; } clock_gettime(CLOCK_REALTIME, &ts_end); useconds_t const tasks_execution_time = delta_t_us(&ts_start, &ts_end); if( tasks_execution_time >= MS_to_US(2) ) { /* report an error that tasks took longer than 2ms to execute */ } /* wait for 2ms - task_execution_time so that tasks get called in * a close 2ms timing grid */ else usleep( MS_to_US(2) - tasks_execution_time ); } } int main(int argc, char *argv[]) { pthread_t thread_id if (pthread_create(&thread_id, NULL, &rastertask, NULL)) { perror ("pthread_create"); exit (1); } return 0; } I am creating a thread in the main function. Created a scheduler in the function for calling the tasks for every 2milli seconds, 10 milli seconds and 100 milliseconds. I am getting a warning in the void *rastertask() as NO return, in function returning non-vo
a non-void function without a return statement left undefined in C & C++? What is the reason?I think there must be return statement exist if the function return type isn't void.Why following is calling convention & https://www.quora.com/Why-does-returning-from-a-non-void-function-without-a-return-statement-left-undefined-in-C-C++-What-is-the-reason architecture dependent?#include
highlights just how naive that can be, and started looking into strategies for preventing this problem and ones like it in the future. In C++ it is permissible to omit a return statement from a functions that have been declared with non-void return types. For example, in the following code, the function bar omits the return. This however, is getting into the realms of undefined behavior struct Foo { int a; int b; }; Foo bar() { // missing return } int main() { Foo f = bar(); return 0; } By default compiling that snippet of code using gcc: g++ -c snippet.cpp gives no warnings at all. Most of the time, I'm careful enough to at least compile with warnings enabled (-Wall) > g++ -Wall -c snippet.cpp snippet.cpp: In function ‘Foo bar()': snippet.cpp:8: warning: no return statement in function returning non-void which is at least a helpful warning message. However, at times it can be fairly easy to ignore warnings, and this is where I got bitten. The no return statement warning was hidden amongst warnings being generated by third party library code, and I (wrongly!) assumed that it wasn't important. I'd prefer that this didn't happen again in the future. GCC has a -Werror flag that will convert all warnings to errors, but in my case, I'd prefer fast development cycles to having perfectly clean and portable code (at least for prototyping). Turning to stackoverflow I found a method for converting the warning to an error, and in the process learned how to use gcc warning names and diagnostics. The final solution is to use -Werror=, and in this case -Werror=return-type. g++ -Werror=return-type -Wall -c snippet.cpp snippet.cpp: In function ‘Foo bar()': snippet.cpp:8: error: no return statement in function returning non-void The side-effect of doing this highlighted how important it will be in the future to consistently (but selectively) start to force myself to deal more rigorously with warnings. Again, stackoverflow to the rescue to identify the flags that are most likely to help. It's probably worth noting that under MSVC, this warning is automatically promoted to an error by default Posted by andrew at 22:24 Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest Labels: gcc compiler 2 comments: Fuzz2 September 2011 at 01:39I was going to say that I'm sure I remember a compiler complaining to me for this... but it was the MSVC compiler!Good advice to check all warnings though. I sometimes look at the warnings from libraries (such as the classics related to initialisations of char* that always pop up) and put in specific ig