size_t

Discussion in 'C++' started by josh, Jul 2, 2007.

  1. josh

    josh Guest

    Hi,

    I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    it...

    I'm using gcc 4.1.2 on Linux

    Thanks
    josh, Jul 2, 2007
    #1
    1. Advertising

  2. On Jul 2, 1:42 pm, josh <> wrote:
    > Hi,
    >
    > I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    > it...
    >


    IIRR, size_t is not a macro but a typedef. The exact type is not
    specified by the standard. One of the many usages of size_t is to
    denote the type of value returned by sizeof().

    With GCC , you might find find something similar to typedef
    __SIZE_TYPE__ size_t, but thats compiler dependent.

    -Neelesh
    Neelesh Bodas, Jul 2, 2007
    #2
    1. Advertising

  3. On 2007-07-02 10:42, josh wrote:
    > Hi,
    >
    > I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    > it...


    Why, just include <stddef.h> or <cstddef> and you have have it. Remember
    that the size of size_t can vary between different hardware so typing
    code that depends on size_t being of a specific size is stupid.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 2, 2007
    #3
  4. josh

    josh Guest

    On 2 Lug, 11:22, Neelesh Bodas <> wrote:
    > On Jul 2, 1:42 pm, josh <> wrote:
    >
    > > Hi,

    >
    > > I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    > > it...

    >
    > IIRR, size_t is not a macro but a typedef. The exact type is not
    > specified by the standard. One of the many usages of size_t is to
    > denote the type of value returned by sizeof().
    >
    > With GCC , you might find find something similar to typedef
    > __SIZE_TYPE__ size_t, but thats compiler dependent.
    >
    > -Neelesh


    Yes but I have been confused by this:

    in stddef.h

    #ifndef __SIZE_TYPE__
    #define __SIZE_TYPE__ long unsigned int
    #endif
    #if !(defined (__GNUG__) && defined (size_t))
    typedef __SIZE_TYPE__ size_t;

    now why is **defined** __SIZE_TYPE__ and not simple
    typedef long unsigned int __SIZE_TYPE__

    and when is written >> defined (size_t)) where is size_t before type-
    defined?

    and also how is used typedef???
    when compiler meets >> typedef __SIZE_TYPE__ size_t;
    it should convert __SIZE_TYPE__ in long unsigned int accordingly with
    previous #define __SIZE_TYPE__ long unsigned int but typedef don't
    want
    like its first operand the type synonymous and like second operand the
    real type??

    Oh, I'm very confused!!
    josh, Jul 2, 2007
    #4
  5. On Jul 2, 2:42 pm, josh <> wrote:

    > Yes but I have been confused by this:
    >
    > in stddef.h
    >
    > #ifndef __SIZE_TYPE__
    > #define __SIZE_TYPE__ long unsigned int
    > #endif
    > #if !(defined (__GNUG__) && defined (size_t))
    > typedef __SIZE_TYPE__ size_t;
    >
    > now why is **defined** __SIZE_TYPE__ and not simple
    > typedef long unsigned int __SIZE_TYPE__



    this is probably very specific to the GCC handles C++ libraries. That
    has nothing to do with C++ standard.

    >
    > and when is written >> defined (size_t)) where is size_t before type-
    > defined?
    >


    Specific to GCC.

    > and also how is used typedef???
    > when compiler meets >> typedef __SIZE_TYPE__ size_t;
    > it should convert __SIZE_TYPE__ in long unsigned int accordingly with
    > previous #define __SIZE_TYPE__ long unsigned int but typedef don't
    > want
    > like its first operand the type synonymous and like second operand the
    > real type??
    >


    syntax for typedef is : typedef old_type new_type;
    more on typedef here:
    > Oh, I'm very confused!!



    Note that if you are digging up in the headers just for the sake of
    getting an understanding specific to GCC, then its fine. But if you
    plan to use this information while writing (portable) C++ code, you
    will end up in trouble.

    -Neelesh
    Neelesh Bodas, Jul 2, 2007
    #5
  6. josh

    James Kanze Guest

    On Jul 2, 11:42 am, josh <> wrote:
    > On 2 Lug, 11:22, Neelesh Bodas <> wrote:


    > > On Jul 2, 1:42 pm, josh <> wrote:


    > > > I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    > > > it...


    > > IIRR, size_t is not a macro but a typedef. The exact type is not
    > > specified by the standard.


    But is required to be an unsigned integral type. (According to
    the current C++ standard, it must be a standard unsigned
    integral type; i.e. one of "unsigned char", "unsigned short",
    "unsigned int" or "unsigned long". C99 and I believe the next
    version of the C++ standard will allow an extended integral
    types as well, e.g. uint64_t, where even long is only 32 bits.)

    > > One of the many usages of size_t is to
    > > denote the type of value returned by sizeof().


    > > With GCC , you might find find something similar to typedef
    > > __SIZE_TYPE__ size_t, but thats compiler dependent.


    > Yes but I have been confused by this:


    By snooping where you have no business snooping:).

    > in stddef.h


    > #ifndef __SIZE_TYPE__
    > #define __SIZE_TYPE__ long unsigned int
    > #endif
    > #if !(defined (__GNUG__) && defined (size_t))
    > typedef __SIZE_TYPE__ size_t;


    > now why is **defined** __SIZE_TYPE__ and not simple
    > typedef long unsigned int __SIZE_TYPE__


    Because that's the way the authors of this particular library
    wanted to do it. (I suspect that it has something to do with
    the way they handle system dependencies.) At any rate, anything
    with a double underscore is of no concern to you, unless the
    compiler documentation explicitly says otherwise. (Some---I
    think even many---compilers use macros with double underscores
    to turn features on or off. That's obviously not the case
    here.)

    > and when is written >> defined (size_t)) where is size_t before type-
    > defined?


    Again, it's something internal to the implementation.
    "defined( size_t )" will only evaluate to true if size_t is
    already defined as a macro. Which isn't allowed: the
    implementation isn't allowed to define size_t as a macro, and if
    you define it as one, it is undefined behavior. Apparently,
    your library "defines" the undefined behavior, as an extension.
    I can't imagine a case where it would be useful,
    however---perhaps when compiling some special kernel code?
    Don't do it, unless you really, really know what you are doing.

    > and also how is used typedef???


    Typedef defines a symbol which is the name of the type. You
    write the declaration exactly like any other declaration, but
    the name being defined is not a variable, but the name of a
    type, which can be used as the name of a type whenever it is in
    scope. E.g.:

    int array[ 10 ] ; // defines an array of 10 ints,
    // a variable.

    typedef int Array10[ 10 ] ; // defines a symbol Array10,
    // which can be used as the
    // name of a type.
    Array10 array2 ; // defines an array of 10 ints,
    // a variable.


    > when compiler meets >> typedef __SIZE_TYPE__ size_t;
    > it should convert __SIZE_TYPE__ in long unsigned int accordingly with
    > previous #define __SIZE_TYPE__ long unsigned int but typedef don't
    > want
    > like its first operand the type synonymous and like second operand the
    > real type??


    I'm having difficulty parsing that last phrase. After macro
    expansion, yes, you have:

    typedef long unsigned int size_t ;

    If size_t is already defined in the scope, it is an error.
    Otherwise, it defines a new symbol, size_t, as a synonym for the
    type long unsigned ing.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 3, 2007
    #6
  7. josh

    josh Guest

    On 3 Lug, 09:43, James Kanze <> wrote:
    > On Jul 2, 11:42 am, josh <> wrote:
    >
    > > On 2 Lug, 11:22, Neelesh Bodas <> wrote:
    > > > On Jul 2, 1:42 pm, josh <> wrote:
    > > > > I want to find the #DEFINE of size_t but in <stddef.h> I don't see
    > > > > it...
    > > > IIRR, size_t is not a macro but a typedef. The exact type is not
    > > > specified by the standard.

    >
    > But is required to be an unsigned integral type. (According to
    > the current C++ standard, it must be a standard unsigned
    > integral type; i.e. one of "unsigned char", "unsigned short",
    > "unsigned int" or "unsigned long". C99 and I believe the next
    > version of the C++ standard will allow an extended integral
    > types as well, e.g. uint64_t, where even long is only 32 bits.)
    >
    > > > One of the many usages of size_t is to
    > > > denote the type of value returned by sizeof().
    > > > With GCC , you might find find something similar to typedef
    > > > __SIZE_TYPE__ size_t, but thats compiler dependent.

    > > Yes but I have been confused by this:

    >
    > By snooping where you have no business snooping:).
    >
    > > in stddef.h
    > > #ifndef __SIZE_TYPE__
    > > #define __SIZE_TYPE__ long unsigned int
    > > #endif
    > > #if !(defined (__GNUG__) && defined (size_t))
    > > typedef __SIZE_TYPE__ size_t;
    > > now why is **defined** __SIZE_TYPE__ and not simple
    > > typedef long unsigned int __SIZE_TYPE__

    >
    > Because that's the way the authors of this particular library
    > wanted to do it. (I suspect that it has something to do with
    > the way they handle system dependencies.) At any rate, anything
    > with a double underscore is of no concern to you, unless the
    > compiler documentation explicitly says otherwise. (Some---I
    > think even many---compilers use macros with double underscores
    > to turn features on or off. That's obviously not the case
    > here.)


    so that #define __SIZE_TYPE__ long unsigned int is out of standard or
    normal use
    and it's a mystery why this contextual use!
    however if I try to:

    #define ANY unsigned int
    typedef ANY any_any;

    I have the following error:
    declaration does not declare anything

    I'm using gcc 4 and why here the compiler give me an error and
    don't give me the same error when including that header???

    Regards
    josh, Jul 3, 2007
    #7
  8. josh

    James Kanze Guest

    On Jul 3, 11:11 am, josh <> wrote:
    > On 3 Lug, 09:43, James Kanze <> wrote:


    [...]
    > > Because that's the way the authors of this particular library
    > > wanted to do it. (I suspect that it has something to do with
    > > the way they handle system dependencies.) At any rate, anything
    > > with a double underscore is of no concern to you, unless the
    > > compiler documentation explicitly says otherwise. (Some---I
    > > think even many---compilers use macros with double underscores
    > > to turn features on or off. That's obviously not the case
    > > here.)


    > so that #define __SIZE_TYPE__ long unsigned int is out of
    > standard or normal use and it's a mystery why this contextual
    > use!


    Quite.

    > however if I try to:


    > #define ANY unsigned int
    > typedef ANY any_any;


    > I have the following error:
    > declaration does not declare anything


    Really. I don't. It's perfectly legal according to the
    standard, and works with the compilers I have on this machine
    (Sun CC and g++).

    > I'm using gcc 4 and why here the compiler give me an error and
    > don't give me the same error when including that header???


    You've mistyped something. The above works with g++ 4.1.0. (I
    get the "declaration does not declare anything" error if I put
    a ';' between the unsigned and the int in the #define. But
    that's to be expected.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 3, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard Hinnant

    Re: size_t ... standards

    Howard Hinnant, Jun 29, 2003, in forum: C++
    Replies:
    5
    Views:
    3,554
    Jim Fischer
    Jun 30, 2003
  2. Howard Hinnant

    Re: size_t ... standards

    Howard Hinnant, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    832
    Howard Hinnant
    Jun 29, 2003
  3. Chris \( Val \)

    Re: for(size_t a=begin();a!=end();++a){}

    Chris \( Val \), Jul 13, 2003, in forum: C++
    Replies:
    2
    Views:
    348
    John Harrison
    Jul 14, 2003
  4. Alex Vinokur
    Replies:
    9
    Views:
    773
    James Kanze
    Oct 13, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    565
Loading...

Share This Page