sizeof(std :: string)似乎很小 [英] sizeof(std::string) seems to small

查看:68
本文介绍了sizeof(std :: string)似乎很小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的C ++社区,


我对C ++ std :: strings的大小有疑问。

基本上,我在下面编译了以下代码不同的编译器:


std :: string someString =" Hello,world!" ;;

int size1 = sizeof(std :: string);

int size2 = sizeof(someString);


并打印出size1和size2的值。 size1和size2总是

的值匹配(换句话说,size1 == size2)。这对我来说是有意义的。


在Visual C ++ 6.0下,size1和size2都等于16,但

下a GNU C ++编译器(在Linux下),size1和size2都是4.我理解允许不同的编译器以不同的方式实现
std :: string,这样可以区别对待不同编译器之间的sizeof(std :: string)的
结果之间。


我不明白为什么sizeof(std :: string)返回4,

任何编译器。我的意思是,4的值对我来说似乎太小了。我认为任何std :: string实现应该至少有一个

指针(指向主字符串),一个存储
$ b的整数$ b已经为主字符串分配空间(其值返回

调用std :: string :: capacity()),甚至可能是整数

存储字符串的长度。


仅指针将占用4个字节(我测试了它和

sizeof(char *)确实等于4 ),所以我看不出怎么可能

可能还有其他任何空间,比如

保存已分配空间的整数(使用的那个)在

std :: string :: capacity())中。事实上,Visual C ++有一个16的sizeof(std :: string)对我来说更有意义,因为它显然

有足够的空间来容纳这些整数。


所以我的主要问题是:假设sizeof(char *)等于4,那么

是否有可能sizeof(std :: string)可以在任何编译器上都是4?


另外,不应该sizeof(std :: string)至少sizeof(char *)+

sizeof( unsigned int)?我很好奇为什么它不在我正在使用的GNU C ++

编译器上。


提前感谢你们回复。


- 让 - 吕克

解决方案


jl ***** @ hotmail.com 写道:

亲爱的C ++社区,
<我有一个关于C ++ std :: strings大小的问题。
基本上,我在两个不同的编译器下编译了下面的代码:

std :: string someString =" Hello ,world!" ;;
int size1 = sizeof(std :: string);
int size2 = sizeof(someString);

并打印出size1和size2的值。 size1和size2的值总是匹配(换句话说,size1 == size2)。这对我来说是有道理的。

在Visual C ++ 6.0下,size1和size2都等于16,但是在GNU C ++编译器(在Linux下)下,size1和size2是4.我明白允许不同的编译器以不同的方式实现std :: string,这允许不同编译器对sizeof(std :: string)的结果进行区分。

我不明白为什么sizeof(std :: string)会随着任何编译器返回4。我的意思是,4的值对我来说似乎太小了。我想,任何std :: string实现都应该至少有一个
指针(指向主字符串),一个存储已经为主字符串分配的空间的整数(其值)在std :: string :: capacity()的调用中返回
,甚至可能是存储字符串长度的整数

仅指针就会占用4个字节(我测试了它和
sizeof(char *)确实等于4),所以我看不出怎么可能有更多的空间用于其他任何东西,比如整数
保存已经分配的空间(
std :: string :: capacity()中使用的空间)。事实上,Visual C ++的sizeof(std :: string)为16对我来说更有意义,因为它显然有足够的空间来容纳这些整数。
所以我的主要问题是:假设sizeof(char *)等于4,那么在任何编译器上sizeof(std :: string)可能是4的可能性如何?

此外,不应该sizeof(std :: string)至少是sizeof(char *)+
sizeof(unsigned int)?我很好奇为什么它不在我正在使用的GNU C ++
编译器上。

提前感谢您的任何回复。

- Jean-Luc




图书馆可以使用Pimpl习语来实现它(参见
http://www.gotw.ca/gotw/024.htm):


class stringImpl;


class string

{

public:

//转发功能

私人:

stringImpl * pImpl;

};


因此你只有指针作为会员。


干杯! --M


jl ***** @ hotmail。 com 写道:

亲爱的C ++社区,

我对C ++ std :: strings的大小有疑问。
基本上,我编译了以下代码在两个不同的编译器下:

std :: string someString =" Hello,world!" ;;
int size1 = sizeof(std :: string);
int size2 = sizeof(someString);

并打印出size1和size2的值。 size1和size2的值总是匹配(换句话说,size1 == size2)。这对我来说是有道理的。

在Visual C ++ 6.0下,size1和size2都等于16,但是在GNU C ++编译器(在Linux下)下,size1和size2是4.我明白允许不同的编译器以不同的方式实现std :: string,这允许不同编译器对sizeof(std :: string)的结果进行区分。

我不明白为什么sizeof(std :: string)会随着任何编译器返回4。


确实如此?真?等等,你不只是说在Visual C ++下

6.0,size1 ..等于16? ''size1''_is_''sizeof(std :: string)'',不是吗?

那么,为什么你说sizeof(std :: string)返回4与任何编译器 ?它显然不是VC ++ 6.0中的b $ b ...

[...]

所以我的主要问题是:假设sizeof(char *)等于4,如何在任何编译器上sizeof(std :: string)可能为4?


不是。

另外,不应该sizeof(std :: string)至少是sizeof(char *)+
sizeof(unsigned int)?我很好奇为什么它不在我正在使用的GNU C ++
编译器上。




"使用Source,Luke !"看看他们的实施。他们

可能有一个简单的事情,比如


class blah {

blah_internal * pimpl;

公开:

///所有会员只需将请求转发给''pimpl''

};


V


C ++中的任何类型都有固定值。这是因为编译器需要知道每个类型的确切大小来分配堆栈帧。


std :: string对象不包含字符串对象本身的数据

通常。相反,它动态地管理内存中的字符串内容

。通常,默认情况下,它会分配/管理/和

最终在免费商店中释放字符串内容。


std :: string只需要一个指向内容的指针和一个要缓存的整数

字符串的大小。当然,更复杂的表现形式可能是b $ b。许多版本的std :: string管理其动态字符串内容

作为一些内存块。


Ben

Dear C++ community,

I have a question regarding the size of C++ std::strings.
Basically, I compiled the following code under two different compilers:

std::string someString = "Hello, world!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);

and printed out the values of size1 and size2. size1 and size2 always
matched in value (in other words, size1 == size2). That makes sense to
me.

Under the Visual C++ 6.0, size1 and size2 both equalled 16, but
under a GNU C++ compiler (under Linux), size1 and size2 were both 4. I
understand that different compilers are allowed to implement
std::string differently which allows for the differences between the
results of sizeof(std::string) by the different compilers.

What I don''t understand is why sizeof(std::string) returns 4 with
any compiler. I mean, a value of 4 just seems too small for me. I
figure that any std::string implementation should have at least a
pointer (which points to the main string), an integer storing the
already allocated space for the main string (whose value gets returned
in the call to std::string::capacity()), and possibly even an integer
storing the length of the string.

Just the pointer alone would take up 4 bytes (I tested it and
sizeof(char*) does indeed equal 4), so I can''t see how there could
possibly be any more room for anything else, like the integer that
holds the already allocated space (the one used in
std::string::capacity()). The fact that Visual C++ has a
sizeof(std::string) of 16 makes a lot more sense to me, as it clearly
has enough space to hold these integers.

So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::string) can be 4 on any compiler?

