c ++ memcpy返回值 [英] c++ memcpy return value

查看:2074
本文介绍了c ++ memcpy返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://en.cppreference.com/w/cpp/string/byte/memcpy c ++的 memcpy 有三个参数:destination,source和size / bytes。它也返回一个指针。为什么会这样呢?不是足以输入和复制数据的参数。

according to http://en.cppreference.com/w/cpp/string/byte/memcpy c++'s memcpy takes three parameters: destination, source and size/bytes. it also returns a pointer. why is that so? aren't the parameters enough to input and copy data.

还是我误解了什么?示例不使用返回值

or am i misunderstanding something? the examples don't use the return value

推荐答案

如果函数没有特定的返回值,通常返回一个的输入参数(被视为参数的参数)。这样做允许您在表达式中使用链接函数调用。例如,你可以做

If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use "chained" function calls in expressions. For example, you can do

char buffer[1024];
strcat(strcpy(buffer, "Hello"), " World");

特别是因为 strcpy c $ c> dst 值作为结果。基本上,当设计这样一个函数时,你可能要为链接选择最合适的参数,并返回它作为结果(如果你有其他的返回值,否则你的函数返回 void )。

specifically because strcpy returns the original dst value as its result. Basically, when designing such a function, you might want to choose the most appropriate parameter for "chaining" and return it as the result (again, if you have noting else to return, i.e. if otherwise your function would return void).

有些人喜欢,有些人不喜欢。这是个人喜好的问题。 C标准库经常支持这种技术, memcpy 是另一个例子。一个可能的用例可能是

Some people like it, some people don't. It is a matter of personal preference. C standard library often supports this technique, memcpy being another example. A possible use case might be something along the lines of

char *clone_buffer(const char *buffer, size_t size)
{
   return memcpy(new char[size], buffer, size);
}

如果 memcpy 没有返回目标缓冲区指针,我们可能需要实现上面的

If memcpy did not return the destination buffer pointer, we'd probably have to implement the above as

char *clone_buffer(const char *buffer, size_t size)
{
   char *clone = new char[size];
   memcpy(clone, buffer, size);
   return clone;
}

在这两个实现之间没有任何效率的任何差异的原因。而且可以争论哪个版本更可读。仍然有很多人可能会喜欢自由的机会来编写这样简洁的一行作为上面的第一个版本。

which looks "longer". There's no reason for any difference in efficiency between these two implementations. And it is arguable which version is more readable. Still many people might appreciate the "free" opportunity to write such concise one-liners as the first version above.

很多人发现混淆, memcpy 返回目标缓冲区指针,因为有一个流行的观点,即从函数返回指针通常(或总是)指示函数可能分配/重新分配内存。虽然这可能确实表示后者,但没有这样的硬规则,并且从未有过,所以经常表达的意见是返回一个指针(如 memcpy 是)不知何故错误或坏做法是完全没有根据的。

Quite often people find it confusing that memcpy returns the destination buffer pointer, because there is a popular belief that returning a pointer form a function should normally (or always) indicate that the function might allocate/reallocate memory. While this might indeed indicate the latter, there's no such hard rule and there has never been, so the often expressed opinion that returning a pointer (like memcpy does) is somehow "wrong" or "bad practice" is totally unfounded.

这篇关于c ++ memcpy返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