Sign in to follow this  
Prog101

Truncation warning

Recommended Posts

hi guys i'm clearing out my warnings and i have a warning C4305: 'type cast' : truncation from 'void *' to 'char' here is where it points to: lpszNew[cbData] = (TCHAR)NULL; they are initalized like so char *lpszNew; int cbData; can anyone give me a clue how to get rid of this warning

Share this post


Link to post
Share on other sites
Try just:

lpszNew[cbData] = NULL;

since I don't see what the cast was meant to achieve in the first place.

Edit: To clarify, NULL is defined as 0. There's not a whole lot of point casting 0 to anything.

Share this post


Link to post
Share on other sites
if unicode is defined, that 0 is stored as two bytes, and you are trying to assign it to one byte storage. that would cause that warning even though it's just 0. since u are explicitly using char, use (char) instead of (TCHAR).

Share this post


Link to post
Share on other sites
I'm assuming you use C++ because it easily could be and it's the most popular language here.

There are a couple of things wrong with your code. First, NULL is not a well-defined character. On most current implementations NULL==0=='\0', but that isn't guaranteed. All the C++ standard says is:
"The macro NULL is an implementation-defined C++ null pointer constant"
This could be 0xFFFFFFFF which isn't representable as a character and that is why you are being warned. The warning basicly tells you that if the null pointer has anything beyond the two first hex-digits then that will just be ignored, so (char)0xFFFFFFFF==0xFF.

NULL could also be the same as 'a' such that instead of having a termination value you will have an a, but of course this isn't likely.

You should never use NULL for anything, but null-pointers.

A second thing that I'm a bit worried about is the warning itself. It tells us that you try to convert NULL from void* to char. This seems to suggest that NULL is defined as a void*, but the standard states in a footnote about NULL:
"Possible definitions include 0 and 0L, but not (void*)0."
So it seems that your compiler violates the standard.

Third you use lpszNew as an array of char, but you convert NULL into a TCHAR. You should be more consistent and use the same type in both places.

As for the correct solution, use '\0' or if you want to use TCHARs then TEXT('\0').
lpszNew[cbData] = '\0';

Should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
A second thing that I'm a bit worried about is the warning itself. It tells us that you try to convert NULL from void* to char. This seems to suggest that NULL is defined as a void*...

He's using C (though he may not be aware of it). In C - not C++ - NULL is defined as (void *)0.

Incidentally, in C++ the correct solution is always to use 0, not NULL. Why? Because C++ provides automatic conversion to the correct pointer type for 0.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this