难道是保证安全执行的memcpy(0,0,0)? [英] Is it guaranteed to be safe to perform memcpy(0,0,0)?
问题描述
我不是在C标准这么精通,所以请多多包涵。
I am not so well-versed in the C standard, so please bear with me.
我想知道,如果它是保证,由标准,即的memcpy(0,0,0)
是安全的。
I would like to know if it is guaranteed, by the standard, that memcpy(0,0,0)
is safe.
我能找到的唯一限制是,如果内存区域重叠,则行为是不确定...
The only restriction I could find is that if the memory regions overlap, then the behavior is undefined...
但我们认为内存区域重叠在这里?
But can we consider that the memory regions overlap here ?
推荐答案
我有C标准(ISO / IEC 9899:1999)的草稿版本,它有一些有趣的东西,说这一呼吁。对于初学者来说,它提到(教派; 7.21.1 / 2)关于的memcpy
是
I have a draft version of the C standard (ISO/IEC 9899:1999), and it has some fun things to say about that call. For starters, it mentions (§7.21.1/2) in regards to memcpy
that
当一个参数声明为为size_t
n指定一个数组的长度
功能,n可以有到该函数的调用的值为零。除非明确说明
否则在一个特定的功能在本节的描述中,指针参数
这样的电话仍然应具有有效的值,如7.1.4 描述。在这样的呼叫,
即找到一个字符函数没有发生,比较两个函数
字符序列返回零,那副本复印件字符零功能
字符。
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. On such a call, a function that locates a character finds no occurrence, a function that compares two character sequences returns zero, and a function that copies characters copies zero characters.
在这里表示参考点这样:
The reference indicated here points to this:
如果一个参数传递给函数的值无效(如价值
的功能的程序的地址空间外域,或指针外,
或一个空指针或当相应的指针不可修改的存储
参数不是常量限定)或类型(升级后)没有一个函数预期
带有可变数量的参数,这种行为是未定义
If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.
因此,它看起来是依照C规格,呼吁像
So it looks like according to the C spec, calling
memcpy(0, 0, 0)
在不确定的行为,因为空指针被认为是无效值。
results in undefined behavior, because null pointers are considered "invalid values."
这是说,我简直令人吃惊,如果你这样做的的memcpy
任何实际的实现爆发,由于大多数直观的实现我能想到的将在什么都不做所有,如果你说要复制零字节。
That said, I would be utterly astonished if any actual implementation of memcpy
broke if you did this, since most of the intuitive implementations I can think of would do nothing at all if you said to copy zero bytes.
这篇关于难道是保证安全执行的memcpy(0,0,0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!