简体   繁体   中英

Vector won't store correct datatype (wchar_t instead of uint16_t)

I have some code from the net reading hyperspectral data (image, so lots of integers giving pixel intensity) into a vector. I used the code with success on a Linux system, but now I need the same on a windows system. I use Visual Studio 2008.

Reading the data in Linux I get a vector full of integers. On windows I get the integers and then some chars or byte data. I don't know enough to describe it better.

The vector is initialized by

std::vector< unsigned short int > data;
data.resize( samples * lines * bands );
std::fill( data.begin(), data.end(), 0 );

and the relevant code is

for( unsigned int i=0; i < num_pixels && file; ++i ){
     char number[sizeof(DataType)];
     file.read( number , sizeof( DataType ) );
     int l = sizeof(DataType)-1;
    if (machine_endian != header.big_endian) {
        for (int j = 0; j < l; j++, l--){
            number[j] ^=number[l];
            number[l] ^= number[j];
            number[j] ^= number[l];
        }
    }
 unsigned short temp = *((unsigned short int*)number);
     data[i] = temp;
}

The machine_endian part is never run. The temp is just to test if I can cast the number into an int. It works fine. However, when I put temp into the vector there's more information inserted than just the int and it's listed as wchar_t. See the image below. I guess it's something to do with type size, but I am clueless as to why. Is it mine or Visual Studios fault? Any ideas?

Actually, it is working fine. Microsoft just wants to facilitate inspecting wchar_t values (which are short int s and represent utf16 coded characters on windows), so their debugger shows short int s as wchar_t s. It's just a matter of interpretation.

If you had used char s for your data, you would encounter the same phenomenon on almost any architecture.

typedef struct {
    unsigned short x;
    unsigned short y;
    unsigned short z;
} vec3_t;

and used vec3_t instead of std::vector.

In Windows wchar_t is the same as unsigned short, while in Unix it's 4 bytes long, so you should create your own vector type to make your code portable.

Everything works fine, it's just that wchar_t is a synonym for unsigned short on your platform and so the debugger doesn't know what name to display there. Also there is no 'more information' inserted. It's again the MSVC debugger interprets the number as a wide-character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM