高效,安全地处理“char *” [英] Efficient, safe handling of "char *"

查看:83
本文介绍了高效,安全地处理“char *”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我写了一个使用Berkeley DB的ISAPI认证模块,

希望它尽可能高效。


ISAPI和BerkeleyDB都使用字符数组(char *)

来传递和接收信息。


我知道C ++字符串应该是正确的

处理字符串但我怀疑转换char *的方式串行

每次我处理它都是昂贵的(分配内存,复制

内容),特别是当很多次我必须转换

字符串返回" char *" (使用c_str())将其传递给

另一方或返回结果。


是否有已知的类库可以帮助我:


1.创建一个接受char *的对象。 (

以null结尾或使用长度参数)

2.将此字符数组表示为STL容器(提供

迭代器,大部分,但也允许通过迭代器修改数组



3.提供原始char *的句柄。参数


没有复制字节数组?


我用Google搜索并且Boost似乎是一个可能的候选人,但它的

文档有点零星,我很难弄明白

什么以及如何使用它。


谢谢,

--V

解决方案

Vaca Louca写道:

您好,

我编写了一个使用Berkeley DB的ISAPI认证模块,并希望它尽可能高效。

ISAPI和BerkeleyDB都使用字符数组(char *)
传递和接收信息。

我知道C ++字符串应该是正确的字符串。处理字符串的方法,但我怀疑转换char *每次处理它时都很昂贵(分配内存,复制内容),特别是当我多次将
字符串转换回char *时" (使用c_str())将其向前传递到
另一侧或返回结果。



你不应该直接在任何地方传递c_str()的返回指针。


是否有已知的类库能以某种方式帮助我:

1.创建一个接受char *的对象。 (
以null结尾或使用长度参数)
2.将此字符数组表示为STL容器(主要提供
迭代器,但也允许修改数组通过迭代器)
3.提供原始char *的句柄。参数



std :: string。 :-)只需将一个字符串对象放在一个名称空间(或全局)作用域中,或者在局部函数作用域中将其作为静态成员或作为类的静态成员。

我将从命名空间范围开始。在这种情况下,您可以将返回的

指针作为const char *传递给c_str()。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


Ioannis Vranos写道:

我将从命名空间范围开始。在这种情况下,您可以将返回的c_str()指针作为const char *传递。



....而字符串保持不变。

-

Ioannis Vranos

http://www23.brinkster.com/noicys


Vaca Louca写道:

你好,

我编写了一个使用Berkeley DB的ISAPI认证模块,并希望它尽可能高效。

ISAPI和BerkeleyDB都使用字符数组(char *)<传递和接收信息。

我知道C ++字符串应该是正确的。处理字符串的方法,但我怀疑转换char *每次处理它时都很昂贵(分配内存,复制内容),特别是当我多次将
字符串转换回char *时" (使用c_str())将其向前传递到
另一侧或返回结果。


#1。你可能是对的 - 但你有没有对代码进行分析,你是否知道这将是一个问题。


#2。你考虑过懒字符串吗?仅在必要时转换为std :: string

。 ie构造函数在完成任何操作时都会使用const char *和

,而不仅仅是使用char *它将
转换为std :: string。 />
是否有已知的类库可以帮助我以某种方式:

1.创建一个接受char *的对象。 (
以null结尾或使用长度参数)
2.将此字符数组表示为STL容器(主要提供
迭代器,但也允许修改数组通过迭代器)
3.提供原始char *的句柄。参数

没有复制字节数组?


我写了很多这些。

我google了一下,Boost似乎是一个可能的候选人,但它的
文档有点零星的,我正在努力弄清楚
是什么以及如何使用它。




它是什么?


Hello,

I write an ISAPI authentication module which uses Berkeley DB and
want it to be as efficient as possible.

Both ISAPI and BerkeleyDB use arrays of chars (char *)
to pass and receive information.

I know that C++ strings are supposed to be the "right" way to
handle strings but I suspect that converting "char *" to string
every time I deal with it is costly (allocate memory, copy the
content), especially when many times I''ll have to convert the
string back to "char *" (using c_str()) to pass it onward to the
other side or return a result.

Are there known class libraries which can help me somehow:

1. create an object which accepts a "char *" (either
null-terminated or with a length argument)
2. represent this array of chars as an STL container (provide
iterators, mostly, but also allow modification of the array
through the iterators)
3. provides a handle to the original "char *" argument

without copying over the array of bytes?

I googled around and Boost seems like a likely candidate, but its
documentation is a bit sporadic and I''m struggling to figure out
what and how to use it.

Thanks,

--V

解决方案

Vaca Louca wrote:

Hello,

I write an ISAPI authentication module which uses Berkeley DB and
want it to be as efficient as possible.

Both ISAPI and BerkeleyDB use arrays of chars (char *)
to pass and receive information.

I know that C++ strings are supposed to be the "right" way to
handle strings but I suspect that converting "char *" to string
every time I deal with it is costly (allocate memory, copy the
content), especially when many times I''ll have to convert the
string back to "char *" (using c_str()) to pass it onward to the
other side or return a result.

You shouldn''t pass the returned pointer of c_str() anywhere directly.

Are there known class libraries which can help me somehow:

1. create an object which accepts a "char *" (either
null-terminated or with a length argument)
2. represent this array of chars as an STL container (provide
iterators, mostly, but also allow modification of the array
through the iterators)
3. provides a handle to the original "char *" argument


std::string. :-) Just place a string object in a namespace (or global) scope, or make it
static in a local function scope or as a static member of a class.

I would begin with the namespace scope. And in this case you could pass the returned
pointer of c_str() as a const char * around.

--
Ioannis Vranos

http://www23.brinkster.com/noicys


Ioannis Vranos wrote:

I would begin with the namespace scope. And in this case you could pass
the returned pointer of c_str() as a const char * around.


.... while the string remains unmodified.
--
Ioannis Vranos

http://www23.brinkster.com/noicys


Vaca Louca wrote:

Hello,

I write an ISAPI authentication module which uses Berkeley DB and
want it to be as efficient as possible.

Both ISAPI and BerkeleyDB use arrays of chars (char *)
to pass and receive information.

I know that C++ strings are supposed to be the "right" way to
handle strings but I suspect that converting "char *" to string
every time I deal with it is costly (allocate memory, copy the
content), especially when many times I''ll have to convert the
string back to "char *" (using c_str()) to pass it onward to the
other side or return a result.
#1. You may be right - but have you profiled the code and do you "know"
that this will be an issue.

#2. Have you considered a "lazy string" that converts to std::string
only when you have to. i.e. The constructor takes a const char * and
when any operation is done that is more than simply using a char * it is
converted to std::string.

Are there known class libraries which can help me somehow:

1. create an object which accepts a "char *" (either
null-terminated or with a length argument)
2. represent this array of chars as an STL container (provide
iterators, mostly, but also allow modification of the array
through the iterators)
3. provides a handle to the original "char *" argument

without copying over the array of bytes?
I''ve written many of these.

I googled around and Boost seems like a likely candidate, but its
documentation is a bit sporadic and I''m struggling to figure out
what and how to use it.



What is it ?


这篇关于高效,安全地处理“char *”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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