To get a trial key
fill out the form below
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
** By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
** By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
** By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
64-bit code issues in real programs: po…

64-bit code issues in real programs: pointer type change

Dec 03 2009
Author:

Explicit type conversions often mask errors related to a change of a pointer type. One of such errors is casting of a pointer to 32-bit objects into a pointer to 64-bit ones.

Let us look at one example received from the users of our tool PVS-Studio (Viva64). The error shows after porting the code to the 64-bit Windows:

void ProcessTime(long * pTime)
{
  time((time_t *)pTime);
}

In a 32-bit program, the 32-bit version of the type time_t was used. On a 64-bit system, only the 64-bit version of time_t type is available. This is explicitly defined in the header file crtdefs.h:

#ifdef  _USE_32BIT_TIME_T
#ifdef  _WIN64
#error You cannot use 32-bit time_t (_USE_32BIT_TIME_T) with _WIN64
#endif
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif

The explicit type conversion we have demonstrated, can lead to an unpredictable program behavior or crash. It is difficult to diagnose such errors as the construct of the explicit type conversion suppresses the compiler warnings (see the note: "Search of explicit type conversion errors in 64-bit programs").

The diagnostic warning "V114. Dangerous explicit type pointer conversion" generated by PVS-Studio (Viva64) code analyzer when checking 64-bit projects helps detect such errors. Besides the example given above, the diagnostic warning V114 can detect a similar error related to a change of an array type:

int array[4] = { 1, 2, 3, 4 };
size_t *sizetPtr = (size_t *)(array);
cout << sizetPtr[1] << endl;
Result on the 32-bit system: 2
Result on the 64-bit system: 17179869187

Comments (0)

Next comments
Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience. Would you like to learn more?
Accept