warning #2154: Unreachable code -> This is a problem

Started by Elpower, May 10, 2012, 06:00:00 PM

Previous topic - Next topic

Elpower


The compiler warning than code can't be reachable
count = count_reg[0] = fgetc(pfile); //Quantidade de postos
while ((count--)>0)
{
postos = (struct Lista *)malloc(sizeof(struct Lista)); //Aloca a estrutura na memoria
ZeroMemory(postos, sizeof(struct Lista)); //Zera a memoria para evitar erros
postos->prev = (struct Lista*)temp;
if(temp)
((struct Lista*)temp)->next = postos;
fread_int(postos->id); //Id do posto
tempSize_str = fgetc(pfile); //Tamanho do Nome
postos->nome = malloc(tempSize_str);
fgets(postos->nome, tempSize_str, pfile); //Nome do posto
temp = (int)postos;
}


I don't know if "while ((count--)>0)" is the correct away to this loop but i try chance this to "while (count)" and the warning still appear
Someby know who to ignore this warning?

Elpower

#pragma warn(disable : 2154) //Unreachable code.
This resolve my problem for now... :)

frankie

Check if 'count' is declared as 'unsigned'. In this case the compiler assumes that the varible cannot be <0.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Elpower


Stefan Pendl

I don't think the posted code is the problem, but the problem is some lines above it.
---
Stefan

Proud member of the UltraDefrag Development Team

Elpower

int LoadDB(void)
{
FILE *pfile;
int tempSize_str;
int count;
int temp;

#define fread_int(x) {fread(&x , 4 , 1, pfile); return x;}

//Zera os contadores
ZeroMemory(count_reg, 4);

//Abre o ficheiro em modo de leitura
if ((pfile = fopen("pfm_db.db", "rb")) == NULL)
return 0;

//Se fim do ficheiro termina a leitura
if (feof(pfile))
goto end;

//Leitura do nome do batalhão
ZeroMemory(batalhao, 255);
tempSize_str = fgetc(pfile); //Tamanho do nome do batalhao
fgets(batalhao, tempSize_str, pfile); //Nome do batalhao

if (feof(pfile))
goto end;

//Leitura do nome do companhia
ZeroMemory(companhia, 255);
tempSize_str = fgetc(pfile); //Tamanho do nome do companhia
fgets(companhia, tempSize_str, pfile); //Nome do companhia

if (feof(pfile))
goto end;

//Leitura dos postos
temp = 0;
count = count_reg[0] = fgetc(pfile); //Quantidade de postos
while ((count--)>0)
{
postos = (struct Lista *)malloc(sizeof(struct Lista)); //Aloca a estrutura na memoria
ZeroMemory(postos, sizeof(struct Lista)); //Zera a memoria para evitar erros
postos->prev = (struct Lista*)temp;
if(temp)
((struct Lista*)temp)->next = postos;
fread_int(postos->id); //Id do posto
tempSize_str = fgetc(pfile); //Tamanho do Nome
postos->nome = malloc(tempSize_str);
fgets(postos->nome, tempSize_str, pfile); //Nome do posto
temp = (int)postos;
}
if (feof(pfile))
goto end;

there is all code before

iZzz32

Quote from: pocc -E file.c   while ((count--)>0)
   {
      postos = (struct Lista *)malloc(sizeof(struct Lista));
      ZeroMemory(postos, sizeof(struct Lista));
      postos->prev = (struct Lista*)temp;
      if(temp)
         ((struct Lista*)temp)->next = postos;
{fread(& postos->id , 4 , 1, pfile); return postos->id;};
      tempSize_str = fgetc(pfile);
      postos->nome = malloc(tempSize_str);
      fgets(postos->nome, tempSize_str, pfile);
      temp = (int)postos;
// Gray code is unreachable because of the return above.
   }
PS: never disable compiler warnings unless you really know what are you doing ;)

Stefan Pendl

#7
#define fread_int(x) {fread(&x , 4 , 1, pfile); return x;}
The RETURN has nothing to do inside this macro.

#define fread_int(x) (fread(&x , 4 , 1, pfile))
This should be sufficient.
---
Stefan

Proud member of the UltraDefrag Development Team

AlexN

Quote from: Elpower on May 10, 2012, 08:16:59 PM
#define fread_int(x) {fread(&x , 4 , 1, pfile); return x;}
.
.
.
   fread_int(postos->id);   //Id do posto
With this return you will leave the function (LoadDB()) and all the code behind this called macro is unreachable.
So I think this warning is good, because I don't think that you want this reaction and to disable it is a not so good idea. ;)
best regards
Alex ;)

CommonTater

From another perspective... Why bother with a macro (or function) if you're only calling it in one place? 


Elpower

#10
Sorry, I did not notice the "return x;" anyway I have not tested the code
I put the wrong return in the macro.
CommonTater -> i use this macro along the function. I had not posted any full function.
Thanks for the help.

AlexN

Quote from: Elpower on May 11, 2012, 12:11:29 PM
Sorry, I did not notice the "return x;" anyway I have not tested the code
I put the wrong return in the macro.
CommonTater -> i use this macro along the function. I had not posted any full function.
Thanks for the help.
I think a good way to handle such a function, is to create it as inline function. I think the compiler produce the same (or similar) code and you have less troubles. ;)
best regards
Alex ;)