Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is a "const void *src"?

34 views
Skip to first unread message

T

unread,
Nov 30, 2022, 6:51:52 PM11/30/22
to
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

unread,
Dec 1, 2022, 1:10:41 AM12/1/22
to
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

unread,
Dec 1, 2022, 2:01:01 AM12/1/22
to
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


Paul N

unread,
Dec 1, 2022, 9:18:34 AM12/1/22
to
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

unread,
Dec 1, 2022, 10:42:09 AM12/1/22
to
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


T

unread,
Dec 1, 2022, 6:36:52 PM12/1/22
to
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.

Paul N

unread,
Dec 2, 2022, 8:26:57 AM12/2/22
to
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.

Paul N

unread,
Dec 2, 2022, 8:35:59 AM12/2/22
to
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.

R.Wieser

unread,
Dec 2, 2022, 9:40:54 AM12/2/22
to
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


T

unread,
Dec 6, 2022, 6:37:38 PM12/6/22
to
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!
0 new messages