473,883 Members | 2,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memcpy/memmove

(if this is a FAQ or in K&R2, I didn't find it)

What parameters (if any) may be 0 or NULL? IOW, which of the following
statements are guaranteed to produce well-defined behavior?

char src[10];
char dst[10];

memcpy( dst, src, 1 );
memcpy( NULL, src, 1 );
memcpy( dst, src, 0 );
memcpy( NULL, src, 0 );
memcpy( dst, NULL, 1 );
memcpy( NULL, NULL, 1 );
memcpy( dst, NULL, 0 );
memcpy( NULL, NULL, 0 );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05
35 12073
Da*****@cern.ch (Dan Pop) wrote in message news:<bp******* ***@sunnews.cer n.ch>...
In <1f************ **************@ posting.google. com> lj****@tiscalin et.it (Lorenzo J. Lucchini) writes:
Alan Balmer <al******@att.n et> wrote in message news:<gb******* *************** **********@4ax. com>...
On Tue, 18 Nov 2003 08:52:30 +0000, Christian Bau
<ch***********@ cbau.freeserve. co.uk> wrote:

[snip]

More likely because they decided that defining the behavior would have
little point, and would most likely break existing compilers, if not
code.
You said it. They *decided*. Definition 1b of 'decide' in m-w.com
goes:
"to select as a course of action"
This seems to imply that whenever a decision is made, another curse of
action (different from the one that was decided) could have been
selected.

So the C standard committee *decided* that memcpy() should behave that
way *instead of* behaving differently, and you can't know before
looking at the documents they produced.


The *good* question is why would you care about what they decided for a
memcpy call that doesn't make any sense in the first place?

If something doesn't make sense, it is safer to avoid using it, even if
you know what the standard says about its behaviour.


I agree with you here.

But the original question went like this:
< [snip]
< What parameters (if any) may be 0 or NULL? IOW, which of the
following
< statements are guaranteed to produce well-defined behavior?
< [snip]
The OP didn't ask whether it would be sensible or not to use memcpy()
with such and such parameters, he only asked whether a call with those
parameters would behave in a guaranteed way.

That's why I don't agree with what you said in
<bo**********@s unnews.cern.ch> , that
< >[snip]
< >That's where a copy of the Standard comes in handy.
<
< Not so. It is enough to engage your brain. The only non-obvious
cases
< are those where the byte count is 0:
< [snip]

While you specified that the relevant cases are "non-obvious" (which,
in the context of the C standard, seems to imply that "you should read
the standard to know the truth about them"), you also said that "it is
enough to engage your brain" ["instead of using a copy of the
Standard", or that's at least how I interpret this sentence].
Looks like you're either contradicting yourself or answering the wrong
question, unless I'm wrong in my interpretation.
[snip]


by LjL
lj****@tiscali. it
Nov 13 '05 #31
Alan Balmer <al******@att.n et> wrote in message news:<3t******* *************** **********@4ax. com>...
On 19 Nov 2003 03:31:36 -0800, lj****@tiscalin et.it (Lorenzo J.
Lucchini) wrote:
Alan Balmer <al******@att.n et> wrote in message news:<gb******* *************** **********@4ax. com>...
On Tue, 18 Nov 2003 08:52:30 +0000, Christian Bau
<ch***********@ cbau.freeserve. co.uk> wrote:

> [snip]
>
>So the fact that memcpy (NULL, src, 1) produces undefined behavior in C
>is not for some inherent reason, or for some deeply founded necessity,
>it is because the people responsibly for the C Standard decided that it
>would be so.

More likely because they decided that defining the behavior would have
little point, and would most likely break existing compilers, if not
code.


You said it. They *decided*. Definition 1b of 'decide' in m-w.com
goes:
"to select as a course of action"


However, deciding *whether* to define something is different than
deciding *which way* to define something. You need to read the rest of
the sentence as well.


Sure. But deciding *whether* to define something or leave it undefined
is still selecting a course of action.
So it is not the same as deciding *which way* to define something, but
it *is* the same as deciding *which way* to go about the definition.
This seems to imply that whenever a decision is made, another curse of
action (different from the one that was decided) could have been
selected.


Sure - they could have decided that a precise definition was needed.
They would then have to debate what that definition would be.


Correct, that decision would have required a subsequent decision,
while the decision of leaving the definition undecided did not.
So the C standard committee *decided* that memcpy() should behave that
way *instead of* behaving differently, and you can't know before
looking at the documents they produced.


No, they decided to leave the behavior undefined. That's the reason
for this discussion.


Depends which discussion - the OP asked whether some specific calls of
memcpy() had a defined behavior or not; later, some claimed that to
know the answer one has to read the ANSI standard, while others
claimed (or at least their statements were interpreted by more people
than myself alone as claiming) that common sense is enough.

With these parts of the thread in mind, let me assure you that I did
not disagree with your statement that "[they did not define the
behavior] [...] because they decided that defining the behavior would
have little point [...]".
However, I disagree with the implication (one that was not explicitly
stated by you, but from which this sub-thread originated) that it is
obvious that the actual decision of the Committee was to leave the
behavior undefined, only because the behavior would have little point.
I insist that one should read the Standard to know; it can't be
dismissed as obvious.

by LjL
lj****@tiscali. it
Nov 13 '05 #32
On 19 Nov 2003 11:09:19 -0800, lj****@tiscalin et.it (Lorenzo J.
Lucchini) wrote:
Depends which discussion - the OP asked whether some specific calls of
memcpy() had a defined behavior or not; later, some claimed that to
know the answer one has to read the ANSI standard, while others
claimed (or at least their statements were interpreted by more people
than myself alone as claiming) that common sense is enough.


As far as I'm concerned, the question is moot. One needs to read the
standard to know what the defined behavior of memcpy() is. One then
naturally knows what behavior is not defined, as well :-)

