c++ memcpy return value

前端 未结 5 1624
囚心锁ツ
囚心锁ツ 2021-02-05 02:20

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

5条回答
  •  逝去的感伤
    2021-02-05 02:57

    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.

提交回复
热议问题