[LAD] Bit OT: cleaning code for gcc4

Florian Schirmer florian.schirmer at native-instruments.de
Thu Jun 19 15:27:45 UTC 2008


Hi,

Julien Claassen wrote:
> Hi!
>   The errors on lines 318 and 319 disappeared. So the Char** are 
> correctly declared, now as const char**. BUT now I get an error on the 
> next line, because the
> newCDKSelection function from the CDK doesn't accept const char**, but 
> char** and this is not deprecated, but an "invalid conversion".

To be 100% correct you have to make a copy first!

char *title = strdup("my title");
newCDKSelection([parameters],title,[more parameters]);
....
free(title);

should do the trick.

If you can guarantee that newCDKSelection() never ever changes the 
contents of the string you could just cast way the const:

const char *title = "my title";
newCDKSelection([parameters],const_cast<char *>(title),[more parameters]);


>   How did the GCC people think I could avoid such things.

gcc is not the problem here. Your code is wrong. So it is in fact not a 
warning but a fundamental problem :-) See below...


>   Note: The newCDKSelection also takes a char* parameter, which of 
> course is converted in a deprecated manner if I write: 
> newCDKSelection([parameters],"my title",[more parameters]); 

String literals ("my title") in C++ are by definition const char * 
pointers. Why? Because they are subject to string pooling for example.

const char *foo = "Hello world!";
const char *bar = "Hello world!";

Depending on the optimization of the compiler foo and bar might point to 
the same location.

strcpy((char *)foo, "Bad idea");

... is a bad idea because it will change foo and bar. Depending on the 
compiler you might also crash because the strings could be in a read 
only section.


> So I tried 
> this:
> char* title;
> strcpy("my title",title);
>   This still won't work correctly. 

Because it should be the other way round:

strcpy(title, "my title");

Please note that you have to allocate memory for title first. strdup() 
is the function you're really looking for.


>I guess I can live with those 
> warnings, but the two relevant questions are:
> 1. How long?

Until next version of gcc maybe? :-)


> 2. Do I like to live with it?

Probably you should just fix the bugs ;-)


>   ah does this have to be so complicated? :-(
>   Kindest regards and still all suggestions are wellcome!

Are you sure you need to compile your code using g++? If you don't use 
any c++ then just use gcc and the problem will magically go away because 
strings in C are plain char * pointers.

Best,
   Florian



More information about the Linux-audio-dev mailing list