当将std :: string转换为char *的C函数时,需要注意什么? [英] What to watch out for when converting a std::string to a char* for C function?

查看:194
本文介绍了当将std :: string转换为char *的C函数时,需要注意什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多帖子,问如何转换一个C ++ std :: string const std :: string& char * 将它传递给C函数,似乎有很多注意事项。人们必须注意字符串是连续的和很多其他的东西。关键是我从来没有真正理解所有需要注意的点,为什么



我想知道是否有人可以总结一个从 std :: string 转换为
$ b

std :: string 是一个 const 引用,当它只是一个非const引用,当C函数将改变<$ c $

解决方案

首先,无论是const引用还是const引用,值不会改变任何东西。



然后你必须考虑函数的期望。有
是一个函数可以使用 char *
a 不同的东西 ---原始版本的 memcpy ,对于
示例,使用这些类型,可能仍有
这样的代码。它是,希望是罕见的,在下面的
我将假设C函数中的 char * 引用'\\ \\ 0'
终止字符串。



如果C函数使用 char const * ,你可以传递
的结果 std :: string :: c_str();如果它需要一个 char * ,它
依赖。如果只需要一个 char * ,因为它起始于C的
前 - const 天,实际上它没有修改,
std :: string :: c_str()后面跟一个 const_cast
是合适的。然而,如果C函数使用 char * 作为out
参数,事情变得更加困难。我个人认为
喜欢声明一个 char [] 缓冲区,传递这个,然后
将结果转换为 std :: string ,但所有已知的
实现 std :: string 使用连续缓冲区,
使用下一版本的标准将需要它,所以正确
确定 std :: string 的大小(使用
std :: string :: resize ),然后传递& s [0] ,然后
将字符串重新设置为结果长度也可以使用 strlen(s.c_str()),如果有必要)。



也是一个问题,C程序使用
char [] ),你必须考虑任何生命周期的问题大多数
函数采取 char * char const * 只需使用
指针,并将其忽略,但如果函数保存指针
某处,为了以后使用,字符串对象必须至少生活
长,并且其大小不应该在此期间修改。
(在这种情况下,我更喜欢使用 char [] 。)


I have read many posts asking the question on how to convert a C++ std::string or const std::string& to a char* to pass it to a C function and it seems there is quite a few caveat's in regards to doing this. One has to beware about the string being contiguous and a lot of other things. The point is that I've never really understood all the points one needs to be aware of and why?

I wondered if someone could sum up the caveats and downfalls about doing a conversion from a std::string to a char* that is needed to pass to a C function?

This when the std::string is a const reference and when it's just a non-const reference, and when the C function will alter the char* and when it will not alter it.

解决方案

First, whether const reference or value doesn't change anything.

You then have to consider what the function is expecting. There are different things which a function can do with a char* or a char const*---the original versions of memcpy, for example, used these types, and it's possible that there is still such code around. It is, hopefully, rare, and in the following, I will assume that the char* in the C function refer to '\0' terminated strings.

If the C function takes a char const*, you can pass it the results of std::string::c_str(); if it takes a char*, it depends. If it takes a char* simply because it dates from the pre-const days of C, and in fact, it modifies nothing, std::string::c_str() followed by a const_cast is appropriate. If the C function is using the char* as an out parameter, however, things become more difficult. I personally prefer declaring a char[] buffer, passing this, and then converting the results to std::string, but all known implementations of std::string use a contiguous buffer, and the next version of the standard will require it, so correctly dimensioning the std::string first (using std::string::resize(), then passing &s[0], and afterwards redimensionning the string to the resulting length (determined using strlen(s.c_str()), if necessary) can also be used.

Finally (but this is also an issue for C programs using char[]), you have to consider any lifetime issues. Most functions taking char* or char const* simply use the pointer, and forget it, but if the function saves the pointer somewhere, for later use, the string object must live at least as long, and its size should not be modified during that period. (Again, in such cases, I prefer using a char[].)

这篇关于当将std :: string转换为char *的C函数时,需要注意什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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