This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::setlocale(...) return value ??


No need to be rude Liu, a simple * check for an error * would have suffice, nonetheless, thank you for the time you have taken to read my question and for providing such an insightful tip. I will make sure there is no error returned by std::setlocale(...) and let you know how things went.
Again, I truly appreciate your help.


On 04-May-2017 4:36 AM, Liu Hao wrote:
On 2017/5/4 16:33, Papa wrote:
According to the C++ Reference setlocale(...) returns a 'char*',
however, when trying to assign this function's return value to another
'char*', it creates an run time error.
Here is my example:

<snip>
void jme::Locale::setGlobalLocale(const std::string& str){
     char* arg = new char[str.length()+1];
     strcpy(arg, str.c_str()); // const char* to char* -- Works
     char* cp = new char[1024];
     strcpy(cp, std::setlocale(LC_ALL, arg)); // char* to char* --  Does
not work ??

    delete arg;
    delete cp;
}
</snip>
The code compiles, but as I said before it does not run.

What am I missing? How can I solve this problem?


This is a RTFM question. `setlocale()` returns a null pointer in case of failure and in that case you are passing a null pointer to `strcpy()`.


--
ArbolOne.ca
Using FireFox and Thunderbird.
ArbolOne is composed of a group of students and volunteers
dedicated to providing free services to charitable organizations.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]