Compiling legacy code... Is this a bug???
Bill C Riemers
bcr@feynman.com
Thu Feb 10 19:44:00 GMT 2000
I really doubt what I'm seeing is a bug, because it is far
too obvious not to have been noticed. But I can't believe
the C++ standard allows this either. Consistently when
I compile legacy code I'm given the following type of
warning:
doc2tex.l:480: warning: for conversion from `McString' to `const char *'
doc2tex.l:480: warning: because conversion sequence for the argument is better
doc2tex.l:482: warning: choosing `McDArray<char>::operator char *()' over `McDArray<char>::operator const char *() const
This makes absolutely no sense to me, as it almost always results in
a memory leak. Since the typical usage is:
const char message[]="This is a constant string";
const char *foo()
{
return message;
}
char *foo()
{
char *ptr=new char [sizeof(message)];
strcpy(ptr,message);
return message;
}
printf("The original message is '%s'\n",(const char *)foo());
char *bar=foo();
for(char *ptr=bar;*ptr;*ptr=toupper(*ptr));
printf("The new message is '%s'\n",ptr);
delete [] bar;
As you can see, if the wrong type of pointer is used for the first call of
foo(), then there is a memory leak.
I have absolutely no problem avoiding this type of problem in code I write,
since I would not use the same name for both functions, and I never define
both a constant and non-constant version of the same operator. However,
I am lost to confuse as to how I'm suppose to compile other people's code
written for older versions of g++, or other C++ compilers that don't do
this type of thing.
Bill
The
More information about the Gcc-help
mailing list