16

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 function length ()), for which I had to ask them a little effort to just "believe" it is an integer type, I think it would be great to show 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 wihch 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 another 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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

5 Answers5

26

You can try to expand standard include files with the C preprocessor (cpp) by hand and check the output of that:

$ echo '#include <stdlib.h>' | cpp -I/usr/include - > stdlib-expanded.c

You will find that the output of cpp even includes markers to indicate from which files the code in stdlib-expanded.c has been included.

Stefan Majewsky
  • 5,427
  • 2
  • 28
  • 51
  • 4
    +1 just `echo '#include ' | cpp -I/usr/include | grep size_t` gives `typedef long unsigned int size_t;` as first line – stijn Sep 19 '12 at 08:15
  • echo '#include ' | cpp -I/usr/include | grep size_t DID THE TRICK. THANKS! – user2548100 Jan 28 '14 at 22:33
  • @Stefan Majewsky , how do you do this (I mean, execute the same command) with gcc, instead of cpp ? – Nulik Aug 27 '16 at 20:46
  • @Nulik: Why would you do that? gcc is using cpp itself, as far as I know. – Stefan Majewsky Aug 29 '16 at 08:04
  • @StefanMajewsky nevermind, I found it, it is -E flag , for example : echo "#include " | gcc -E -" I don't work with C++ , I write on C only. – Nulik Aug 29 '16 at 14:33
7

gcc provides some of the headers and that is relevant here: size_t is defined in stddef.h which is one of those headers. Here it is for instance at /usr/lib/x86_64-redhat-linux/4.1.1/include/stddef.h. There the definition of size_t is

typedef __SIZE_TYPE__ size_t;

__SIZE_TYPE__ is a compiler predefined macro (which allows to ensure that the compiler and the header agree and, as what the compiler expect depend on its arguments -- for instance with -m32 it is an unsigned 32 bit bits, and with -m64 an unsigned 64 bits types --, to have the header independent of the compiler arguments).

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • Couldn't they have used `unsigned long` instead, which has the same behavior? [Looks like](https://github.com/gcc-mirror/gcc/blob/master/gcc/ginclude/stddef.h#L210) if the macro is not defined, it defaults to `unsigned long` anyway, so I don't quite understand the use for this macro. – Aykhan Hagverdili Jan 23 '23 at 08:48
  • @AyxanHaqverdili unsigned long is at least 32 bits. There are platforms where `size_t` is less wide, 16 bits for instance. Nowadays they have mostly an historical or niche interest. – AProgrammer Jan 26 '23 at 12:08
4

Just for completeness, have you considered simply asking C++ about size_t?

#include <iostream>
#include <cstddef>
#include <limits>

int main()
{
    std::cout << "sizeof(size_t) = " << sizeof(std::size_t) << std::endl;
    std::cout << "is size_t an integer? " <<
        (std::numeric_limits<std::size_t>::is_integer ? "yes" : "no")
        << std::endl;
    std::cout << "is size_t signed? " <<
        (std::numeric_limits<std::size_t>::is_signed ? "yes" : "no")
        << std::endl;
}

gives me

sizeof(size_t) = 8
is size_t an integer? yes
is size_t signed? no
Useless
  • 64,155
  • 6
  • 88
  • 132
3

As others wrote, you probably can find it if you search all the include files. However the fact that this is how most implementations work is not guaranteed.

The standard says that, for example, #include <stddef.h> should provide a definition for size_t which must be an unsigned integer type. But it also says that there's no need for a file called stddef.h to exist anywhere on the file system. And even an implementation that does provide such a file but that file only contains the following line

#pragma stdlib_stddef_h

would be perfectly conforming if the above pragma effectively provides what the standard prescribes for that header.

In other words, size_t is an unsigned integer type because that's what the standard says, not because that's what you can read in a header file.

Analog File
  • 5,280
  • 20
  • 23
0

std::size_t is defined in <cstddef>. See http://en.cppreference.com/w/cpp/header/cstddef.

R Sahu
  • 204,454
  • 14
  • 159
  • 270