CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: std::memcpy

  1. #1
    Join Date
    Aug 2007
    Posts
    3

    std::memcpy

    Is it in poor taste to use std::memcpy in a highly-OO C++ application? That is, in the same way that the use of realloc / free / malloc / calloc is frowned upon?

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: std::memcpy

    That's a loaded question. memcpy can only be used on POD types. If you know the data that you want to copy is of POD type then you can use memcpy, if not then don't as it will constitute undefined behaviour.

    As an aside, under some circumstances, a compiler may convert a "copy by iteration" into a memcpy if it can figure out that the data held in both source and destination is both contiguous and POD.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: std::memcpy

    There's not much reason to prefer memcpy over std::copy in C++, and the latter handles a larger number of cases correctly.

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: std::memcpy

    Agreed, and, the compiler can make it just as fast for POD types.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured