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 :

utilisation de sizeof


Sujet :

C

  1. #1
    Membre confirmé
    Inscrit en
    Janvier 2005
    Messages
    100
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 100
    Par défaut utilisation de sizeof
    Bonjour

    je voudrais savoir quel est le cout d'execution de l'operatuer sizeof


    cad est-ec qu'il vaut mieux
    1) sauvegarder le resultat de sizeof applique a une structure ou un type et utiliser la variable plus tard
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     size_t size = sizeof(uin32_t);
    printf( "size uint32 = %d\n", size)
    int offset = size;
    ou 2) bien a
    2) appeler sizeof a chaque fois qu'on a besoin de la taille du type
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    printf( "size uint32 = %d\n", sizeof(uint32_t));
    int offset = sizeof(uint32_t);
    merci de commenter le choix d'utilisation de sizeof

    [/code]

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 241
    Par défaut
    Le mieu c'est de connaitre la taille, sizeof nécessitant un temps de calcul quel qu'il soit. Je te conseille de déffinir cette taille, par exemple avec un #define ou de déffinir une structure pour ton tableau, si tu souhaite connaitre la taille d'un tableau, comportant ton tableau et sa taille (length_tab), tu n'auras par la suite plus qu'à appeler l'élément length_tab pour en connaitre sa longueur.

  3. #3
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    euh... sizeof tout court ne nécessite aucun temps de calcul: à la compilation, c'est remplacé par une constante...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  4. #4
    Membre éprouvé
    Inscrit en
    Novembre 2002
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 120
    Par défaut
    Pas de temps de calcul à l'exécution (en tout cas en C, c'est pas le cas en C++) car sizeof(X) est par définition une constante évaluable à la compilation.

  5. #5
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut Re: utilisation de sizeof
    Citation Envoyé par Mokhtar BEN MESSAOUD
    je voudrais savoir quel est le cout d'execution de l'operatuer sizeof
    EDIT
    Si l'objet n'est pas un VLA ([C99] Variable Length Array), 0. Il génère une expression constante. (Pas de temps, pas de mémoire).
    cad est-ec qu'il vaut mieux
    1) sauvegarder le resultat de sizeof applique a une structure ou un type et utiliser la variable plus tard
    Non. Il faut mieux appeler 2 fois sizeof.

    Rappel, il ne fonctionne que sur les objets statiques [C99] et les VLA.

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 241
    Par défaut
    Tiens? T'as vu ça où?

  7. #7
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par petdelascar
    Le mieu c'est de connaitre la taille, sizeof nécessitant un temps de calcul quel qu'il soit.
    Non. Une fois à la compilation, c'est tout.

  8. #8
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par petdelascar
    Tiens? T'as vu ça où?
    Bouton [citer] please. Tu parles de quoi ? A qui ?

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 241
    Par défaut
    sizeof ne nécessite pas de temps de calcul après compile
    Où puis je lire cette info?

  10. #10
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par petdelascar
    sizeof ne nécessite pas de temps de calcul après compile
    Où puis je lire cette info?
    La confiance règne...

    Dans la norme

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/

    Citation Envoyé par ISO/IEC 9899:1999 (E) ©ISO/IEC
    6.5.3.4 The sizeof operator
    Constraints
    1 The sizeof operator shall not be applied to an expression that has function type or an
    incomplete type, to the parenthesized name of such a type, or to an expression that
    designates a bit-field member.
    Semantics
    2 The sizeof operator yields the size (in bytes) of its operand, which may be an
    expression or the parenthesized name of a type. The size is determined from the type of
    the operand. The result is an integer. If the type of the operand is a variable length array
    type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
    integer constant.

    3 When applied to an operand that has type char, unsigned char, or signed char,
    (or a qualified version thereof) the result is 1. When applied to an operand that has array
    type, the result is the total number of bytes in the array.84) When applied to an operand
    that has structure or union type, the result is the total number of bytes in such an object,
    including internal and trailing padding.
    4 The value of the result is implementation-defined, and its type (an unsigned integer type)
    is size_t, defined in <stddef.h> (and other headers).
    5 EXAMPLE 1 A principal use of the sizeof operator is in communication with routines such as storage
    allocators and I/O systems. A storage-allocation function might accept a size (in bytes) of an object to
    allocate and return a pointer to void. For example:
    extern void *alloc(size_t);
    double *dp = alloc(sizeof *dp);
    The implementation of the alloc function should ensure that its return value is aligned suitably for
    conversion to a pointer to double.
    6 EXAMPLE 2 Another use of the sizeof operator is to compute the number of elements in an array:
    sizeof array / sizeof array[0]
    7 EXAMPLE 3 In this example, the size of a variable-length array is computed and returned from a
    function:
    #include <stddef.h>
    size_t fsize3(int n)
    {
    char b[n+3]; // variable length array
    return sizeof b; // execution time sizeof
    }
    84) When applied to a parameter declared to have array or function type, the sizeof operator yields the
    size of the adjusted (pointer) type (see 6.9.1).
    80 Language §6.5.3.4
    ©ISO/IEC ISO/IEC 9899:1999 (E)
    int main()
    {
    size_t size;
    size = fsize3(10); // fsize3 returns 13
    return 0;
    }
    Forward references: common definitions <stddef.h> (7.17), declarations (6.7),
    structure and union specifiers (6.7.2.1), type names (6.7.6), array declarators (6.7.5.2).
    Pour être plus précis, sizeof sur un VLA est évalué dynamiquement (ca parait logique), sinon, c'est bien une expresson constante.

    Je corrige dans ce sens.

  11. #11
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 241
    Par défaut
    Je préfers être sur!

  12. #12
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par petdelascar
    Je préfers être sur!
    Nous sommes sûrs et la communauté des programmeurs C aussi !

    Les gens d'IBM qui ne passent pas pour être des imbéciles sont d'accord...

    http://publib.boulder.ibm.com/infocenter/macxhelp/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05szofe.htm
    sizeof Operator

    The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type. A sizeof expression has the form:

    >>-sizeof--+-expr------------+---------------------------------><
    '-(--type-name--)-'



    The result for either kind of operand is not an lvalue, but a constant integer value. The type of the result is the unsigned integral type size_t defined in the header file stddef.h.
    Il est cassé ton bouton [Citer] ?

  13. #13
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 241
    Par défaut
    Pas besoind de citer quand on répond juste après non?
    Je savai pas que tu bossais chez ibm.

  14. #14
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par petdelascar
    Pas besoind de citer quand on répond juste après non?
    Il n'y a pas de 'juste après'. Le forum est un système client/serveur. Plusieurs clients peuvent écrire en même temps, l'ordre d'arrivee n'est pas prévisible.
    Je savai pas que tu bossais chez ibm.
    Qui a dit ça ? Je bosse ici http://www.aeta.com Rien à voir !

Discussions similaires

  1. comment utiliser sizeof( )
    Par ikuzar dans le forum Débuter
    Réponses: 3
    Dernier message: 02/05/2011, 20h21
  2. [VS.net2003] utilisation de sizeof
    Par stgi02 dans le forum Visual Studio
    Réponses: 9
    Dernier message: 18/04/2006, 12h04
  3. utilisation du meta type ANY
    Par Anonymous dans le forum CORBA
    Réponses: 1
    Dernier message: 15/04/2002, 12h36
  4. [BCB5] Utilisation des Ressources (.res)
    Par Vince78 dans le forum C++Builder
    Réponses: 2
    Dernier message: 04/04/2002, 16h01
  5. Réponses: 2
    Dernier message: 20/03/2002, 23h01

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