Re: Problem with "warning: deprecated conversion from string constant to ‘char*’"
Jens Rehsack
rehsack@googlemail.com
Tue Oct 12 16:50:00 GMT 2010
2010/10/12 Ian Lance Taylor <iant@google.com>:
> Jens Rehsack <rehsack@googlemail.com> writes:
>
>> 2010/10/12 Ian Lance Taylor <iant@google.com>:
>>> Jens Rehsack <rehsack@googlemail.com> writes:
>>>
>>>> I currently write some c++ code using libConfuse
>>>> (http://savannah.nongnu.org/projects/confuse/).
>>>> During initialisation of the required cfg_opt_t structures I get a
>>>> (very) lot of warnings like:
>>>> c2.cpp:11: warning: deprecated conversion from string constant to ‘char*'
>>>>
>>>> I attached a small example to demonstrate the problem. It's not
>>>> trivial - changing the "name" member
>>>> of struct demo being const, free failes (and is invalid, because
>>>> there're demo_opt instances with
>>>> modifyable names).
>>>>
>>>> Any suggestions welcome.
>>>> 1) Killing the warning with -Wno-... is not an option
>>>> 2) Remove the finding using const_cast<char *>() is the same as (1)
>>>> 3) initializing the struct using strdup is neither reasonable
>>>
>>> I'm not sure what you want us to suggest. You say that the names are
>>> modifiable.
>>
>> Where do I say that? I apologize saying this (must be happened because
>> of my not so good english).
>
> "changing the "name" member of struct demo being const, free failes (and
> is invalid, because there're demo_opt instances with modifyable names)."
>
> I apologize if I misread that sentence.
I tried to say (and you can easily verify by modify the attached example of
the original post): when I modify the structure to have "const char *name"
members like:
struct demo_opt {
const char *name;
int flags;
};
c1.c: demo_free() fails in line 52.
>>> String constants are not modifiable. If the strings are
>>> indeed modified, and you pass in a string constant, then your program is
>>> going to crash when the library tries to modify the strings. The gcc
>>> warning is helpfully pointing that out.
>>
>> Correct. And if the strings are not modifiable? (I thought I provided an
>> example which shows, that they neither are assumed being modifiable
>> nor being modified).
>>
>>> This is not a gcc issue. It's a C++ language issue. If you need a
>>> modifiable string, then you must not use a string constant.
>>
>> How can this being expressed without declaring dozen of helper
>> variables?
>
> I am not sure that I understand what you are asking. Can you provide a
> short, standalone, bit of code showing the problem?
Attached at the first mail :)
Or is it to long?
> Is there any reason to think that this is a gcc issue rather than a C++
> issue?
I don't know. I encounter this problem first time - and I don't have another
c++ compiler to test if they deal better. It's likely a c++ language issue, but
I can't imagine that there is no way out (or better I hope that there is a way
to deal with such situations).
/Jens
More information about the Gcc-help
mailing list