I'm having difficulties understanding and printing the max value of a size_t type.

Apparantly the size_t is typedefed to a unsigned long int on my system. Is this safe to assume its the same on all c++ compilers?

The 'sizeof' function returns the number of bytes(size char), used to represent the given datatype.

Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?

The size_t is a unsigned long int, so apparantly it uses 8 bytes,
thats 8*8 bits, thats 64 bit
Why cant I do a 63 bit left shift?

It seems that the max value of a size_t on my 64bit ubuntu system is
18446744073709551615
If I had one hell of a machine would this mean that I should be able to allocate an array with this extreme big dimension?

Thanks in advance

#include <iostream>
#include <limits>

int main(){
  std::cout<<(size_t)-1 <<std::endl;
  std::cout<<std::numeric_limits<std::size_t>::max()<<std::endl;
  printf("max using printf:%lu\n",std::numeric_limits<std::size_t >::max()\
);
  printf("size of size_t: %lu\n",sizeof(size_t ));
  printf("size of size_t: %d\n",sizeof(size_t ));
  printf("size of long int: %d\n",sizeof(long int));
  printf("size of unsingned long int: %d\n",sizeof(unsigned long int));

  unsigned long int tmp = 1<<63;
  std::cout<<tmp<<std::endl;

  return 0;
}
-*- mode: compilation; default-directory: "~/" -*-
Compilation started at Sun Mar 29 00:24:02

g++ test.cpp -Wall
test.cpp: In function ‘int main()’:
test.cpp:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has t\
ype ‘long unsigned int’
test.cpp:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has t\
ype ‘long unsigned int’
test.cpp:12: warning: format ‘%d’ expects type ‘int’, but argument 2 has t\
ype ‘long unsigned int’
test.cpp:14: warning: left shift count >= width of type

Compilation finished at Sun Mar 29 00:24:02
./a.out
18446744073709551615
18446744073709551615
max using printf:18446744073709551615
size of size_t: 8
size of size_t: 8
size of long int: 8
size of unsingned long int: 8
0

Recommended Answers

All 6 Replies

>>Is this safe to assume its the same on all c++ compilers?
No. See limits.h for size limitations.

>>Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?

Probably because size_t is unsigned long and %d is signed int. Try %u and see if the warning persists.

>>The size_t is a unsigned long int, so apparantly it uses 8 bytes,
No, its only 4 bytes on a 32-bit compiler. See the sizeof operator.

>>It seems that the max value of a size_t on my 64bit ubuntu system is
18446744073709551615

It's the compiler's limits in limits.h. If you are using a 32-bit compiler than the size of size_t is still 32-bits even though you may be running the compiler on a 64-bit operating system.

Considering the output you received from your program I would guess that g++ is compiling as a 64-bit compiler.

>Apparantly the size_t is typedefed to a unsigned long int on my system. Is this safe to assume its the same on all c++ compilers?

Nope: "The value of the result (of the operator sizeof) is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers)."

>Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?

Use well-known %lu format specifier for unsigned long int...

>Why cant I do a 63 bit left shift?
1<<63 - constant 1 type is int, not unsigned long int...

>If I had one hell of a machine would this mean that I should be able to allocate an array with this extreme big dimension?

Nope. The language standard does not define max array extent, it's an implementation-defined value.

Why does printf("size of size_t: %d\n",sizeof(size_t )); make a warning?

Use well-known %lu format specifier for unsigned long int...

What I don't really grasp is that the sizeof function maps into 'unsigned long int' domain, instead of simply int?

Why cant I do a 63 bit left shift?

1 << 63 - constant 1 type is int, not unsigned long int...

thanks typecasting (unsigned long int) 1 << 63 works

Nope. The language standard does not define max array extent, it's an implementation-defined value.

But given the 'one hell of a machine' would I be able to allocate
integer array with length '18446744073709551615' doing

int[] array = new int[18446744073709551615]

Thanks for your reply

I found out that the sizeof function does return a size_t type.
So it wasn't that strange,

thanks for your reply

> I found out that the sizeof function does return a size_t type.

Correct. And to print that portably you use %zu not %u or %lu:

printf("%zu\n", sizeof(int));

>>Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?
>Use well-known %lu format specifier for unsigned long int...
What I don't really grasp is that the sizeof function maps into 'unsigned long int' domain, instead of simply int?

No, do not use %lu to printf a sizeof() return value. Use %zu instead;
it's defined to print sizeof correctly on all machines.

The return value of sizeof() is never negative, which is why it's %zu
and not %zd.

The return value of sizeof() differs for different architectures.
That's why you can't use %u or %lu and must use the special %zu in
printf() formats.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.