C++ memcpy 返回值

发布于 2024-08-30 10:38:46 字数 376 浏览 7 评论 0原文

根据 http://en.cppreference.com/w/cpp/string/ byte/memcpy C++ 的 memcpy 采用三个参数:目标、源和大小/字节。它还返回一个指针。

void* memcpy( void* dest, const void* src, std::size_t count );

为什么会这样呢?参数还不够输入和复制数据吗?

我误解了什么吗?这些示例不使用返回值。

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.

void* memcpy( void* dest, const void* src, std::size_t count );

Why is that so? Aren't the parameters enough to input and copy data?

Am I misunderstanding something? The examples don't use the return value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

溺ぐ爱和你が 2024-09-06 10:38:46

如果函数没有任何特定的返回值,则通常习惯返回输入参数之一(被视为主要的参数)。这样做允许您在表达式中使用“链接”函数调用。例如,您可以

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

专门执行此操作,因为 strcpy 返回原始 dst 值作为其结果。基本上,在设计这样的函数时,您可能想要选择最合适的“链接”参数并将其作为结果返回(同样,如果您没有其他要返回的内容,即如果否则您的函数将返回 void< /代码>)。

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

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

如果memcpy没有返回目标缓冲区指针,我们可能必须实现上面看起来

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

“更长”的内容。这两种实现之间的效率没有任何差异。哪个版本更具可读性是有争议的。尽管如此,许多人可能会感激有“免费”的机会来写出像上面第一个版本这样简洁的俏皮话。

人们常常对 memcpy 返回目标缓冲区指针感到困惑,因为人们普遍认为从函数返回指针通常(或总是)表明该函数可能会分配/重新分配内存。虽然这可能确实表明了后者,但没有这样的硬性规则,而且从来没有这样的规则,因此经常表达的观点是,返回指针(如memcpy)在某种程度上是“错误”或“不良做法”是完全没有根据的。

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");

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).

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);
}

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.

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.

醉梦枕江山 2024-09-06 10:38:46

IIRC,在 C 的早期版本中没有 void 返回。因此,由于遗留原因,已经存在了足够长的时间的库函数会返回一些东西,这是他们能想到的最好的办法。

string.h 中有很多返回目标参数的函数:memcpystrcpystrcat。它不是很有用,但没有什么害处(可能在许多调用约定中甚至不需要指令来实现)。

您可能会想到使用: char *nextbuf = memcpy(get_next_buf(), previous_buf+offset, previous_size-offset); 而不是 char *nextbuf = get_next_buf(); memcpy(nextbuf 等); 或者其他什么。

为了进行比较,qsort 返回 void。它本可以被定义为返回base,原则是“返回一些东西,它可能会派上用场”,但事实并非如此。 std::copy 更有用的是返回一个迭代器到输出范围的end。对于非随机访问迭代器,对于调用者来说计算可能并不简单,甚至不可能。

IIRC, in early versions of C there was no void return. So library functions which have been around long enough return something for legacy reasons, and this was the best they could come up with.

There are a bunch of functions in string.h which return the destination parameter: memcpy, strcpy, strcat. It's not very useful, but it does no harm (probably in many calling conventions doesn't even require an instruction to implement).

You might conceivably come up with a use: char *nextbuf = memcpy(get_next_buf(), previous_buf+offset, previous_size-offset); instead of char *nextbuf = get_next_buf(); memcpy(nextbuf, etc); Or something.

For comparison, qsort returns void. It could have been defined to return base on the principle of "return something, it might come in handy", but wasn't. std::copy rather more usefully returns an iterator to the end of the output range. For non-random-access iterators that might not be trivial, or even possible, for the caller to compute.

沉睡月亮 2024-09-06 10:38:46

通过返回一个值,memcpy 函数的调用可以用作右值。

By returning a value, an invocation of the memcpy function can be used as an r-value.

恬淡成诗 2024-09-06 10:38:46

如果你想表明你没有使用它,你可以将 return 转换为 void - 例如:

<代码>
(无效)memcpy(mydest,mysrc,mybytes);

You can cast the return into a void if you want to indicate that you aren't using it - eg:


(void) memcpy(mydest, mysrc, mybytes);

只等公子 2024-09-06 10:38:46
void *memcpy(void* dest, const void* src, std::size_t count);
//returns void * which can be assigned to another void array that can be used as int or char data type.
void *ret = new int[6];
ret = memcpy(newarr, arr, sizeof(int)* 5);
void *memcpy(void* dest, const void* src, std::size_t count);
//returns void * which can be assigned to another void array that can be used as int or char data type.
void *ret = new int[6];
ret = memcpy(newarr, arr, sizeof(int)* 5);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文