Getting an additional error message

During this section, I’ve come across an additional warning that doesn’t appear and isn’t addressed in this lecture. I’ve started looking into this, but I’m a beginner and can’t quite figure out what’s going on here (and wanting to address it in the spirit of sorting warnings).

It’s a C4267 warning - ‘return’: conversion from ‘size_t’ to ‘int32’, possible loss of data

For whatever reason, the compiler doesn’t like the return value on line 33 (return MyHiddenWord.length();). Code below…

https://gist.github.com/92408512049/4438c0ed2b77079cfa379923d89d31a1

Is anyone able to help me understand what’s happening here?

Okay, will admit that I didn’t search for a possible solution on this forum first (though I did scan this topic section). The suggested solution is to alter the using statement from…

using int32 = int;

to

using int32 = size_t;

However, it’s unclear to me if this needs to be done globally, and if it’s actually a good idea to make this change given that it breaks alignment with the current course syntax (I’ve gotten in trouble a few times already by doing that).

To test this, I made the change in the class file and it just threw out additional problems (didn’t actually fix it).

Thoughts?

What’s happening is that MyHiddenWord.length() returns a value of type size_t, which is not the same as int32. In fact, int32 is always stored in memory with 32 bits, while the size of size_t depends on the target platform (32 bits for 32-bit applications, 64 bits for 64-bit applications). You are probably compiling for a 64-bit platform, which means that you are trying to take the 64-bit value returned by MyHiddenWord.length() and assign it as the 32-bit return value. The compiler then warns you that this kind of conversion might cause loss of data (64 bits of data can’t fit in 32 bits).

In Visual Studio, take a look at the toolbar right below the menus. To the left of the “Play” button , there should be a box reading “x86” or “x64”. Make sure it is set to x86 (which is 32 bits), and the warning should go away.

Note that size_t is also unsigned, while int32 is signed, which might trigger some other warnings when making comparisons.

Privacy & Terms