C ++警告:不推荐从字符串常量转换为'char *'[-Wwrite-strings] [英] C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

查看:10080
本文介绍了C ++警告:不推荐从字符串常量转换为'char *'[-Wwrite-strings]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gnuplot在C ++中绘制图形。图表按预期绘制,但在编译期间有警告。警告是什么意思?

 警告:不推荐从字符串常量转换为char *[-Wwrite-strings] 

这是我使用的函数:

  void plotgraph(double xvals [],double yvals [],int NUM_POINTS)
{
char * commandsForGnuplot [] = {set title \Probability Graph \\,
plot'data.temp'用行};
FILE * temp = fopen(data.temp,w);
FILE * gnuplotPipe = popen(gnuplot -persistent,w);
int i;
for(i = 0; i {
fprintf(temp,%lf%lf \\\
,xvals [i],yvals [i]) ;
//将数据写入临时文件
}
for(i = 0; i {
fprintf(gnuplotPipe, %s \\\
,commandsForGnuplot [i]);
//逐个向gn uplot发送命令。
}
fflush(gnuplotPipe);
}


解决方案

字符串文字是 array of const char ,我们可以从草稿C ++标准部分 2.14.5 看到这一点, > emphasis mine ):

普通字符串文字和UTF-8字符串文字也被称为窄字符串文字。窄字符串字符串具有类型n const char的数组,其中n是下面定义的字符串的大小,并且具有静态存储持续时间(3.7)。


因此此更改将删除以下警告:

  commandsForGnuplot [] = {set title \Probability Graph \,plot'data.temp'with lines}; 
^^^^^

注意,允许一个< 是 来指向 const //en.wikipedia.org/wiki/Undefined_behavior\">未定义的行为。我们可以通过转到 7.1.6.1 部分cv-qualifiers 部分:


除了任何类成员声明mutable(7.1.1)可以被修改,
任何尝试修改const对象在其生命周期(3.8)结果
in undefined行为。


2.14.5 em>它说:


是否所有字符串字面值是不同的(即存储在
非重叠对象) 。
尝试修改字符串文字的效果未定义。



I am using gnuplot to draw a graph in C++. The graph is being plot as expected but there is a warning during compilation. What does the warning mean?

warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

This is the function I am using:

void plotgraph(double xvals[],double yvals[], int NUM_POINTS)
{
    char * commandsForGnuplot[] = {"set title \"Probability Graph\"", 
        "plot     'data.temp' with lines"};
    FILE * temp = fopen("data.temp", "w");
    FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w");
    int i;
    for (i=0; i < NUM_POINTS; i++)
    {
        fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); 
        //Write the data to a te  mporary file
    }
    for (i=0; i < NUM_COMMANDS; i++)
    {
        fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); 
        //Send commands to gn  uplot one by one.
    }
    fflush(gnuplotPipe);
}

解决方案

String literals are an array of const char, we can see this from the draft C++ standard section 2.14.5 String literals which says (emphasis mine):

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type "array of n const char", where n is the size of the string as defined below, and has static storage duration (3.7).

so this change will remove the warning:

const char * commandsForGnuplot[] = {"set title \"Probability Graph\"", "plot     'data.temp' with lines"};
^^^^^

Note, allowing a non-const char* to point to const data is a bad idea since modifying a const or a string literal is undefined behavior. We can see this by going to section 7.1.6.1 The cv-qualifiers which says:

Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

and section 2.14.5 String literals which says:

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.

这篇关于C ++警告:不推荐从字符串常量转换为'char *'[-Wwrite-strings]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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