- Jun 15, 2001
- 34,413
- 1,601
- 126
I'm off on another C++ programming adventure and this time I'm dealing with creating an array of unsigned chars of a certain size. The program works fine, but VC++ gives me a warning when I request more than 255 of these:
Specifically the warnings I get are:
Now I get that 255 is the largest value which can be stored in an unsigned char, but my understanding is that this code is requesting a 32-bit memory address pointing to the first of 256 unsigned chars and storing that value into a 32-bit-large pointer to unsigned chars.
What am I missing here?
Code:
// I get a warning from this
unsigned char* out = new unsigned char(256);
// I get no warning from this
unsigned char* out = new unsigned char(255);
Specifically the warnings I get are:
Code:
Warning C4305: 'initializing': truncation from 'int' to 'unsigned char'
Warning C4309: 'initializing': truncation of constant value
Now I get that 255 is the largest value which can be stored in an unsigned char, but my understanding is that this code is requesting a 32-bit memory address pointing to the first of 256 unsigned chars and storing that value into a 32-bit-large pointer to unsigned chars.
What am I missing here?