• What is a "const void *src"?

    From T@21:1/5 to All on Wed Nov 30 15:51:47 2022
    Hi All,

    In the following:

    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/memcpy-s-wmemcpy-s?view=msvc-170
    C
    errno_t memcpy_s(
    void *dest,
    size_t destSize,
    const void *src,
    size_t count
    );

    What exactly is a "const void *src"?

    Many thanks,
    -T

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From T@21:1/5 to All on Wed Nov 30 22:10:38 2022
    On 11/30/22 15:51, T wrote:
    Hi All,

    In the following:

    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/memcpy-s-wmemcpy-s?view=msvc-170
    C
    errno_t memcpy_s(
    void *dest,
    size_t destSize,
    const void *src,
    size_t count
    );

    What exactly is a "const void *src"?

    Many thanks,
    -T


    M$ answered this one for me:


    MinxinYu-MSFT answered

    const void *src

    You need to split it into two parts

    1) Pointer to Constant
    The address of pointers can be changed, but the
    value of the variable that the pointer points
    cannot be changed.

    E.g. const int* ptr;


    2) .void pointer
    A void* pointer can be converted into any
    other type of data pointer.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Thu Dec 1 08:00:40 2022
    T,

    What exactly is a "const void *src"?
    ...
    M$ answered this one for me:

    Now the only thing you need to consider is /why/. Why does the source has
    a "const" prefix, but the destination does not need it. What does it
    /mean/.

    IOW, you have *some* , but not *the* answer.

    And no, I've got no idea myself.

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul N@21:1/5 to R.Wieser on Thu Dec 1 06:18:32 2022
    On Thursday, December 1, 2022 at 7:01:01 AM UTC, R.Wieser wrote:
    T,
    What exactly is a "const void *src"?
    ...
    M$ answered this one for me:
    Now the only thing you need to consider is /why/. Why does the source has
    a "const" prefix, but the destination does not need it. What does it
    /mean/.

    IOW, you have *some* , but not *the* answer.

    And no, I've got no idea myself.

    You have some funny blind spots, Rudy!

    memcpy copies things from one place in memory to another, so presumably memcpy_s does the same but with some sort of safety check. src is the "source", so it is only looked at. dest is the "destination" which obviously will be altered. (Just to clarify,
    the const in this case does not determine whether the parameters src and dest themselves can be altered, it determines whether the memory they point at can be altered.) Both are void pointers because at this stage we are not worrying about exactly what
    is stored in the memory. In my opinion this is a confusing use of the word "void" which also means "takes no parameters" and "doesn't return a result" but that is the word they decided to use.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Thu Dec 1 16:41:53 2022
    Paul,

    And no, I've got no idea myself.

    You have some funny blind spots, Rudy!

    Yeah, including the place between my shoulder blades. I can never twist my head that far back. :-)

    As for those "blind spots" ? That might be because I do not really program
    in C{anything}. I do know a few odds and ends about it, but thats all.

    (Just to clarify, it determines whether the memory they point at can be altered)

    Thats odd, as I have no problem with altering the memory "src" is pointing
    at. Before, or after (have not tried during) that memcpy will do/has done
    its thing.

    IOW, there is a reason I have no idea, as it doesn't make any sense to me
    that a function tells / can tell me what I can or can't do with two memory blocks I own. Can you explain ?

    Both are void pointers because at this stage we are not worrying
    about exactly what is stored in the memory.

    Now that I knew. :-)

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From T@21:1/5 to Paul N on Thu Dec 1 15:36:49 2022
    On 12/1/22 06:18, Paul N wrote:
    In my opinion this is a confusing use of the word "void"

    "Void" always gets me too.

    I see it as a pointer that can point to any type
    of structure.


    void
    adjective

    Containing no matter; empty.
    Not occupied; unfilled.
    Completely lacking; devoid: synonym: empty.

    Real bad name to have picked.


    They do the same thing in Raku:

    sigil
    noun

    A seal; a signet.
    A sign or an image considered magical.

    In Raku, it is the "$,@,%" sign in front
    of the variable's name.

    Another real bad choice.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul N@21:1/5 to R.Wieser on Fri Dec 2 05:35:57 2022
    On Thursday, December 1, 2022 at 3:42:09 PM UTC, R.Wieser wrote:
    (Just to clarify, it determines whether the memory they point at can be altered)

    Thats odd, as I have no problem with altering the memory "src" is pointing at. Before, or after (have not tried during) that memcpy will do/has done its thing.

    IOW, there is a reason I have no idea, as it doesn't make any sense to me that a function tells / can tell me what I can or can't do with two memory blocks I own. Can you explain ?

    It's not telling you what you can or can't do, memcpy_s is promising that *it* won't alter the memory pointed to by src. So you know which bits of memory might get altered at various points and which will not be. It also means that if *your* function has
    promised not to alter someone else's memory, you can pass that memory to memcpy_s as a source and the compiler will be happy. The compiler will object if you try to pass a const pointer to memcpy_s as a destination, so it helps reduce bugs.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul N@21:1/5 to All on Fri Dec 2 05:26:55 2022
    On Thursday, December 1, 2022 at 11:36:52 PM UTC, T wrote:
    On 12/1/22 06:18, Paul N wrote:
    In my opinion this is a confusing use of the word "void"
    "Void" always gets me too.

    I see it as a pointer that can point to any type
    of structure.

    Yes, that's right. I sometimes think of it as "a pointer wrapped in a blanket". At some point in the past the value was used to actually put something into memory. At some point in the future it will be used to fish something out of memory. But at the
    moment, it's a pointer to something and we don't care for the moment what the "something" is.

    void
    adjective

    Containing no matter; empty.
    Not occupied; unfilled.
    Completely lacking; devoid: synonym: empty.

    Real bad name to have picked.

    I think part of the idea is to keep the number of "keywords" to a minimum, so they keep using the same word for lots of different things. In C, "static" has several meanings that are not easy to reconcile, and in C++ it gets at least one more meaning.
    There are people who say that the various meanings of "void" in C are all the same concept, but I have a degree in Maths and I have difficulty seeing it that way so I imagine most other people do too.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Fri Dec 2 15:40:36 2022
    Paul,

    It's not telling you what you can or can't do, memcpy_s is promising that *it*
    won't alter the memory pointed to by src

    Having thought about it I think its way more plausible that the prefix is ment-for-and-used-by the C{something} compiler, and us humans using it as an indicator to the direction of the dataflow is more-or-less if not purely "collateral damage".

    Thanks for the explanation though.

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From T@21:1/5 to Paul N on Tue Dec 6 15:37:32 2022
    On 12/2/22 05:26, Paul N wrote:
    I see it as a pointer that can point to any type
    of structure.
    Yes, that's right. I sometimes think of it as "a pointer wrapped in a blanket". At some point in the past the value was used to actually put something into memory. At some point in the future it will be used to fish something out of memory. But at the
    moment, it's a pointer to something and we don't care for the moment what the "something" is.



    Sort of like the stem cells of C Pointers!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)