有什么需要注意的字符串转换一个std ::时为char *的C函数呢? [英] What to watch out for when converting a std::string to a char* for C function?

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

问题描述

我已经看了很多帖子,询问如何在C ++ 的std :: string的常量标准::字符串&功放转换的问题; 的char * 将它传递给一个C函数,它似乎有不少需要注意的的问候这样做。一个人要提防有关字符串是连续的和很多其他的事情。问题的关键是,我从来没有真正理解人们需要知道的所有点和为什么

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?

我想知道,如果有人可以总结告诫和挫折有关从的std ::字符串的char *做一个转换是需要传递给C函数?

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?

这当的std :: string的常量的参考,当它只是一个非const引用,当C函数将改变的char * ,当它不会改变它。

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.

您接下来要考虑什么功能期待。那里
是不同的东西一个功能可以用的char *做
一个字符常量* ---原始版本的memcpy ,对于
例如,使用这些类型,它可能仍有
例如code左右。它是有希望,稀有,并且在下面,
我将假设的char * 的C函数参考'\\ 0'
结尾的字符串。

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.

如果C函数需要一个字符常量* ,你可以通过它的
结果的std ::字符串:: c_str();如果它需要一个的char * ,它
依靠。如果需要一个的char * ,只是因为它的日期
pre - 常量天的C,而事实上,它会修改什么,
的std ::字符串:: c_str()后跟一个的const_cast
适当。如果C函数使用的char * 作为out
参数,但是,事情就变得更加困难。我个人
preFER声明的char [] 缓冲,通过这个,然后
转换结果的std ::字符串,但所有已知的
的实现的std ::字符串使用连续的缓冲区,并
该标准的下一个版本将需要它,所以正确
标注的的std ::字符串第一(使用
的std ::字符串::调整(),然后通过&放大器; S [0] ,事后
redimensionning字符串向所得的长度(确定
使用的strlen(s.c_str()),如果需要的话)也可以使用。

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.

最后(不过这也是使用C程序的问题
的char [] ),你必须要考虑的问题,终生。最
功能服用的char * 字符常量* 只需使用
指针,忘记它,但如果函数保存指针
某处,以备后用,该字符串对象必须住在至少
一样长,并且其大小不应当在此期间进行修改。
(同样,在这种情况下,我使用preFER一个的char []

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 ::时为char *的C函数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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