处理返回的 C 字符串

发布于 2024-11-05 01:13:25 字数 363 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

究竟谁懂我的在乎 2024-11-12 01:13:25

将缓冲区作为参数传递可以解决此类代码可能遇到的大多数问题。

如果它返回指向缓冲区的指针,那么您需要决定如何分配它以及调用者是否负责释放它。该函数可以返回不需要释放的静态指针,但它不是线程安全的。

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.

写下不归期 2024-11-12 01:13:25

传递缓冲区和大小通常不太容易出错,特别是如果字符串的大小通常为“合理”大小。如果动态分配内存并返回指针,则调用者负责释放内存(并且必须记住根据函数分配内存的方式使用相应的内存释放函数)。

如果您检查大型 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).

难如初 2024-11-12 01:13:25

我更喜欢第二个选项,因为它允许函数决定需要多大的缓冲区。通常来电者无法做出决定。

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.

荆棘i 2024-11-12 01:13:25

传递缓冲区和大小样式的另一种替代方法是使用返回代码:

size_t example_m_a(type_a a,char * buff,size_t buff_size)

返回代码表示调用者的缓冲区是合适的并且已被填充。

零 0 表示调用者的缓冲区太小,并显示实际需要的大小,允许调用者调整缓冲区大小并重试。

Another alternative to the pass a buffer and size style, using a return code:

size_t example_m_a(type_a a,char * buff,size_t buff_size)

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.

少女七分熟 2024-11-12 01:13:25

在大多数情况下,传递缓冲区地址和长度是最好的。它不太容易出错,而且不必担心内存泄漏。事实上,在一些紧密的嵌入式系统中,完全不希望使用堆。但是,该函数不得超出缓冲区,因为这可能会导致系统崩溃,甚至更糟:使其容易受到黑客的攻击。

我唯一一次看到函数返回分配的缓冲区是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文