Jump to content
  • Advertisement
Sign in to follow this  
Prog101

Truncation warning

This topic is 4064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
From MSDN:

Quote:
The identifier is converted to a smaller type, resulting in loss of information.


So, it seems to me as though there isn't a way to get rid of it.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!