Some insight about c++ source in a linux system

Hi all,
I'm teaching C++. Now I'm talking about new type definition by a programmer using typedef keyword. As long as my pupils are used to the type size_t (for example by using funciton length ()), for which I had to ask them a little effort to just "believe" it is an integer type, I think it woud be great to show noew them where this type is defined.

So, I've done a lot grep's in /usr/include in an ubuntu box, and what I see is that size_t is, in turn, a redefinition of size_type wich in turn is a redefinition of metadata_type, and that's the end in this directory. Not found the final "typedef unsigned int metadata_type;".

In /usr/src I've found anohter previous type called yy_size_t,...

But, in any case I've been unable the get to the end of the chain.

Does anyone know where to find out the final definition to check out that it is an unsigned int (or the like)?
May be I miss a development package in my box? In this case, why I'm able to compile programs using size_t type?

Thanks a lot
Fernando
std::size_t is defined in <cstddef>, as required by C++ standard.

*How* it is defined, depends on the compiler:

Here, I'll look at a few:

gcc: <cstddef> has the line "using ::size_t;" which pulls in C's size_t, defined in <stddef.h>
gcc's stddef.h does typedef __SIZE_TYPE__ size_t;, where __SIZE_TYPE__ is a macro constant defined with #define __SIZE_TYPE__ long unsigned int

xlc: <cstddef> also has the line using ::size_t;, and <stddef.h> simply has typedef unsigned long size_t;

acc: <cstddef> also has the line using ::size_t;, and <stddef.h> has #include <sys/_size_t.h> , which says typedef unsigned long size_t;

etc
Hey, thanks both.

Humm, I don't know if explaining this in my classroom. It's an primary level and I guess I'm going to be forced to explain too much things, with the result opposite to intended.

For instance, someone could ask if, in the case of gcc, it's needed the macro __SIZE_TYPE__. (or, based on my experience as a developer, it's just something needed at certain moment in time, and later on it happens to be more painful changing it back than to keep it as is).

Anyway it's me who has learned a bit more about the setting up of this "mess" ;)

Thanks again.
Fernando
Well, you don't really need to go digging through the headers to satisfy yourself it's an integral type.

The standard says:

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <limits>
using namespace std;

int main () {
  cout << boolalpha;
  cout << "size_t is integral: " << numeric_limits<size_t>::is_integer << endl ;
  cout << "Minimum value for size_t: " << numeric_limits<size_t>::min() << endl;
  cout << "Maximum value for size_t: " << numeric_limits<size_t>::max() << endl;
  cout << "size_t is signed: " << numeric_limits<size_t>::is_signed << endl;
  cout << "Non-sign bits in size_t: " << numeric_limits<size_t>::digits << endl;
}
size_t is integral: true
Minimum value for size_t: 0
Maximum value for size_t: 4294967295
size_t is signed: false
Non-sign bits in size_t: 32



Now I'm talking about new type definition by a programmer using typedef keyword.


You don't really create new types with typedef. You alias already existing types.

Topic archived. No new replies allowed.