Thread: memcpy and its derivatives

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Lightbulb memcpy and its derivatives

    Hello,

    this subject is rather about memcpy_s which is known as secure copy because it actually check for null pointers and overlapping sections. However my first question would be where is it really implemented and how can i use it?
    Quick googling for "where is memcpy_s" revealed that in order to use it I should include string.h and use at least c11 so I did but gcc still complains:
    Code:
     warning: implicit declaration of function memcpy_s; did you mean memcpy?
    PS: I compile on linux.

    I have couple of more questions:
    1. Is memcpy_s part of the standard?
    2. If I want to implement my own version what should I follow and comply with?
    3. Should memcpy_s enable copies inside array or memory blocks? I mean if I have an array of ten elements and use memcpy_s to copy last two bytes into first two bytes should it be allowed?

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    The docs point out:

    As with all bounds-checked functions, memcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.
    Yes, as of C11, memcpy_s part of the standard.

    I doubt you want to implement your own version. It is conceptually quite simple, but several compilers resort to inline assembler to implement memcpy most efficiently for a given CPU. If your concern is about safely implementing memcpy in a system without memcpy_s, what seems better is to consider a function that first tests all that is required before calling memcpy. That is, if one tests the pre-requisites, memcpy can be quite safe to do.

    While memcpy_s could be used inside an array, you may need to consider memmove instead (and the related memmove_s). There is a difference between moving some section from the end to the beginning of a block, vs moving, say, the last 2/3rds of a block to it's beginning.

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    @Niccolo but when I ask gcc to compile sing c11 it complains about unknown memcpy_s. So it is not defined.

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    214
    Is __STDC_LIB_EXT1__ defined?

    Is this in your header:

    #define __STDC_WANT_LIB_EXT1__ 1

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't think gcc supports the optional bounds checking interfaces. And if I'm not mistaken Microsoft's implementations don't completely follow the C11 standard, which is understandable since Microsoft doesn't truly support anything but C90.

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    214
    @jimblumberg has a point.

    I have reports that it depends on the platform and configuration. For example, in MinGW-w64 it is available, but in MinGW it isn't.

    It is an extension, which is to say you can't assume it would be portable or even likely available.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, that's what __STDC_LIB_EXT1__ is for, although if you're going to check for it, perhaps you might as well just do better checking for memcpy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors definition for base class that works for derivatives.
    By User Name: in forum C++ Programming
    Replies: 12
    Last Post: 07-21-2010, 02:13 PM
  2. coding and mathematics of derivatives and integrals
    By musikluvah in forum C Programming
    Replies: 2
    Last Post: 01-30-2006, 02:42 AM
  3. Optimization through derivatives...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-08-2005, 11:29 PM
  4. Mathematics <Derivatives of Sin>
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-01-2005, 09:18 AM
  5. Calculus Derivatives, OMG!!!
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-27-2003, 10:56 AM

Tags for this Thread