Public Group

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.

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 on other sites
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 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 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 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 on other sites
Quote:
 Original post by CTarA 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.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631740
• Total Posts
3001965
×