Solved

"not all control paths return a value"

Posted on 2001-08-09
3
517 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
ID: 6367612
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
ID: 6367662
>>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
ID: 6367668
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

789 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