473,761 Members | 10,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memcpy(0,0,0)

Hi, all.

This question came up in a code review: are null pointers legal as
arguments to memcpy? That is, is the program at the end of this article
strictly conforming? (Of course, I check the FAQ first and could not
find an answer on point.)

I don't have C89 or C99 handy, so I checked draft N869 from here:
http://www.open-std.org/jtc1/sc22/wg...69/n869.txt.gz

I note the following:

7.21.1 String function conventions
Where an argument declared as "size_t n" specifies the
length of the array for a function, "n" can have the value
zero on a call to that function. Unless explicitly stated
otherwise in the description of a particular function in
this subclause, pointer arguments on such a call shall still
have valid values, as described in 7.1.4.

7.21.2.1 The memcpy function
void *memcpy(void * restrict s1,
const void * restrict s2,
size_t n);
[But see N869 for complete text].

7.1.4 Use of library functions
... unless explicitly stated otherwise in the
detailed descriptions ...
If an argument to a function has an invalid
value (such as ... a null pointer) ... the behavior
is undefined.

So, by my analysis, 7.1.4 says that you must not pass a null pointer to
any library function unless the 7.X documentation allows it. 7.21.1
reenforces 7.1.4, and does not explicity allow a null pointer. 7.21.2.1
also does not explicitly allow a null pointer.

So, memcpy(0,0,0) invokes undefined behavior?

Regards,

Rob

#include <string.h>
int main(void)
{
memcpy(0,0,0);
return 0;
}
Nov 15 '05 #1
1 4867
remove the CAPS <""usenetABC\"@ xyzebeep.org (remove the caps)"> wrote:
This question came up in a code review: are null pointers legal as
arguments to memcpy? That is, is the program at the end of this article
strictly conforming? (Of course, I check the FAQ first and could not
find an answer on point.) So, memcpy(0,0,0) invokes undefined behavior?


Yes.

http://snipurl.com/i0ej

(Note that I asked essentially the same question, two years ago, so
you are not alone in wondering.)

--
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 15 '05 #2

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

Similar topics

13
17511
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
16
7679
by: Delali Dzirasa | last post by:
I would have a number packed with its hex representation of the integer below is some sample code of what is being done. int value = 20; //in hex it is 0x14 AddData (value); .. .. ..
6
7526
by: Samee Zahur | last post by:
Hi all, I'm a little confused - my guess is memcpy is no longer (or perhaps never was) a standard c++ function, since it has very little type check into it - and can potentially create havoc for user-defined types. Now my confusion is here - a simple instantiation of the standard copy algorithm can be quite slow compared to the older memcpy for obvious reasons - specially for a large array of built-in types like int or something; so do...
5
3747
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a struct into a buffer to put it into a database (BerkeleyDB, to be specific) - what do I do to memcpy the thing back? Code Example: //struct I want to work with
35
12060
by: Christopher Benson-Manica | last post by:
(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; char dst; memcpy( dst, src, 1 ); memcpy( NULL, src, 1 );
16
15949
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include <string.h> void *memcpy(void *dest, const void *src, size_t n);
33
33759
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
2949
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
18
21599
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data pointed to by the source pointer is volatile and not the pointer itself?
18
2708
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
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
9905
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
9775
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
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
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
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.