修改C ++字符串对象的基础char数组 [英] Modifying underlying char array of a c++ string object

查看:105
本文介绍了修改C ++字符串对象的基础char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的:

string s = "abc";
char* pc = const_cast<char*>( s.c_str() );
pc[ 1 ] = 'x';
cout << s << endl;

当我使用GCC编译以上代码段时,得到的结果"axc" 符合预期.我的问题是,以这种方式修改C ++字符串的基础char数组是否安全且可移植?还是可能有其他方法直接处理字符串数据?

When I compiled the snippet above using GCC, I got the result "axc" as expected. My question is, is that safe and portable to modify the underlying char array of a C++ string in this way? Or there might be alternative approaches to manipulate string's data directly?

仅供参考,我的意图是编写一些可以由C和C ++调用的纯C函数,因此,它们只能接受char*作为参数.从char*到字符串,我知道涉及复制,这是不利的.因此,有人可以提出一些建议来应对这种情况吗?

FYI, my intention is to write some pure C functions that could be called both by C and C++, therefore, they can only accept char* as arguments. From char* to string, I know there is copying involved, the penalty is unfavorable. So, could anybody give some suggestions to deal with this sort of situation.

推荐答案

对于第一部分,c_str()返回const char*,这表示它的意思.在这种情况下,const_cast所能实现的全部就是编译未定义的行为.

To the first part, c_str() returns const char* and it means what it says. All the const_cast achieves in this case is that your undefined behavior compiles.

第二部分,保证C ++ 0x中的std::string具有连续存储,就像C ++ 03中的std::vector一样.因此,只要字符串不为空,就可以使用&s[0]获取char*传递给函数.实际上,当前正在积极开发中的所有string实现都已经连续存储:在标准委员会会议上进行了一次草率调查,没有人提供反例.因此,您可以根据需要立即使用此功能.

To the second part, in C++0x std::string is guaranteed to have contiguous storage, just like std::vector in C++03. Therefore you could use &s[0] to get a char* to pass to your functions, as long as the string isn't empty. In practice, all string implementations currently in active development already have contiguous storage: there was a straw poll at a standard committee meeting and nobody offered a counter-example. So you can use this feature now if you like.

但是std::string使用的字符串格式与C风格的字符串根本不同,即它是data + length,而不是nul终止.如果您从C函数中修改字符串数据,则无法更改字符串的长度,并且不能确保没有c_str()的末尾会有一个nul字节.并且std::string可以包含嵌入的nul,它们是数据的一部分,因此即使您确实找到了nul,也不知道长度,您仍然不知道已找到字符串的结尾.您对在两种不同类型的数据上都能正确运行的功能所能做的工作非常有限.

However, std::string uses a fundamentally different string format from C-style strings, namely it's data+length rather than nul-terminated. If you modify the string data from your C functions, then you can't change the length of the string and you can't be sure there's a nul byte at the end without c_str(). And std::string can contain embedded nuls which are part of the data, so even if you did find a nul, without knowing the length you still don't know that you've found the end of the string. You're very limited what you can do in functions that will operate correctly on both different kinds of data.

这篇关于修改C ++字符串对象的基础char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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