简体   繁体   English

如何检索数据类型的大小?

[英]How to retrieve size of a data type?

Let a be an array. a为数组。 So what is the difference between a.size() and sizeof(a) ? 那么a.size()sizeof(a)什么区别?

.size() can return anything, depends on how it is implemented. .size()可以返回任何内容,具体取决于实现方式。

Anyway, .size() usually returns the size of an container , in other words - the number of elements, that the container contains. 无论如何, .size() 通常返回容器的大小,换句话说-容器包含的元素数。

The sizeof operator is integrated operator, that returns the number of bytes, allocated on the stack. sizeof运算符是集成运算符,它返回在堆栈上分配的字节数。 For example 例如

        char a[50];
        char* b = new char[50];
        cout << sizeof( a ) << '\n';
        cout << sizeof( b );

prints 50 (which is 50 * sizeof( char ) = 50 * 1 = 50 ), and the second line prints 8 - as this is the size of the pointer ( my machine is x64 , that's why it's 8, if I had 32bit CPU, it would be 4). 打印50(这是50 * sizeof( char ) = 50 * 1 = 50 ),第二行打印8-因为这是指针的大小( 我的机器是x64 ,这就是为什么如果我有32位CPU的原因是8 ,那就是4)。

cout << sizeof( *b ); would print 1, as *b dereferences the pointer and returns the first element of the array (it's the same as sizeof( b[0] ) which is sizeof( char ) ) 将打印1,因为*b取消了对指针的引用并返回数组的第一个元素(与sizeof( b[0] )相同,后者为sizeof( char )

In other words, you'd better rely on .size() if you want to see the number of elements, if it's a container and if it provides such method, of course. 换句话说,如果要查看元素的数量(如果它是一个容器)并且如果它提供了这样的方法,则最好依靠.size()


Other example: 其他例子:

class A
{
        int a;
        char b[100];
};

class B
{
        int a;
        char* b;
public:
        B()
        {
                b = new char[100];
        }
        ~B()
        {
                delete[] b;
        }
};
int main()
{
        cout << sizeof( A ) << '\n';
        cout << sizeof( B ) << '\n';
        B b;
        cout << sizeof( b ) << '\n';
        return 0;
}

The first one, on a x64 plarform, will print 104, as we have 100*sizeof(char) + sizeof(int) (=4 ) ( note 104 mod 8 == 0 , you'll see why ); 第一个在x64平台上将打印104,因为我们有100*sizeof(char) + sizeof(int) (=4 )注意 104 mod 8 == 0 ,您将看到原因);

The second one and the third one will print the same. 第二个和第三个将打印相同。 But note , we have alignment here! 但是请注意 ,我们在这里对齐! On x64, the alignment is on 8B, so sizeof( char * ) gives 8, sizeof( int ) gives 4 and this makes 12. BUT 12 mod 8 == 4 which is != 0, so because of the alignment, sizeof( B ) prints 16 ( 16 mod 8 == 0 ). 在x64上,对齐方式是在8B上,因此sizeof( char * )给出8, sizeof( int )给出4并由此得出12。 但是 12 mod 8 == 4这是!= 0,因此由于对齐, sizeof( B )打印16 (16 mod 8 == 0)。

So, when you use sizeof , be very careful.. 因此,当您使用sizeof ,请非常小心。
Hope that helps (: 希望对您有所帮助(:


For your question about list, take a look at this: 关于列表的问题,请看一下:

        list< int > lint;
        list< char > lchar;
        cout << sizeof( lint ) << '\n';
        cout << lint.size() << '\n';
        cout << sizeof( lchar ) << '\n';
        lint.push_back( 10 );
        lint.push_back( 10 );
        cout << lint.size() << '\n';
        cout << sizeof( lint ) << '\n';

All operators sizeof will print the same, depends on the implementation of std::list . 所有操作符sizeof都将打印相同的内容,具体取决于std::list的实现。 But the first .size() will return 0, as there're no elements, and the second one will return 2, as we have 2 elements in the list. 但是第一个.size()将返回0,因为没有元素,而第二个.size()将返回2,因为列表中有2个元素。

sizeof(a) is the number of bytes used by a , not including any dynamically allocated memory. sizeof(a)是由所使用的字节数a ,不包括任何动态分配的内存。 This is a compile time constant. 这是一个编译时间常数。 You can also use it with types, sizeof(int) gives you the number of bytes in an int. 您也可以将其与类型一起使用, sizeof(int)提供sizeof(int)的字节数。

a.size() is usually defined for container types, and gives the number of elements currently in the container (not the number of bytes). 通常为容器类型定义a.size() ,并给出容器中当前的元素数(而不是字节数)。 This is a runtime value, that changes as you add or remove objects from the container. 这是一个运行时值,随着您在容器中添加或删除对象而改变。

a.size() gives you value that is special for class. a.size()为您提供了类特别的值。 For example length of a string or a list. 例如字符串或列表的长度。 It depends on type of a class and person who implemented it. 这取决于课程的类型和实施人员。 But usually size is meaningful like in lists. 但通常size是有意义的,如列表中所示。

Sizeof is an operator that you should avoid if you are a beginner programmer. 如果您是新手程序员,那么应该避免使用sizeof运算符。 It was widely used in C to alloc memory. 它被广泛应用在C到alloc内存。 It returns how much space your object occupies in memory. 它返回对象在内存中占用的空间。 In C++ we use new to dynamically allocate memory. 在C ++中,我们使用new来动态分配内存。 It is much easier. 这要容易得多。 More about sizeof: http://en.wikipedia.org/wiki/Sizeof 有关sizeof的更多信息: http : //en.wikipedia.org/wiki/Sizeof

Not sure about a.size(), but sizeof(a) returns its length in bytes . 不确定a.size(),但sizeof(a)返回其长度( 以字节为单位) If you want the number of elements in the array, divide it by the size of each element. 如果需要数组中元素的数量,请将其除以每个元素的大小。 For example: 例如:

int size = sizeof(myArray) / sizeof(*myArray);

If a is an array - 如果a是数组-

int a[5] ; // Taking it as an array that can hold integers.

Now - 现在-

a.size(); // Error. `a` is not a class type to use `.` operator on it. 

sizeof(a); // Correct. It returns 20 because 5*4 bytes.

sizeof is a unary operator that returns the size of the datatype , in number of bytes. sizeof是一元运算符,返回数据类型的大小(以字节数为单位)。

sizeof(std::array<int, 3>) == sizeof(int[3]) == 3 * sizeof(int)
class Foo
{
  int a, b;
};
sizeof(Foo) == sizeof(int) * 2

size() is a method of template<typename T, size_t S> std::array<T,S> returning the number of elements in the array. size()template<typename T, size_t S> std::array<T,S>的方法,该方法返回数组中的元素

std::array<int, 3> a = {1,2,3};
a.size() == 3;

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

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