string :: c_str()通常是如何实现的? [英] How is string::c_str() usually implemented?

查看:315
本文介绍了string :: c_str()通常是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇string :: c_str的性能,

所以我想知道它是如何实现的。执行

大多数std :: string实现只需保留一个额外的

char为NULL终止分配,这样他们就可以

返回指向其内部的指针缓冲区,或者是

它们同样可以根据需要创建一个新的缓冲区吗?

我知道标准不需要任何特殊的

实现,这就是为什么我很好奇,如果在实现中有共同的

a。

I''m curious about the performance of string::c_str,
so I''m wondering how it''s commonly implemented. Do
most std::string implementations just keep an extra
char allocated for the NULL termination so they can
return a pointer to their internal buffer, or are
they equally likely to create a new buffer on demand?
I know the standard doesn''t require any particular
implementation, which is why I''m curious if there is
a consensus among implementations.

推荐答案

在星期二,25 2004年5月10:18:43 -0400,Derek< us ** @ nospam.org>写道:
On Tue, 25 May 2004 10:18:43 -0400, Derek <us**@nospam.org> wrote:
我很好奇string :: c_str的性能,所以我想知道它是如何实现的。大多数std :: string实现只是为NULL终止分配了额外的
char,这样他们就可以返回指向其内部缓冲区的指针,或者它们同样可能按需创建一个新的缓冲区?
我知道标准并不需要任何特定的实现,这就是为什么我很好奇是否有实现之间的共识。
I''m curious about the performance of string::c_str,
so I''m wondering how it''s commonly implemented. Do
most std::string implementations just keep an extra
char allocated for the NULL termination so they can
return a pointer to their internal buffer, or are
they equally likely to create a new buffer on demand?
I know the standard doesn''t require any particular
implementation, which is why I''m curious if there is
a consensus among implementations.




我看了大约5,他们都直接返回指针,或者

先做一个条件测试并返回几个指针中的一个(我怀疑

这与小字符串优化有关)。但是没有一个人能够创造一个新的缓冲区。我不知道他们/怎么样/,$ / b $ b因为它会改变返回指针的动态。调用者

将负责删除内存,使用与正常

实现的代码不兼容的

库实现呈现代码。


查看标准'c_str(21.3.6)的规格,我没有看到任何明确禁止分配副本的
(我或许只是不知道

在哪里看),但它说的指针将变为无效

在调用非常量字符串成员函数之后(在相关的

字符串上)几乎讲述了这个故事。

-leor

-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www .bdsoft.com / tools / stlfilt.html


2004年5月25日星期二10:18:43 -0400,Derek写道:
On Tue, 25 May 2004 10:18:43 -0400, Derek wrote:
我很好奇string :: c_str的性能,所以我想知道它是如何实现的。大多数std :: string实现只是为NULL终止保留一个额外的char,这样它们就可以返回指向它们内部缓冲区的指针,或者它们是否同样可以创建一个新的缓冲区一经请求?我知道标准并不需要任何特定的实现,这就是为什么我很好奇实现之间是否存在共识。
I''m curious about the performance of string::c_str, so I''m wondering how
it''s commonly implemented. Do most std::string implementations just
keep an extra char allocated for the NULL termination so they can return
a pointer to their internal buffer, or are they equally likely to create
a new buffer on demand? I know the standard doesn''t require any
particular implementation, which is why I''m curious if there is a
consensus among implementations.




这里是来自GCC-3.4 C ++标准库basic_string.h:

// _Rep:字符串表示

//不变量:

// 1.字符串确实包含_M_length + 1个字符;最后设置

//仅在调用c_str()时为0。我们避免实例化//

_CharT(),其中接口不需要它。


我相信其他实现也会保留一个额外的字节,但我'' m不是

确定他们是否将它设置为0。


-Ryan Mack

电子邮件:[第一封信名字] [姓] @ [姓氏] man.net



Here''s from the GCC-3.4 C++ standard library basic_string.h:
// _Rep: string representation
// Invariants:
// 1. String really contains _M_length + 1 characters; last is set
// to 0 only on call to c_str(). We avoid instantiating //
_CharT() where the interface does not require it.

I believe other implementations also keep an extra byte around but I''m not
sure if they keep it set to 0 or not.

-Ryan Mack
email: [first letter of first name][last name]@[last name]man.net


Leor Zolman写道:
Leor Zolman wrote:

我看了一下5,他们都要么直接返回指针,要么首先做一个条件测试并返回几个指针中的一个(我怀疑这与小字符串优化有关)。但是我没看过的那些人都没有创建新的缓冲区。我不知道他们/怎么样/,
因为它会改变返回指针的动态。调用者将负责删除内存,使用与正常实现的代码不兼容的库实现来呈现代码。

查找标准对于c_str(21.3.6)的规格,我没有看到明确禁止分配副本的任何内容(我可能只是不知道在哪里看),但是方式它表示指针将变为无效
任何对非常量字符串成员函数的调用(在相关的
字符串上)几乎都讲述了这个故事。

I looked at about 5, and they all either return the pointer directly, or
first do a conditional test and return one of several pointers (I suspect
this has to do with the small string optimization). But none of the ones
I''ve looked at go creating a new buffer. I don''t see how they /could/,
because it would change the dynamics of the pointer returned. The caller
would become responsible for deleting the memory, rendering code using that
library implementation incompatible with code from "normal"
implementations.

Looking up the Standard''s spec for c_str (21.3.6), I didn''t see anything
that explicitly disallows allocating a copy (I perhaps just don''t know
where to look), but the way it says the pointer will become invalidated
after any call to a non-const string member function (on the associated
string) pretty much tells the tale.




basic_string对象可以删除这些点的缓冲区,如果它分配了一个,或者它可以等到下一次调用c_str或直到

销毁。当然,并非任何人都这样做。但是想法

是可以实现basic_string来保存其文本在

非连续的块中,并且只需要收集它们的内容

一起打电话给c_str。


-


Pete Becker

Dinkumware,Ltd。(< a rel =nofollowhref =http://www.dinkumware.comtarget =_ blank> http://www.dinkumware.com )

这篇关于string :: c_str()通常是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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