Also, shouldn''t sizeof(std::string) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I''m curious why it isn''t on the GNU C++
compiler that I''m using.

Thank-you in advance for any responses.

-- Jean-Luc

解决方案


jl*****@hotmail.com wrote:

Dear C++ community,

I have a question regarding the size of C++ std::strings.
Basically, I compiled the following code under two different compilers:

std::string someString = "Hello, world!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);

and printed out the values of size1 and size2. size1 and size2 always
matched in value (in other words, size1 == size2). That makes sense to
me.

Under the Visual C++ 6.0, size1 and size2 both equalled 16, but
under a GNU C++ compiler (under Linux), size1 and size2 were both 4. I
understand that different compilers are allowed to implement
std::string differently which allows for the differences between the
results of sizeof(std::string) by the different compilers.

What I don''t understand is why sizeof(std::string) returns 4 with
any compiler. I mean, a value of 4 just seems too small for me. I
figure that any std::string implementation should have at least a
pointer (which points to the main string), an integer storing the
already allocated space for the main string (whose value gets returned
in the call to std::string::capacity()), and possibly even an integer
storing the length of the string.

Just the pointer alone would take up 4 bytes (I tested it and
sizeof(char*) does indeed equal 4), so I can''t see how there could
possibly be any more room for anything else, like the integer that
holds the already allocated space (the one used in
std::string::capacity()). The fact that Visual C++ has a
sizeof(std::string) of 16 makes a lot more sense to me, as it clearly
has enough space to hold these integers.

So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::string) can be 4 on any compiler?

Also, shouldn''t sizeof(std::string) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I''m curious why it isn''t on the GNU C++
compiler that I''m using.

Thank-you in advance for any responses.

-- Jean-Luc



The library could implement it using the Pimpl idiom (cf.
http://www.gotw.ca/gotw/024.htm):

class stringImpl;

class string
{
public:
// Forwarding functions
private:
stringImpl* pImpl;
};

Thus you have only a pointer as a member.

Cheers! --M


jl*****@hotmail.com wrote:

Dear C++ community,

I have a question regarding the size of C++ std::strings.
Basically, I compiled the following code under two different compilers:

std::string someString = "Hello, world!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);

and printed out the values of size1 and size2. size1 and size2 always
matched in value (in other words, size1 == size2). That makes sense to
me.

Under the Visual C++ 6.0, size1 and size2 both equalled 16, but
under a GNU C++ compiler (under Linux), size1 and size2 were both 4. I
understand that different compilers are allowed to implement
std::string differently which allows for the differences between the
results of sizeof(std::string) by the different compilers.

What I don''t understand is why sizeof(std::string) returns 4 with
any compiler.
It does? Really? Wait, didn''t you just say that "Under the Visual C++
6.0, size1 .. equalled 16"? And ''size1'' _is_ ''sizeof(std::string)'', no?
So, why do you say "sizeof(std::string) returns 4 with any compiler"? It
apparently does NOT in VC++ 6.0...
[...]

So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::string) can be 4 on any compiler?
It isn''t.
Also, shouldn''t sizeof(std::string) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I''m curious why it isn''t on the GNU C++
compiler that I''m using.



"Use the Source, Luke!" Just look at their implementation. They
may have a simple thing like

class blah {
blah_internal *pimpl;
public:
/// all members simply forwarding the requests to ''pimpl''
};

V


Any type in C++ has a fixed value. This is because the compiler needs to
know the exact size of each type to allocate stack frame.

An std::string object does not contain the string data in the object itself
typically. Rather, it dynamically manages the string content somewhere else
in the memory. Usually, and by default, it allocates/ manages/ and
eventually deallocates the string content on the free store.

An std::string only needs a pointer to the content and an integer to cache
the size of the string. Of course, more complex of representation is
possible. Many versions of std::string manages its dynamic string contents
as a number of memory "chunks".

Ben


这篇关于sizeof(std :: string)似乎很小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