Question about size_t

Documentation states that size_t is defined in header <cstddef>. However, the following program runs using size_t even though I've commented out the include. My question is: how can this program compile and run with size_t as a type if the header that defines size_t is not included? The program declares two built-in arrays, fills the first array's elements with values equal to its index, displays them, then copies the values to a second array and displays them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
//#include <cstddef>

using std::cout; using std::endl;

int main()
{
   const size_t size = 10;
   int arr1[size];
   int arr2[size];
   int arrayInitializer = 0;

   for (int &i : arr1) {
      i = arrayInitializer;
      ++arrayInitializer;
   }

   // Display contents of arr1.
   cout << "arr1 contents: ";
   for (int i : arr1)
      cout << i << " ";
   cout << endl << endl;

   // Copy values of arr1 into arr2.
   if (sizeof arr1 / sizeof (int) == sizeof arr2 / sizeof (int))
      for (int i = 0; i < size; ++i)
         arr2[i] = arr1[i];

   // Display contents of arr2.
   cout << "arr2 contents: ";
   for (int i : arr2)
      cout << i << " ";
   cout << endl << endl;

   return 0;
}
To my knowledge, there's no rule to prevent library headers from including each other.

In your case, the iostream header directly or indirectly includes the cstddef header, or whatever "internal" header the library implementers wrote to define std::size_t in.

So this code compiles for you, but remember that it is not portable: other people using other compilers may get an error because cstddef was not included. Always explicitly include the headers from which you use something.
To my knowledge, there's no rule to prevent library headers from including each other.

In your case, the iostream header directly or indirectly includes the cstddef header, or whatever "internal" header the library implementers wrote to define std::size_t in.


I'm using Visual Studio 2013, and this program was created as an empty project. Before posting the question I checked the includes of <iostream>, and found that the result of including <iostream> causes a chain of <istream>, <ostream>, and <ios> includes, but not <cstddef> as far as I can tell. So there must be some 'internal' hidden includes as you say (or maybe microsoft's own version of a global size_t?). Yes, I agree with your comment about the portability.

edit: following up on the 'global size_t' idea, notice that I didn't have to declare size_t as being in the std namespace...just a plain size_t instead of a std::size_t
hmm...
Last edited on
Regarding the size_t type in my program...it's not the one from cstddef...it's microsoft's version, as stated in this page:

http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k%28crtdefs%2Fsize_t%29;k%28size_t%29;k%28DevLang-C%2B%2B%29;k%28TargetOS-Windows%29&rd=true

I checked this on Visual Studio 2013 Express:

iostream
-> istream
-> ostream
-> ios
-> xlocnum (uses size_t)
-> cstdio
-> stdio.h (a-ha! this would explain the global size_t)
-> crtdefs.h (the actual typedefs of size_t are located here)
Last edited on
Documentation states that size_t is defined in header <cstddef>.

Yes. It also states in the C standard that size_t is defined in multiple headers (other than <stddef.h>) Two of those are stdlib.h and stdio.h, which, in the case of VC++, are included indirectly by <iostream>.


Regarding the size_t type in my program...it's not the one from cstddef...it's microsoft's version, as stated in this page:


size_t is size_t.
So xlocnum gets size_t into it! I checked the includes as far as <ios> and said...nah xlocnum won't get it...silly me! So there's no difference between size_t and std::size_t?
So there's no difference between size_t and std::size_t?

Well one of them is global, and the other one is in the std namespace.
http://stackoverflow.com/questions/237370/does-stdsize-t-make-sense-in-c

In the end you should use std::size_t if including cstddef.
http://en.cppreference.com/w/cpp/types/size_t
In the end you should use std::size_t if including cstddef.
http://en.cppreference.com/w/cpp/types/size_t


A new site to bookmark. Thanks!
Topic archived. No new replies allowed.