处理返回的 C 字符串
在 C 中编写返回字符串的方法时,什么被认为是更好的做法?
传入缓冲区和大小:
void example_m_a(type_a a,char * buff,size_t buff_size)
或制作并返回适当大小的字符串:
char * example_m_b(type_a a)
PS 您认为返回缓冲区 ptr 以允许分配样式和 嵌套函数调用即
char * example_m_a(type_a a,char * buff,size_t buff_size)
{
...
return buff;
}
What is considered better practice when writing methods that return strings in C?
passing in a buffer and size:
void example_m_a(type_a a,char * buff,size_t buff_size)
or making and returning a string of proper size:
char * example_m_b(type_a a)
P.S. what do you think about returning the buffer ptr to allow assignment style and
nested function calls i.e.
char * example_m_a(type_a a,char * buff,size_t buff_size)
{
...
return buff;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将缓冲区作为参数传递可以解决此类代码可能遇到的大多数问题。
如果它返回指向缓冲区的指针,那么您需要决定如何分配它以及调用者是否负责释放它。该函数可以返回不需要释放的静态指针,但它不是线程安全的。
Passing a buffer as an argument solves most the problems this type of code can run into.
If it returns a pointer to a buffer, then you need to decide how it is allocated and if the caller is responsible for freeing it. The function could return a static pointer that doesn't need to be freed, but then it isn't thread safe.
传递缓冲区和大小通常不太容易出错,特别是如果字符串的大小通常为“合理”大小。如果动态分配内存并返回指针,则调用者负责释放内存(并且必须记住根据函数分配内存的方式使用相应的内存释放函数)。
如果您检查大型 C API(例如 Win32),您会发现几乎所有返回字符串的函数都使用第一种形式,其中调用者传递缓冲区和大小。只有在有限的情况下,您可能会找到函数分配返回值的第二种形式(我目前想不出任何形式)。
Passing a buffer and a size is generally less error-prone, especially if the sizes of your strings are typically of a "reasonable" size. If you dynamically allocate memory and return a pointer, the caller is responsible for freeing the memory (and must remember to use the corresponding free function for the memory depending on how the function allocated it).
If you examine large C APIs such as Win32, you will find that virtually all functions that return strings use the first form where the caller passes a buffer and a size. Only in limited circumstances might you find the second form where the function allocates the return value (I can't think of any at the moment).
我更喜欢第二个选项,因为它允许函数决定需要多大的缓冲区。通常来电者无法做出决定。
I'd prefer the second option because it allows the function to decide how big a buffer is needed. Often the caller is not in a position to take that decision.
传递缓冲区和大小样式的另一种替代方法是使用返回代码:
返回代码表示调用者的缓冲区是合适的并且已被填充。
零 0 表示调用者的缓冲区太小,并显示实际需要的大小,允许调用者调整缓冲区大小并重试。
Another alternative to the pass a buffer and size style, using a return code:
A zero return code indicates that the caller's buffer was suitable and has been filled in.
A return code > 0 indicates that the caller's buffer was too small and reveals the size that is actually needed, allowing the caller to resize his buffer and retry.
在大多数情况下,传递缓冲区地址和长度是最好的。它不太容易出错,而且不必担心内存泄漏。事实上,在一些紧密的嵌入式系统中,完全不希望使用堆。但是,该函数不得超出缓冲区,因为这可能会导致系统崩溃,甚至更糟:使其容易受到黑客的攻击。
我唯一一次看到函数返回分配的缓冲区是 libxml 的 API,用于从 xmlDoc 生成 XML 文本。
Passing buffer address and length is best in most cases. It is less error-prone and one does not have to worry about memory leaks. In fact, in some tight embedded systems it is completely undesirable to use the heap. However, the function must not overrun the buffer as that can crash the system and worse: make it vulnerable to hackers.
The only time where I've seen function returning allocated buffer is libxml's API to generate XML text from xmlDoc.