Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > size_t

Reply
Thread Tools

size_t

 
 
josh
Guest
Posts: n/a
 
      07-02-2007
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

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-02-2007
On Jul 2, 1:42 pm, josh <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-02-2007
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
 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      07-02-2007
On 2 Lug, 11:22, Neelesh Bodas <(E-Mail Removed)> wrote:
> On Jul 2, 1:42 pm, josh <(E-Mail Removed)> 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!!




















 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-02-2007
On Jul 2, 2:42 pm, josh <(E-Mail Removed)> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-03-2007
On Jul 2, 11:42 am, josh <(E-Mail Removed)> wrote:
> On 2 Lug, 11:22, Neelesh Bodas <(E-Mail Removed)> wrote:


> > On Jul 2, 1:42 pm, josh <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      07-03-2007
On 3 Lug, 09:43, James Kanze <(E-Mail Removed)> wrote:
> On Jul 2, 11:42 am, josh <(E-Mail Removed)> wrote:
>
> > On 2 Lug, 11:22, Neelesh Bodas <(E-Mail Removed)> wrote:
> > > On Jul 2, 1:42 pm, josh <(E-Mail Removed)> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-03-2007
On Jul 3, 11:11 am, josh <(E-Mail Removed)> wrote:
> On 3 Lug, 09:43, James Kanze <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Re: for(size_t a=begin();a!=end();++a){} Chris \( Val \) C++ 2 07-14-2003 06:31 AM
Re: size_t ... standards Howard Hinnant C++ 5 06-30-2003 07:22 PM
Re: size_t ... standards Howard Hinnant C++ 0 06-29-2003 05:45 PM



Advertisments