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