简体   繁体   English

谁能解释为什么使用size_t类型作为示例?

[英]can anyone explain why size_t type is used with an example?

I was wondering why this size_t is used where I can use say int type. 我想知道为什么在可以使用int类型的地方使用size_t Its said that size_t is a return type of sizeof operator. 它说size_tsizeof运算符的返回类型。 What does it mean? 这是什么意思? like if I use sizeof(int) and store what its return to an int type variable, then it also works, it's not necessary to store it in a size_t type variable. 就像如果我使用sizeof(int)并将其返回的内容存储到int类型变量中一样,那么它也可以工作,就不必将其存储在size_t类型变量中。 I just clearly want to know the basic concept of using size_t with a clearly understandable example.Thanks 我只是想通过一个易于理解的size_t来了解使用size_t的基本概念。

size_t is guaranteed to be able to represent the largest size possible, int is not. 可以保证size_t能够表示可能的最大大小,而int则不能。 This means size_t is more portable. 这意味着size_t更便于携带。

For instance, what if int could only store up to 255 but you could allocate arrays of 5000 bytes? 例如,如果int最多只能存储255个但您可以分配5000个字节的数组怎么办? Clearly this wouldn't work, however with size_t it will. 显然这是行不通的,但是使用size_t可以。

The simplest example is pretty dated: on an old 16-bit- int system with 64 k of RAM, the value of an int can be anywhere from -32768 to +32767, but after: 最简单的例子是相当日:一个旧的16比特上int系统64 k的RAM,一个的值int可以在任何地方从-32768到32767,但经过:

char buf[40960];

the buffer buf occupies 40 kbytes, so sizeof buf is too big to fit in an int , and it needs an unsigned int . 缓冲区buf占用40 KB,因此sizeof buf太大而无法容纳int ,因此需要unsigned int

The same thing can happen today if you use 32-bit int but allow programs to access more than 4 GB of RAM at a time, as is the case on what are called "I32LP64" models (32 bit int , 64-bit long and pointer). 如果您使用32位int但允许程序一次访问超过4 GB的RAM,今天就会发生同样的事情,就像所谓的“ I32LP64”模型(32位int ,64位long和指针)。 Here the type size_t will have the same range as unsigned long . 在这里, size_t类型的范围与unsigned long范围相同。

You use size_t mostly for casting pointers into unsigned integers of the same size, to perform calculations on pointers as if they were integers, that would otherwise be prevented at compile time. 您通常使用size_t将指针转换为相同大小的无符号整数,以对指针进行计算,就好像它们是整数一样,否则在编译时将被阻止。 Such code is intended to compile and build correctly in the context of different pointer sizes, eg 32-bit model versus 64-bit. 此类代码旨在在不同的指针大小(例如32位模型与64位模型)的上下文中正确编译和构建。

size_t is a typedef defined to store object size. size_t是定义存储对象大小的typedef。 It can store the maximum object size that is supported by a target platform. 它可以存储目标平台支持的最大对象大小。 This makes it portable. 这使其易于携带。

For example: 例如:

void * memcpy(void * destination, const void * source, size_t num);

memcpy() copies num bytes from source into destination. memcpy()将 num个字节从源复制到目标。 The maximum number of bytes that can be copied depends on the platform. 可以复制的最大字节数取决于平台。 So, making num as type size_t makes memcpy portable. 因此,将num设置size_t类型使memcpy可移植。

Refer https://stackoverflow.com/a/7706240/2820412 for further details. 有关更多详细信息,请参阅https://stackoverflow.com/a/7706240/2820412

  1. size_t is a typedef for one of the fundamental unsigned integer types. size_t是基本无符号整数类型之一的typedef。 It could be unsigned int, unsigned long, or unsigned long long depending on the implementation. 根据实现的不同,它可以是unsigned int,unsigned long或unsigned long long。
  2. Its special property is that it can represent the size of (in bytes) of any object (which includes the largest object possible as well!). 它的特殊属性是它可以表示任何对象(还包括最大的对象!)的大小(以字节为单位)。 That is one of the reasons it is widely used in the standard library for array indexing and loop counting (that also solves the portability issue). 这是它在标准库中广泛用于数组索引和循环计数的原因之一(这也解决了可移植性问题)。 Let me illustrate this with a simple example. 让我用一个简单的例子来说明这一点。

Consider a vector of length 2*UINT_MAX, where UINT_MAX denotes the maximum value of unsigned int (which is 4294967295 for my implementation considering 4 bytes for unsigned int). 考虑一个长度为2 * UINT_MAX的向量,其中UINT_MAX表示无符号int的最大值(对于我的实现,考虑到无符号int为4个字节,则为4294967295)。

std::vector vec(2*UINT_MAX,0); std :: vector vec(2 * UINT_MAX,0);

If you would want to fill the vector using a for-loop such as this, it would not work because unsigned int can iterate only upto the point UINT_MAX (beyond which it will start again from 0). 如果要使用这样的for循环填充向量,则它将无法正常工作,因为unsigned int只能迭代到UINT_MAX点(超过该点它将再次从0开始)。

for(unsigned int i = 0; i<2*UINT_MAX; ++i) vec[i] = i; for(unsigned int i = 0; i <2 * UINT_MAX; ++ i)vec [i] = i;

The solution here is to use size_t since it is guaranteed to represent the size of any object (and therefore our vector vec too!) in bytes. 这里的解决方案是使用size_t,因为可以保证以字节为单位表示任何对象的大小(因此也可以表示矢量vec!)。 Note that for my implementation size_t is a typedef for unsigned long and therefore its max value = ULONG_MAX = 18446744073709551615 considering 8 bytes. 请注意,对于我的实现,size_t是无符号long的typedef,因此考虑8个字节,其最大值= ULONG_MAX = 18446744073709551615

for(size_t i = 0; i<2*UINT_MAX; ++i) vec[i] = i; for(size_t i = 0; i <2 * UINT_MAX; ++ i)vec [i] = i;

References: https://en.cppreference.com/w/cpp/types/size_t 参考: https : //en.cppreference.com/w/cpp/types/size_t

它是实现定义的,但是在64位系统上,您会发现size_t通常为64位,而int仍为32位(除非是ILP64或SILP64模型)。

depending on what architecture you are on (16-bit, 32-bit or 64-bit) an int could be a different size. 根据您所使用的体系结构(16位,32位或64位),int的大小可能会有所不同。

if you want a specific size I use uint16_t or uint32_t .... You can check out this thread for more information 如果您想要特定的大小,请使用uint16_t或uint32_t...。您可以查看此线程以获取更多信息

What does the C++ standard state the size of int, long type to be? C ++标准说明int,long类型的大小是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM