时间:2019-05-06 标签:c++sizeof(...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3629301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ sizeof( ... )
提问by Kevin Meredith
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
cout << "size of String " << sizeof(string );
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
输出:
size of String = 4
字符串的大小 = 4
Does that mean that, since sizeof(char) = 1 Byte (0 to 255)
, string can only hold 4
characters?
这是否意味着,因为sizeof(char) = 1 Byte (0 to 255)
, string 只能容纳4
字符?
回答by Ori Pessach
It isn't clear from your example what 'string' is. If you have:
从您的示例中不清楚“字符串”是什么。如果你有:
#include <string>
using namespace std;
then string
is std::string
, and sizeof(std::string)
gives you the size of the class instance and its data members, not the length of the string. To get that, use:
然后string
是std::string
,并sizeof(std::string)
为您提供类实例及其数据成员的大小,而不是字符串的长度。要做到这一点,请使用:
string s;
cout << s.size();
回答by Graham Perks
When string
is defined as:
当string
定义为:
char *string;
sizeof(string)
tells you the size of the pointer. 4 bytes (You're on a 32-bit machine.) You've allocated no memory yet to hold text. You want a 10-char string? string = malloc(10); Now string points to a 10-byte buffer you can put characters in.
sizeof(string)
告诉你指针的大小。4 个字节(您使用的是 32 位机器。)您尚未分配任何内存来保存文本。你想要一个 10 个字符的字符串吗?字符串 = malloc(10); 现在 string 指向一个 10 字节的缓冲区,您可以将字符放入其中。
sizeof(*string)
will be 1. The size of what string is pointing to, a char.
sizeof(*string)
将是 1。字符串指向的大小,一个字符。
If you instead did
如果你改为
char string[10];
sizeof(string)
would be 10. It's a 10-char array.
sizeof(*string)
would be 1 still.
sizeof(string)
将是 10。这是一个 10 个字符的数组。
sizeof(*string)
还是 1。
It'd be worth looking up and understanding the __countof macro.
值得查找和理解 __countof 宏。
Update: oh, yeah, NOW include the headers :) 'string' is a class whose instances take up 4 bytes, that's all that means. Those 4 bytes could point to something far more useful, such as a memory area holding more than 4 characters.
更新:哦,是的,现在包括标题:) 'string' 是一个类,它的实例占用 4 个字节,这就是全部。这 4 个字节可能指向更有用的东西,例如保存超过 4 个字符的内存区域。
You can do things like:
您可以执行以下操作:
string s = "12345";
cout << "length of String " << s.length();
回答by Chubsdad
sizeof(char)
is always 1 byte. A byte
which we think is 8-bits need not be the case
. There are architectures where a BYTE is 32-bits, 24-bits and so on. The sizeof
applied to any other type is in multiples of sizeof(char)
which is by definition 1
.
sizeof(char)
总是 1 个字节。一个byte
我们认为是8-bits need not be the case
。存在 BYTE 为 32 位、24 位等的体系结构。在sizeof
适用于任何其它类型的是在倍数sizeof(char)
是by definition 1
。
The next important thing to note is that C++ has three character types: plain char, signed char and unsigned char
. A plain char
is either signed
or unsigned
. So it is wrong to assume that char can have only values from 0 to 255. This is true only when a char is 8-bits, and plain char
is unsigned
.
下一个需要注意的重要事项是 C++ 具有三种字符类型:plain char, signed char and unsigned char
. Aplain char
是signed
或unsigned
。因此,假设 char 只能具有 0 到 255 之间的值是错误的。仅当 char 为 8 位时才是正确的,并且plain char
是unsigned
。
Having said, that assuming that 'string' is 'std::namespace'
, sizeof(string) == 4
means that the sizeof the 'std::string'
class is 4 bytes. It occupies 4 times the number of bytes that a 'char'
on that machine takes. Note that signed T, unsigned T always have the same size
. It does not mean that the actual buffer of characters (which is called string in common parlance) is only 4 bytes. Inside the 'std::string'
class, there is a non static member pointer which is allocated dynamically to hold the input buffer. This can have as many elements as the system allows (C++ places no restriction on this length). But since the 'std::string'
class only holds the pointer to this potentially infite length buffer, the sizeof(std::string) always remains the same as sizeof pointer on the given architecture which on your system is 4.
话虽如此,假设 'string' is 'std::namespace'
,则sizeof(string) == 4
意味着'std::string'
该类的大小为 4 个字节。它占用的字节数是'char'
该机器上 a 占用的字节数的 4 倍。Note that signed T, unsigned T always have the same size
. 这并不意味着字符的实际缓冲区(通常称为字符串)只有 4 个字节。在'std::string'
类内部,有一个动态分配的非静态成员指针来保存输入缓冲区。这可以具有系统允许的尽可能多的元素(C++ 对此长度没有限制)。但是由于'std::string'
该类只保存指向这个潜在无限长度缓冲区的指针,所以 sizeof(std::string) 始终与给定体系结构上的 sizeof 指针保持相同,在您的系统上为 4。
回答by Tarantula
I know a lot of people had answered your question, but here are some points:
我知道很多人已经回答了你的问题,但这里有几点:
- It's not the size of the
string
or the capacity of the string, this value represents the structural size of the classstring
, which you can see by its implementation (and it can change from implementation to implementation) that is a simple pointer; - As the
sizeof(string)
is the size of the class structure, you'll get the size of the only internal pointer, that in your case is 4 bytes (because you are in a 32-bit machine, this can change from platform to platform too); - This pointer inside the
string
class, points to a memory buffer where the class will hold the real string data, this memory buffer is reallocated as needed, it can increase/decrease as you append/delete/create more string text; - If you want to get the real size of the string, you need to call the
size()
method from the class which will check the memory buffer string size (which isn't the same as the memory buffer size).
- 它不是
string
字符串的大小或容量,这个值代表了类的结构大小string
,你可以通过它的实现(并且可以从实现到实现变化)看到,它是一个简单的指针; - 由于
sizeof(string)
是类结构的大小,您将获得唯一内部指针的大小,在您的情况下为 4 个字节(因为您使用的是 32 位机器,这也可能因平台而异); string
类内部的这个指针指向一个内存缓冲区,该类将保存真正的字符串数据,该内存缓冲区根据需要重新分配,它可以随着您追加/删除/创建更多字符串文本而增加/减少;- 如果要获取字符串的实际大小,则需要
size()
从类中调用该方法,该方法将检查内存缓冲区字符串大小(与内存缓冲区大小不同)。
I think your problem is your conception of sizeof
, see more information hereand hereis some explanation on how it works.
回答by Tom
No, it means that the sizeof the class string is 4.
不,这意味着类字符串的大小为 4。
It does not mean that a string can be contained in 4 bytes of memory. Not at all. But you have to difference between dynamic memory, used to contain the size
characters a string can be made of, and the memory occupied by the address of the first of those characters
这并不意味着一个字符串可以包含在 4 个字节的内存中。一点也不。但是你必须区分动态内存,用于包含size
可以组成字符串的字符,以及由这些字符中的第一个地址占用的内存
Try to see it like this:
试着这样看:
contents --------> |h|e|l|l|o| |w|o|r|ld|size_type size() const
|
sizeof 4 refers to the memory occupied by contents. What it contents? Just a pointer to (the address of ) the first character in the char array.
sizeof 4 指的是内容占用的内存。它的内容是什么?只是指向 char 数组中第一个字符(的地址)的指针。
How many characters does a string can contain ? Ideally, a character per byte available in memory.
一个字符串可以包含多少个字符?理想情况下,内存中每字节一个字符。
How many characters does a string actually have? Well, theres a member function called size()
that will tell you just that
一个字符串实际上有多少个字符?嗯,有一个成员函数被调用size()
,它会告诉你
s.max_size() // will give the true maximum size
s.capacity() // will tell you how much it can hold before resizing again
s.size() // tells you how much it currently holds
See more on the SGIpage !
在SGI页面上查看更多信息!
回答by Scott Stafford
Not at all. It means that the class's structure is that, it doesn't include the dynamic memory it can control. std::string will expand dynamically to meet any required size.
一点也不。这意味着类的结构是,它不包括它可以控制的动态内存。std::string 将动态扩展以满足任何所需的大小。
// Finding length of a string in C++
#include<iostream>
#include<string>
using namespace std;
int count(string);
int main()
{
string str;
cout << "Enter a string: ";
getline(cin,str);
cout << "\nString: " << str << endl;
cout << count(str) << endl;
return 0;
}
int count(string s){
if(s == "")
return 0;
if(s.length() == 1)
return 1;
else
return (s.length());
}
The 4 you get from sizeof
is likely a pointer of some kind to the larger structure. Although some optimizations on some platforms will use it as the actual string data until it grows larger than can fit.
您从中获得的 4sizeof
可能是某种指向更大结构的指针。尽管某些平台上的一些优化会将其用作实际的字符串数据,直到它变得无法容纳为止。
回答by dan04
A string
object contains a pointerto a buffer on the heap that contains the actual string data. (It can also contain other implementation-specific meta-information, but yours apparently doesn't.) So you're getting the size of that pointer, not the size of the array it points to.
一个string
对象包含一个指向包含实际字符串数据的堆上的缓冲区的指针。(它也可以包含其他特定于实现的元信息,但你的显然没有。)所以你得到的是那个指针的大小,而不是它指向的数组的大小。
回答by user2808359
you can also use strings and can find out its length by string.length() function. look at the below code:
您还可以使用字符串,并可以通过 string.length() 函数找出其长度。看看下面的代码:
##代码##you can get the details from : http://www.programmingtunes.com/finding-length-of-a-string-in-c/
您可以从以下位置获取详细信息:http: //www.programmingtunes.com/finding-length-of-a-string-in-c/