Discussion:
What is a "const void *src"?
(too old to reply)
T
2022-11-30 23:51:47 UTC
Permalink
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
T
2022-12-01 06:10:38 UTC
Permalink
Post by T
Hi All,
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.
R.Wieser
2022-12-01 07:00:40 UTC
Permalink
T,
Post by T
What exactly is a "const void *src"?
...
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
Paul N
2022-12-01 14:18:32 UTC
Permalink
T,
Post by T
What exactly is a "const void *src"?
...
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.
R.Wieser
2022-12-01 15:41:53 UTC
Permalink
Paul,
Post by Paul N
Post by R.Wieser
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.
Post by Paul N
(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 ?
Post by Paul N
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
Paul N
2022-12-02 13:35:57 UTC
Permalink
Post by R.Wieser
Post by Paul N
(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.
R.Wieser
2022-12-02 14:40:36 UTC
Permalink
Paul,
Post by Paul N
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
T
2022-12-01 23:36:49 UTC
Permalink
Post by Paul N
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.
Paul N
2022-12-02 13:26:55 UTC
Permalink
Post by T
Post by Paul N
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.
Post by T
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.
T
2022-12-06 23:37:32 UTC
Permalink
Post by Paul N
Post by T
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!

Loading...