IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Unreachable code ?


Sujet :

C

  1. #1
    Membre confirmé
    Inscrit en
    Avril 2005
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 156
    Par défaut Unreachable code ?
    Bonjour,

    Je développe un programme sous Linux avec Eclipse/GCC. Mon problème est que lorsque j'utilise l'option -Wunreachable-code, j'ai plusieurs warnings indiquant une ligne qui ne sera jamais exécutée alors qu'il n'y a pas de raison pour ce warning, comme dans le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    /**
     * Gives the hash table's node containing the value referenced by key.
     * @param t Address of the hash table. If NULL, nothing is done.
     * @param key Key from which the function will search the node. If NULL, nothing is done.
     * @param hashed_index If not NULL, will be fitted with the hash value.
     * @return Node containing the value referenced by key.
     */
    static struct node *lookup(const Hashtable *t, const char *key, unsigned long *hashed_index)
    {
     
    	struct node *current = NULL; /* Used to loop through the collision list, and store the address of the searched node */
     
    	if (t != NULL && key != NULL)
    	{
     
    		unsigned long hi; /* Hash index */
     
    		/* Computing the hash value and copying it in hashed_index if necessary */
    		hi = hash(t, (const unsigned char *)key);
    		if (hashed_index != NULL)
    			*hashed_index = hi; /* Warning : ce code ne sera jamais exécuté */
     
    		/* Getting the searched node */
    		for (current = t->table[hi]; current != NULL && strcmp(current->key, key) != 0; current = current->next)
    			;
     
    	}
     
    	return current;
     
    }
    Quel est le problème (en mode Debug avec les mêmes options, aucun warning) ?

    Une autre petite question à propos des options de compilations : quelle est la différence entre -ansi et -pedantic ? S'il n'y a aucun avertissement ni erreur, cela veut-il dire que le code est utilisable tel quel sur toutes les plate-fromes ?

  2. #2
    Membre confirmé
    Inscrit en
    Avril 2005
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 156
    Par défaut
    En fait les avertissements sont dus aux options d'optimisations (-O3). En passant à -O2 ils disparaîssent tous à l'exception des lignes contenant les fonctions ntoh? et hton?, qui n'acceptent aucune optimisation (-O0)...Y a-t-il un moyen de régler cela ou non ?

    Mon autre question sur -ansi et -pedantic tient toujours

  3. #3
    Membre Expert Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Par défaut
    -pedantic
    Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

    Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few will require -ansi or a -std option specifying the required version of ISO C). However, without this option, certain GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.

    -pedantic does not cause warning messages for use of the alternate keywords whose names begin and end with `__'. Pedantic warnings are also disabled in the expression that follows __extension__. However, only system header files should use these escape routes; application programs should avoid them. See Alternate Keywords.

    Some users try to use -pedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.

    A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -pedantic. We don't have plans to support such a feature in the near future.

    Where the standard specified with -std represents a GNU extended dialect of C, such as `gnu89' or `gnu99', there is a corresponding base standard, the version of ISO C on which the GNU extended dialect is based. Warnings from -pedantic are given where they are required by the base standard. (It would not make sense for such warnings to be given only for features not in the specified GNU C dialect, since by definition the GNU dialects of C include all features the compiler supports with the given option, and there would be nothing to warn about.)
    -ansi
    In C mode, this is equivalent to `-std=c89'. In C++ mode, it is equivalent to `-std=c++98'.

    This turns off certain features of GCC that are incompatible with ISO C90 (when compiling C code), or of standard C++ (when compiling C++ code), such as the asm and typeof keywords, and predefined macros such as unix and vax that identify the type of system you are using. It also enables the undesirable and rarely used ISO trigraph feature. For the C compiler, it disables recognition of C++ style `//' comments as well as the inline keyword.

    The alternate keywords __asm__, __extension__, __inline__ and __typeof__ continue to work despite -ansi. You would not want to use them in an ISO C program, of course, but it is useful to put them in header files that might be included in compilations done with -ansi. Alternate predefined macros such as __unix__ and __vax__ are also available, with or without -ansi.

    The -ansi option does not cause non-ISO programs to be rejected gratuitously. For that, -pedantic is required in addition to -ansi. See Warning Options.

    The macro __STRICT_ANSI__ is predefined when the -ansi option is used. Some header files may notice this macro and refrain from declaring certain functions or defining certain macros that the ISO standard doesn't call for; this is to avoid interfering with any programs that might use these names for other things.

    Functions that would normally be built in but do not have semantics defined by ISO C (such as alloca and ffs) are not built-in functions when -ansi is used. See Other built-in functions provided by GCC, for details of the functions affected.
    Pour la première question, il faut savoir que la compilation avec GCC doit idéalement être fait en 2 passes, une première en -O0 avec -Wunreachable-code et une seconde avec -O2 ou -O3 avec -Wuninitialized.

  4. #4
    Membre confirmé
    Inscrit en
    Avril 2005
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 156
    Par défaut
    Merci beaucoup !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. problème "unreachable code is detected" C#
    Par khalil.ajmi dans le forum C#
    Réponses: 1
    Dernier message: 22/10/2012, 15h45
  2. Code unreachable, pourquoi ?
    Par ram-0000 dans le forum C++
    Réponses: 19
    Dernier message: 17/02/2009, 18h02
  3. Unreachable code ?
    Par gpierre13 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 18/06/2008, 15h44
  4. unreachable code erreur
    Par nadsky dans le forum Général Java
    Réponses: 5
    Dernier message: 28/05/2008, 16h05
  5. Erreur "Unreachable code"
    Par DebuDev dans le forum Général Java
    Réponses: 1
    Dernier message: 23/02/2008, 03h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo