size_t in Visual Studio 2019 32-bit vs 64-bit

Started by
8 comments, last by rick_appleton 3 years, 11 months ago

Does anyone know why the following code would compile as is in Visual Studio 2019 in 64-bit, but not in 32-bit when compiling as C? I suspect a quirk in the compiler, but would like to rule out anything I'm missing.

(Note that I know I can include for example stddef.h to get the definition of size_t, I'm more interested in why it would work in 64-bit)

int square() {
    size_t i = 5;
    return (int)i;
}

See the following links:

https://godbolt.org/z/DMR9z5

vs

https://godbolt.org/z/QnsTi3

Advertisement

Include <stddef.h>. I have never compiled a 32 bit program and do not know if there are differences like this. It might, though, simply be that in the second example stddef.h has already been included elsewhere, e.g. via stdio.h …

Green_Baron said:

Include <stddef.h>. I have never compiled a 32 bit program and do not know if there are differences like this. It might, though, simply be that in the second example stddef.h has already been included elsewhere, e.g. via stdio.h …

Check the links. That piece of code is all there is. No headers are included. The links are online, but the same thing happens when you try this locally in Visual Studio.

Check the documentation ;-)

I can't check locally because Linux and i lack a 32bit environment. But a Windows colleague will surely show up soon. Anyway, if it says size_t needs <stddef.h> to be included (Edit: or one of the other headers that have it), size_t will be available if that is included.

And if you cite me, do so without adding silly links !

@Green_Baron I didn't purposefully add that link, no idea how it got there.

As stated in my original post: I wanted to know why it DOES compile in 64-bit. I get why it DOESN'T compile in 32-bit.

OK, i have asked in the appropriate subforum if this insertion is done by the forum software.

Anyway, GCC doesn't compile size_t if stddef or for example stdio isn't included. I'd simply include it to be on the safe side and in comformity with the docu.

The C Programming Language (ISO/IEC 9899 7.17) says you have to include <stddef.h> to get the definition of size_t. If the standard tells you have to do something and you do not do it, your program's behaviour is undefined. The internet is full of people asking questions to the effect of “I have written code containing undefined behaviour and it does not do what I expect. Why?”. The answer is always that your expectations are incorrect.

A compiler could have a built-in definition of size_t, so you code will compile without including <stddef.h>. Relying on that would be relying on undefined behaviour. This is what's happening in your case.

Stephen M. Webb
Professional Free Software Developer

Btw there is an online solution I regularly use for such tests on different compilers

Shaarigan said:

Btw there is an online solution I regularly use for such tests on different compilers

Something like the links in my first post you mean? :D

This topic is closed to new replies.

Advertisement