Where do I find the definition of size_t?

前端 未结 9 2184
醉酒成梦
醉酒成梦 2020-11-28 04:37

I see variables defined with this type but I don\'t know where it comes from, nor what is its purpose. Why not use int or unsigned int? (What about other \"similar\" types?

9条回答
  •  悲&欢浪女
    2020-11-28 04:51

    Practically speaking size_t represents the number of bytes you can address. On most modern architectures for the last 10-15 years that has been 32 bits which has also been the size of a unsigned int. However we are moving to 64bit addressing while the uint will most likely stay at 32bits (it's size is not guaranteed in the c++ standard). To make your code that depends on the memory size portable across architectures you should use a size_t. For example things like array sizes should always use size_t's. If you look at the standard containers the ::size() always returns a size_t.

    Also note, visual studio has a compile option that can check for these types of errors called "Detect 64-bit Portability Issues".

提交回复
热议问题