Solved

"not all control paths return a value"

Posted on 2001-08-09
3
498 Views
Last Modified: 2006-11-17
Hi guys,

I'm fond of writing loops with a return statement in the body, usually when there need not be any other return statement...for example,

int someFunction()
{
for(int i=0; i<arraySize; i++)
    if(array[i] == TRUE)
         return i;
}

You usually get a compiler warning for this sort of thing. I typically argue that I'm well aware that not all paths return a value because, say, I know that there will be at least one array[i] that will be true (or something like that)

My question is, is this a bad coding practise? It is unabasahed laziness on my part, that's for sure, but how harmful is it?

Thanks

Senkwe
0
Comment
Question by:chandas
3 Comments
 

Expert Comment

by:vfinn
Comment Utility
It's not really bad but it's probably best to give a default return at the bottom
This gets rid of the warning and is safer in case of unexpected behaviour (e.g. arraySize being a strange value, etc..)

The alternative is to have a variable iReturnValue which you set in the for loop and then always return that, but that means you have to use a break in the for loop or you lose speed which is probably worse
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
Comment Utility
>>is this a bad coding practise?
No, I don't think so. I saw many codes and wrote many
times such code, BUT remember: you must free all
memory before return. Next code will be memory leek:

char *sz = new char[255];
...
for(int i=0; i<arraySize; i++)
   if(array[i] == TRUE)
        return i;
 delete [] sz;

return -1;
..
If you want get rid of worning, set in last line
return -1; as im my example.
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
You should strive to produce warning free code, the way to handle this situation is to use an assertion (of some sort):

int someFunction()
{
   for(int i=0; i<arraySize; i++)
      if(array[i] == TRUE)
          return i;

   assert(0); // Should Never Reach this point
   return 0;  // Dummy Return
}

this makes your assumption explicit, you are not leaving it to other programmers to decipher your assumption.

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now