开发者

Function returns char *

开发者 https://www.devze.com 2023-04-04 06:05 出处:网络
Is it correct to write a function that returns开发者_StackOverflow中文版 char *? Something like this:

Is it correct to write a function that returns开发者_StackOverflow中文版 char *?

Something like this:

char * func()
{  
 char *c = new char[3];
 strcpy(c, "hi");
 c[2] = '\0';
 return c;
}

int main()
{
 char *c;
 c = func();
 //code using c
 delete [] c;
 return 1;
}

It works, but is it correct?


It is correct (assuming you meant strcpy and not strlen), but it must be very clear from the documentation of the function that it's the caller's responsibility to free the returned pointer, and what method does it have to use to free it (new => delete, new [] => delete[], malloc => free, etc). That is the "C way" of doing it.

This relies on users of your function (including you after several months you wrote it) to read the documentation to get things right, so it's quite error prone; also, in C++ there are several more complications that are not present in C (namely: exceptions) that make using raw pointers in these contexts not such a good idea.

That's the reason why you normally should return classes that encapsulate resources (e.g. std::string in this case, and in general containers that manage the memory automatically) or ownership-transferring smart pointers, that also have the advantage of being exception-safe (which your code is not).

This sounds extremely complicated, but in fact it's not:

#include <string>

std::string func()
{  
   return "hi"; // actually, to be more explicit it should be return std::string("hi")
}

int main()
{
    std::string c;
    c=func();
    return 1;
}

That's it, no need to worry about allocations/deallocations and exceptions, all this is automatically handled by the std::string class, so you can manage strings almost as they were builtin types.


Well, that works. The memory is allocated on the heap and is deallocated correctly. There is no need to manually add a null-terminator since strcpy does so.

But it's not really C++. In C++ you would use std::string.


It works, but is it correct?

Your code is correct.

However, it depends on the context, if it's a good programming practice or not. For this minimal program you posted, I will prefer not to use new[] and delete[]. But would rather rely on std::string.

Demo.


Your code is technically correct. However, I think most SO-ers here have reservations on whether it's considered "good" code or not.

For starters:

  • Pick meaningful variable/function names. c and func are not very descriptive and make it difficult for others to understand what you're trying to do
  • Generally you should have a corresponding function which cleans up any resources that your function has allocated.
  • Prefer strncpy() over strcpy() to prevent unexpected buffer overflows (or look into the String class)


try:

char * func()
{  
 char * c = new char[3];
 strcpy(c,"hi");
 return c;
}

int main()
{
 char *c;
 c = func();
 //code using c
 delete [] c;
 return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消