1

I am using mingw-w64 compiler (gcc 8.1.0) on Windows 10 (64 bit installation). My expectation is to get the maximum value of size_t equal to (2^64 - 1). But my programs always return the value of SIZE_MAX as (2^32 - 1 only). What could be the reason? How can i achieve the a maximum value of (2^64 - 1) in my C programs?

I am learning C language and wanted to check out the statement made in the book that on modern computers max size of size_t can be (2^64 - 1). The piece of code I tried for checking this is as below:

#include <stdio.h>
#include <stdint.h>

int main(){
    printf("%d\n", sizeof(size_t));
    printf("%zu\n", SIZE_MAX); 
    return 0;
}

Output:

4
4294967295

I am using only one flag -std=c11 during compiling and gcc --version returns:

gcc.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 2
    There's really nothing that states that it *must* be a 64-bit unsigned integer type, only that is must be unsigned. The size is implementation defined. So please explain why you need `size_t` to be a 64-bit value? What is the real problem you need to solve? – Some programmer dude Sep 25 '19 at 06:42
  • By the way, you did include the compiler-provided `` header file for the `SIZE_MAX` macro? And is it a 32- or 64-bit installation of Windows 10? – Some programmer dude Sep 25 '19 at 06:48
  • The reason is probably that the compiler must be able to implement `ptrdiff_t` on 64 bit types. Possible duplicate: [Why is the maximum size of an array “too large”?](https://stackoverflow.com/questions/42574890/why-is-the-maximum-size-of-an-array-too-large). – Lundin Sep 25 '19 at 06:59
  • @Someprogrammerdude I am learning C language and was curious to check out the statement given in one of the books that 'size_t' can return a max value of 2^64 - 1 on 64 bit systems. Yes, I have included `` and it is a 64 bit installation of Windows 10 – Pradeep Pillai Sep 25 '19 at 07:07
  • @Jabberwocky `sizeof(size_t)` returned 4 and `SIZE_MAX` returned 4294967295 which is 2^32 -1. – Pradeep Pillai Sep 25 '19 at 07:08
  • 1
    Then *how* do you build? What flags and options do you give when building? Are you sure you're using the correct compiler? If you run `gcc --version` does it tell you the correct version? And do you have a [mcve] of your test program that you can show us? Please edit your question to include all of this and all you just told us in the comments. – Some programmer dude Sep 25 '19 at 07:13
  • I'm not on the latest MSVC but ***it*** is 64-bit code but generates code with 32-bit pointers and a maximum approx 1.9 Gb memory available. It's impossible to allocate more (total of static + dynamic), even though the system has 8 Gb in total. – Weather Vane Sep 25 '19 at 07:15
  • @Someprogrammerdude Original question edited with all details – Pradeep Pillai Sep 25 '19 at 07:35
  • 3
    There are two problems with the code you show, both being that you have mismatching format specifiers and types (which leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior)). First of all, to print a `size_t` value you need to use the `"%zu"` format specifier. The `z` prefix to say it's a `size_t` and the `u` because it's an unsigned value. The second mismatch is that `SIZE_MAX` might be to large for the `int` format `"%u"`. You should use `PRIu64` format to be sure that it can be printed, and cast it to an unsigned 64-bit type (like `unsigned long long`). – Some programmer dude Sep 25 '19 at 07:41
  • 1
    Maybe related: https://superuser.com/questions/238112/what-is-the-difference-between-i686-and-x86-64 – Bob__ Sep 25 '19 at 07:42
  • 1
    @Someprogrammerdude "use PRIu64 format to be sure that it can be printed, and cast it to an unsigned 64-bit type (like unsigned long long)" is not the best suggestion. Using `PRIu64` with `(uint64_t)`, `"%llu"` with `(unsigned long long)` would be better. `(unsigned long long)` today is commonly 64-bit, but may be larger. – chux - Reinstate Monica Sep 25 '19 at 12:34

1 Answers1

5

When checking gcc --version you state that you get

gcc.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0

The important part of the output is this: i686-posix-dwarf-rev0, more specifically the i686 part.

This tells us that GCC is built as a 32-bit application (i686 is a 32-bit system), and will default to creating 32-bit executables.

To build a 64-bit executable you need to add the -m64 flag.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621