将const char *返回为char *,然后更改数据 [英] returning const char* to char* and then changing the data

查看:148
本文介绍了将const char *返回为char *,然后更改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码感到困惑:

I am confused about the following code:

string _str = "SDFDFSD";
char* pStr = (char*)_str.data();
for (int i = 0; i < iSize; i++)
    pStr[i] = ::tolower(pStr[i]);

此处_str.data()返回const char*.但是,我们将其分配给char*.我的问题是,

here _str.data() returns const char*. But we are assigning it to a char*. My questions is,

_str.data()返回指向常量数据的指针.如何将其存储在指向数据的指针中?数据是恒定的,对不对?如果将其分配给char指针,则可以像在for语句中那样对它进行更改,而对于恒定数据则不可能.

_str.data()is returning pointer to a constant data. How is it possible to store it in a pointer to data? The data was constant right? If we assign it to char pointer than we can change it like we are doing inside the for statement which should not be possible for a constant data.

推荐答案

您正在做的事情在标准库级别(您违反了std::string contract)是无效的,但是在C ++核心语言级别是有效的.

What you are doing is not valid at the standard library level (you're violating std::string contract) but valid at the C++ core language level.

不应写入从data返回的char *,因为例如在理论上可以在具有相同值的不同字符串之间共享它(*).

The char * returned from data should not be written to because for example it could be in theory(*) shared between different strings with the same value.

如果要修改字符串,只需使用std::string::operator[],它将告知对象意图,并且会在最初共享该字符串的情况下为特定实例创建专用缓冲区.

If you want to modify a string just use std::string::operator[] that will inform the object of the intention and will take care of creating a private buffer for the specific instance in case the string was originally shared instead.

从技术上讲,您可以从指针或引用中删除常量性,但是是否有效是取决于特定情况的语义.之所以允许执行该操作,是因为C ++的主要理念是程序员不会犯任何错误,并且知道自己在做什么.例如,从C ++语言的角度来看,从技术上讲memcpy(&x, "hello", 5)是合法的,其中x是类实例,但是结果很可能是未定义的行为".

Technically you are allowed to cast-away const-ness from a pointer or a reference, but if it's a valid operation or not depends on the semantic of the specific case. The reason for which the operation is allowed is that the main philosophy of C++ is that programmers make no mistakes and know what they are doing. For example is technically legal from a C++ language point of view to do memcpy(&x, "hello", 5) where x is a class instance, but the results are most probably "undefined behavior".

如果您认为您的代码有效",那是因为您对有效"的真正含义有错误的理解(提示:有效"并不意味着有人曾经观察过代码在做合理的事情,但是在所有情况下都可以使用).如果运行该程序,有效的C ++实现可以自由地执行其想要的任何事情:观察到您认为不错的东西实际上并不意味着什么,可能是您看起来不够近,或者您很幸运(不幸的是,实际上)没有崩溃立即发生.

If you think that your code "works" it's because you've the wrong understanding of what "works" really should mean (hint: "works" doesn't mean that someone once observed the code doing what seemed reasonable, but that will work in all cases). A valid C++ implementation is free to do anything it wants if you run that program: that you observed something you think is fine doesn't really mean anything, may be you didn't look close enough, or may be you were just lucky (unfortunate, actually) that no crash happened right away.

(*)在现代,std :: string的COW(写时复制)实现不受欢迎,因为它们带来了很多问题(例如多线程),并且内存现在便宜很多.仍然std::string合同规定您不能更改data()的返回值所指向的内存.如果您做任何事情都可能发生.

(*) In modern times the COW (copy-on-write) implementations of std::string are low in popularity because they pose a lot of problems (e.g. with multithreading) and memory is a lot cheaper now. Still std::string contract says you're not allowed to change the memory pointed by the return value of data(); if you do anything may happen.

这篇关于将const char *返回为char *,然后更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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