CsizeofBasicTypes

From qtnode

Jump to: navigation, search

This code will allow you to determine the number of bytes that are allocated for the various types used in your C++ programming.

#include <stdint.h>
#include <cstdlib>
#include <iostream>
#include <fstream>

using std::ofstream;
using std::endl;
int main(int argc, char *argv[])
{
  ofstream fout;
  fout.open("size_output.txt");
  fout << "char =        " << sizeof( char        ) << endl;
  fout << "short =       " << sizeof( short       ) << endl;
  fout << "int =         " << sizeof( int         ) << endl;
  fout << "long =        " << sizeof( long        ) << endl;
  fout << "long long =   " << sizeof( long long   ) << endl;
  fout << "float =       " << sizeof( float       ) << endl;
  fout << "double =      " << sizeof( double      ) << endl;
  fout << "long double = " << sizeof( long double ) << endl;
  fout << "int8_t =      " << sizeof( int8_t      ) << endl;
  fout << "int16_t =     " << sizeof( int16_t     ) << endl;
  fout << "int32_t =     " << sizeof( int32_t     ) << endl;
  fout << "int64_t =     " << sizeof( int64_t     ) << endl;
  fout << "uint8_t =     " << sizeof( uint8_t     ) << endl;
  fout << "uint16_t =    " << sizeof( uint16_t    ) << endl;
  fout << "uint32_t =    " << sizeof( uint32_t    ) << endl;
  fout << "uint64_t =    " << sizeof( uint64_t    ) << endl;
  fout << "size_t =      " << sizeof( size_t      ) << endl;
  fout << "void* =       " << sizeof( void*       ) << endl;
  fout << "intptr_t =    " << sizeof( intptr_t    ) << endl;
  fout.close();
  return 0;
}

Here is the size_output.txt file on a Xeon Irwindale system running Windows XP Pro, using MinGW 3.4.2:

char =        1
short =       2
int =         4
long =        4
long long =   8
float =       4
double =      8
long double = 12
int8_t =      1
int16_t =     2
int32_t =     4
int64_t =     8
uint8_t =     1
uint16_t =    2
uint32_t =    4
uint64_t =    8
size_t =      4
void* =       4
intptr_t =    4
Personal tools