From: Prajakta on
HI,

I am porting 32 bit application on 64-bit windows.
I am using Visual studio 2005 for building the application.

The existing code consists of a function call:

LPDWORD b;
LPWSTR a = new WCHAR[(unsigned int) b];

While building this code for 64-bit i get warning
warning C4311: 'type cast' : pointer truncation from 'LPDWORD' to 'unsigned
int'

As the pointer size changes from 4 bytes to 8 bytes.

Please provide me solution for this type of warning.

From: David Wilkinson on
Prajakta wrote:
> HI,
>
> I am porting 32 bit application on 64-bit windows.
> I am using Visual studio 2005 for building the application.
>
> The existing code consists of a function call:
>
> LPDWORD b;
> LPWSTR a = new WCHAR[(unsigned int) b];
>
> While building this code for 64-bit i get warning
> warning C4311: 'type cast' : pointer truncation from 'LPDWORD' to 'unsigned
> int'
>
> As the pointer size changes from 4 bytes to 8 bytes.
>
> Please provide me solution for this type of warning.
>

Prajakta:

The solution for this kind of warning is to use the correct type in the first place.

size_t b = 10;
LPWSTR a = new WCHAR[b];

Using LPDWORD in particular makes no sense whatever.

--
David Wilkinson
Visual C++ MVP
From: David Connet on
=?Utf-8?B?UHJhamFrdGE=?= <Prajakta(a)discussions.microsoft.com> wrote in
news:16CD61EF-104D-4652-BA1F-8C9DECFC6116(a)microsoft.com:

> HI,
>
> I am porting 32 bit application on 64-bit windows.
> I am using Visual studio 2005 for building the application.
>
> The existing code consists of a function call:
>
> LPDWORD b;
> LPWSTR a = new WCHAR[(unsigned int) b];
>
> While building this code for 64-bit i get warning
> warning C4311: 'type cast' : pointer truncation from 'LPDWORD' to
> 'unsigned int'

Looks like a bug in the original code. I suspect what was wanted was:
LPWSTR a = new WCHAR[(unsigned int)(*b)];

'b' is a _pointer_ to a DWORD. Casting a pointer to an unsigned int is just
asking for trouble (as David W noted also)

Dave Connet
From: Alex Blekhman on
"David Connet" wrote:
> Looks like a bug in the original code. I suspect what was wanted
> was:
> LPWSTR a = new WCHAR[(unsigned int)(*b)];

Actually, the cast to `unsigned int' in the above case is
redundant. Using DWORD with operator new is all right.

Alex


From: David Connet on
"Alex Blekhman" <tkfx.REMOVE(a)yahoo.com> wrote in news:#KJ3XAEtIHA.548
@TK2MSFTNGP06.phx.gbl:

> "David Connet" wrote:
>> Looks like a bug in the original code. I suspect what was wanted
>> was:
>> LPWSTR a = new WCHAR[(unsigned int)(*b)];
>
> Actually, the cast to `unsigned int' in the above case is
> redundant. Using DWORD with operator new is all right.

True - I just wanted to keep the code as close to the original to show the
OP the single missing '*'. Personally, I try to avoid casts. Especially c-
style casts.

// Corrected
LPWSTR a = new WCHAR[*b];

Dave