私はいくつかのレガシーコードに取り組んでおり、cppファイルにいくつかの変更を加える必要があります.cppファイルにはextern "c"ブロックにコード全体が含まれています-
char* を返す関数を更新しました。コードは以下の func1() のようになります。私は std::strring と stringstream を使用しているので、extern ブロックの前に sstream と string ヘッダー ファイルを含めました。以下の関数は、c ファイルと cpp ファイルの両方から呼び出されます。したがって、ここで std::string を返すことはできません -
char* func1(someStruct* pt){
std::strig nam = somefunc(pt);
//have to append some integer in particular format
std::stringstream ss;
ss<<nam<<pt->int1 ......;
nam = ss.str();
//More code here for returning char* based on queries - (a)
}
この関数が呼び出される場所の 1 つで -
void otherFunc(.....){
//......
char* x = func(myptr);
if(based_on_some_condition){
char* temp = func3(x); //returns a char* to dynamically allocated array.
strcpy(x,temp); //copying (b)
}
//..........
}
以下は私のクエリです
-1)(a)で、次の2つの形式でchar *を返すことができます.(b)でコピーしても未定義の動作が発生しないように決定する必要があります-
i)Create a char array dynamically with size = nam.length()+10 (extra 10 for some work happening in func3).<br>
char* rtvalue = (char*)calloc(sizeof(char),nam.length()+10);
strcpy(rtvalue,nam.c_str());
return rtvalue;
And free(temp); in otherFunc() after strcpy(x,temp);
ii) Declare 'nam' as static std::string nam;
and simply return const_cast<char*>(nam.c_str());
Will defining 'nam' with static scope ensure that a correct return happen from function (ie no dangling pointer at 'x')?
More importantly, can I do this without worrying about modification happening at (b).
どちらがより良い解決策ですか?