locked
Continue on an old respond about System.IntPtr RRS feed

  • Question

  • User-1495974460 posted

    The idea with System.IntPtr is to represent an integer whose width in bits correspond to the size of a pointer in the hardware and operating system that the CLR is running in, it's platform specific in other words. It supports 32-bit and 64-bit architectures only.

    This is a legacy from old-style C API's where pointers and ints were frequently treated as interchangeable. The Win32 API has many such parameters, where the width varies with the underlying architecture - thus the mess with porting of code from 32-bit to 64-bit (we already did this once when going from 16-bit to 32-bit, funny how we never learn...).

    In practice, as is mentioned in other responses, it can be used to hold unmanaged resource handles and pointers etc that stem from the underlying Win32 functions. It can also be used to hold integers of the natural size for current platform. You can also use IntPtr.Size to dtermine the size of a pointer on the current platform.

     

    My Question:

    SO when you explain this to a child ;) this would be used to let program's flow at the same speed at diffrent kinds of operating systems?

    Monday, October 18, 2010 6:23 AM

Answers

  • User187056398 posted

    this would be used to let program's flow at the same speed at diffrent kinds of operating systems?
     

    No.  It has nothing to do with speed.

    Using a "flexible" data size allowed programs to be ported easily to different machines (8-bit, 16-bit, 32-bit, etc.).  The pointers would always be the correct size...the size of the CPU's hardware registers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 8:10 AM