Reading the standard would be considerably quicker than asking here,
as well. But not as much fun :-)

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #33
Alan Balmer <al******@att.n et> spoke thus:
Reading the standard would be considerably quicker than asking here,
as well. But not as much fun :-)


Certainly not, this has been a quite entertaining discussion... glad
I asked ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #34
In article <3F************ ***@yahoo.com>,
CBFalconer <cb********@yah oo.com> wrote:
Christian Bau wrote:
CBFalconer <cb********@yah oo.com> wrote:
Christian Bau wrote:
>
... snip ...
>
> If for example strcpy (dst, src) had defined behavior whenever
> the dst argument is an actual array of char, and src a pointer
> to a string with strlen (src) >= sizeof (dst), then the world
> would be a much better place.

Why do you want to limit strcpy to strings shorter than the size
of a pointer? In other words, those semantics are unimplementable
with the existing definition of a C string.


I wrote "dst is an actual array of char", like in

char dst [100];

with sizeof (dst) == 100.


It doesn't matter. When you pass it to strcpy the thing passed is
a pointer, and sizeof (dst) will automatically be the sizeof a
pointer. The point is that strcpy does not receive sufficient
information to allow it to have that defined behaviour.


Before it is passed to strcpy all the information is there.
Nov 13 '05 #35
In <ch************ *************** ******@slb-newsm1.svr.pol. co.uk> Christian Bau <ch***********@ cbau.freeserve. co.uk> writes:
In article <3F************ ***@yahoo.com>,
CBFalconer <cb********@yah oo.com> wrote:
Christian Bau wrote:
> CBFalconer <cb********@yah oo.com> wrote:
> > Christian Bau wrote:
> > >
> > ... snip ...
> > >
> > > If for example strcpy (dst, src) had defined behavior whenever
> > > the dst argument is an actual array of char, and src a pointer
> > > to a string with strlen (src) >= sizeof (dst), then the world
> > > would be a much better place.
> >
> > Why do you want to limit strcpy to strings shorter than the size
> > of a pointer? In other words, those semantics are unimplementable
> > with the existing definition of a C string.
>
> I wrote "dst is an actual array of char", like in
>
> char dst [100];
>
> with sizeof (dst) == 100.


It doesn't matter. When you pass it to strcpy the thing passed is
a pointer, and sizeof (dst) will automatically be the sizeof a
pointer. The point is that strcpy does not receive sufficient
information to allow it to have that defined behaviour.


Before it is passed to strcpy all the information is there.


Wrong! If the array definition is not in scope, the array is still an
array of 100 char, but the information is not available to the compiler.

What you want really requires a language enforcing array bound checking.
C is not this kind of language and was certainly not designed for people
needing this kind of checking. The problem is not the language, it is
the people who're not fit for using it, but use it, anyway.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
5283
by: Method Man | last post by:
Just a few theoretical questions I had on 'memmove': 1. Is there ever a good reason to use memcpy instead of memmove? 2. Is memmove useful to copy structs (as opposed to = operator)? 3. In general, when should memmove explicitly be used?
5
18794
by: xdevel | last post by:
Hi, anyone can make me an example where memmove does not cause a memory overlapping and where memcpy do it? Thanks
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
11154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.