NO

Author Topic: warning #2154: Unreachable code -> This is a problem  (Read 13848 times)

Elpower

  • Guest
warning #2154: Unreachable code -> This is a problem
« on: May 10, 2012, 06:00:00 PM »

The compiler warning than code can't be reachable
Code: [Select]
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

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #1 on: May 10, 2012, 06:11:40 PM »
Code: [Select]
#pragma warn(disable : 2154) //Unreachable code.This resolve my problem for now... :)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: warning #2154: Unreachable code -> This is a problem
« Reply #2 on: May 10, 2012, 07:07:38 PM »
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

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #3 on: May 10, 2012, 07:23:17 PM »
count is declared as int :"int count;"

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: warning #2154: Unreachable code -> This is a problem
« Reply #4 on: May 10, 2012, 08:01:01 PM »
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

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #5 on: May 10, 2012, 08:16:59 PM »
Code: [Select]
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

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #6 on: May 10, 2012, 08:22:32 PM »
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 ;)
« Last Edit: May 10, 2012, 08:25:17 PM by iZzz32 »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: warning #2154: Unreachable code -> This is a problem
« Reply #7 on: May 10, 2012, 08:38:24 PM »
Code: [Select]
#define fread_int(x) {fread(&x , 4 , 1, pfile); return x;}The RETURN has nothing to do inside this macro.

Code: [Select]
#define fread_int(x) (fread(&x , 4 , 1, pfile))This should be sufficient.
« Last Edit: May 10, 2012, 10:44:01 PM by Stefan Pendl »
---
Stefan

Proud member of the UltraDefrag Development Team

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: warning #2154: Unreachable code -> This is a problem
« Reply #8 on: May 11, 2012, 08:25:17 AM »
#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

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #9 on: May 11, 2012, 08:57:15 AM »
From another perspective... Why bother with a macro (or function) if you're only calling it in one place? 


Elpower

  • Guest
Re: warning #2154: Unreachable code -> This is a problem
« Reply #10 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.
« Last Edit: May 11, 2012, 12:13:15 PM by Elpower »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: warning #2154: Unreachable code -> This is a problem
« Reply #11 on: May 11, 2012, 12:48:53 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 ;)