-
deprecated char *
Code:
static char *abortMsg[] = {
NULL,
"Invalid command line arguments",
"Failed to open source file",
"Failed to open intermediate form file",
"Failed to open assembly file",
"Too many syntax errors",
"Stack overflow",
"Code segment overflow",
"Nesting too deep",
"Runtime error",
"Unimplemented feature",
};
In another thread I was given the advice to get rid of char * and use string type. However, in the above code from my old project I get warnings about this conversion. The recommendation was to change everything to string. I don't see how to do this, but another recommendation that worked and got rid of the warnings was to change to static const char* [etc]. I don't see how to use string or string_view to fix these warnings.
Any advice appreciated.
maxcy/wt1v
-
Like this?
Code:
#include <string>
static std::string abortMsg[] = {
NULL,
"Invalid command line arguments",
"Failed to open source file",
"Failed to open intermediate form file",
"Failed to open assembly file",
"Too many syntax errors",
"Stack overflow",
"Code segment overflow",
"Nesting too deep",
"Runtime error",
"Unimplemented feature",
};
Changing to const char * is also fine, since there is never any intention to try and change the strings.
> The recommendation was to change everything to string.
That's more appropriate when you're trying to do your own memory management in some form or other.
Examples.
Code:
char buff[SIZE];
strcpy(buff,"something");
char *buff = (char*)malloc(SIZE); // not a good thing to use in C++
strcpy(buff,"something");
free(buff);
char *buff = new[SIZE];
strcpy(buff,"something");
delete [] buff;
If you're ever in the position of having to calculate (or hope/guess) the value for SIZE, or in any way forgetful about freeing memory after you've allocated it, then std::string is your friend.