Difference between revisions of "maxint"

From Lazarus wiki
(Corrected bad information about MAXINT and added an example program.)
Line 1: Line 1:
''Maxint'' is a special [[const]] predefined by the compiler which declares the positive [[Integer|integer]] with the largest magnitude which the compiler can handle directly.  This typically is the word size for the particular compiler, and varies according to the number of bits the processor for that machine uses.  Note that maxint is an [[unsigned]] value.
 
  
The typical values of MAXINT are:
+
'''MAXINT''' is a global constant, equal to the upper bound of the [[Integer|integer]] type, [http://www.moorecad.com/standardpascal/iso7185.html#6.7.2.2%20Arithmetic%20operators per the ISO 7185 standard].
* 8-bit and 16-bit machines, 2**16-1: 65,535
 
* 32-bit machines, 2**32-1: 4,294,967,295
 
* 64-bit machines, 2**64-1: 18,446,744,073,709,551,615
 
  
Maxint can be overridden with a user-defined value.
+
Note that this value changes based on the compiler mode and has nothing to do with the capabilities of the host or target machine.
 +
 
 +
If you want to check the size of the address space, check '''sizeof(pointer)'''.
 +
 
 +
If you want to know if you are running on a CPU with 64-bit ALU, check whether the symbol '''cpu64''' is defined.
 +
 
 +
The following example illustrates how these values relate:
 +
 
 +
<syntaxhighlight>
 +
program maxvals;
 +
  const width = 20;
 +
begin
 +
  writeln;
 +
  writeln( 'these change depending on compiler mode:' );
 +
  writeln( '----------------------------------------' );
 +
  writeln( 'maxint:          :', maxint : width );
 +
  writeln( 'high( integer )  :', high( integer ) : width );
 +
  writeln;
 +
  writeln( 'constant, regardless of mode or target: ' );
 +
  writeln( '----------------------------------------' );
 +
  writeln( 'high( int32 )    :', high( int32 ) : width );
 +
  writeln( 'high( int64 )    :', high( int64 ) : width );
 +
  writeln;
 +
  writeln( 'variable, depending on target cpu:' );
 +
  writeln( '----------------------------------------' );
 +
  writeln( 'sizeof( pointer ) :', sizeof( pointer ) : width );
 +
  writeln;
 +
  writeln( 'compile-time definitions:' );
 +
  writeln( '----------------------------------------' );
 +
  {$IFDEF cpu64} writeln( 'cpu64' ); {$ENDIF}
 +
  {$IFDEF cpu32} writeln( 'cpu32' ); {$ENDIF}
 +
  {$IFDEF cpu16} writeln( 'cpu16' ); {$ENDIF}
 +
  writeln;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
Compare the results with '''-Mtp''' vs '''-Mobjfpc''', for example:
 +
 
 +
<pre>
 +
fpc -Mtp maxvals.pas && ./maxvals
 +
fpc -Mobjfpc maxvals.pas && ./maxvals
 +
</pre>

Revision as of 00:18, 29 November 2012

MAXINT is a global constant, equal to the upper bound of the integer type, per the ISO 7185 standard.

Note that this value changes based on the compiler mode and has nothing to do with the capabilities of the host or target machine.

If you want to check the size of the address space, check sizeof(pointer).

If you want to know if you are running on a CPU with 64-bit ALU, check whether the symbol cpu64 is defined.

The following example illustrates how these values relate:

program maxvals;
  const width = 20;
begin
  writeln;
  writeln( 'these change depending on compiler mode:' );
  writeln( '----------------------------------------' );
  writeln( 'maxint:           :', maxint : width );
  writeln( 'high( integer )   :', high( integer ) : width );
  writeln;
  writeln( 'constant, regardless of mode or target: ' );
  writeln( '----------------------------------------' );
  writeln( 'high( int32 )     :', high( int32 ) : width );
  writeln( 'high( int64 )     :', high( int64 ) : width );
  writeln;
  writeln( 'variable, depending on target cpu:' );
  writeln( '----------------------------------------' );
  writeln( 'sizeof( pointer ) :', sizeof( pointer ) : width );
  writeln;
  writeln( 'compile-time definitions:' );
  writeln( '----------------------------------------' );
  {$IFDEF cpu64} writeln( 'cpu64' ); {$ENDIF}
  {$IFDEF cpu32} writeln( 'cpu32' ); {$ENDIF}
  {$IFDEF cpu16} writeln( 'cpu16' ); {$ENDIF}
  writeln;
end.

Compare the results with -Mtp vs -Mobjfpc, for example:

fpc -Mtp maxvals.pas && ./maxvals
fpc -Mobjfpc maxvals.pas && ./maxvals