关于memmo的实现 [英] Regarding implementation of memmove

查看:50
本文介绍了关于memmo的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wikibooks.org 上查看公共领域的实现.它实现 memmove() 如下,明确说明它不是完全可移植的"!我想知道为什么:

I was looking at public-domain implementations on wikibooks.org. It implements memmove() as following explicitly stating that it is "not completely portable"! I was wondering as to why:

  1. 括号放在代码的第一行,并且
  2. 代码不是完全可移植的.

代码如下:

void *(memmove)(void *s1, const void *s2, size_t n)
{
   char *p1 = s1;
   const char *p2 = s2;

   if (p2 < p1 && p1 < p2 + n) {
       /* do a descending copy */
       p2 += n;
       p1 += n;
       while (n-- != 0)
           *--p1 = *--p2;
   } else
       while (n-- != 0)
           *p1++ = *p2++;

   return s1;
}

推荐答案

memmove() 函数的规范是可以处理重叠的源和目的,但是规范没有说memmove() 必须使用指向同一内存块(标准术语中的对象")的指针来调用.

The specification of function memmove() is that it can handle overlapping source and destination, but the specification does not say that memmove() has to be called with pointers to the same memory block ("object" in the standard's parlance).

p1p2 是指向不同内存块的指针时,条件 p2 未定义的行为.C99 标准说 (6.5.8:5):

When p1 and p2 are pointers to different memory blocks, the condition p2 < p1 is undefined behavior. The C99 standard says (6.5.8:5):

比较两个指针时,结果取决于相对指向的对象在地址空间中的位置.如果两个指向对象或不完整类型的指针都指向同一个对象,或者都指向同一个数组对象的最后一个元素,它们比较相等.如果指向的对象是相同的成员聚合对象,指向稍后声明的结构成员的指针比较大于指向结构中较早声明的成员的指针,和指向具有较大下标值的数组元素的指针进行比较大于指向相同数组元素的指针下标值.指向同一联合对象成员的所有指针比较相等.如果表达式 P 指向数组的一个元素对象和表达式 Q 指向相同的最后一个元素数组对象,指针表达式 Q+1 比较大于 P.所有其他情况,行为未定义.

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

我不知道这是否是解释所指的内容,但它是不可移植性的一个明确来源.

I don't know if this is what the explanation refers to, but it is one definite source of non-portability.

一个不同的实现可能使用 (uintptr_t)p2 <(uintptr_t)p1.那么比较<就是整数之间的比较.到 uintptr_t 的转换给出了实现定义的结果.uintptr_t 类型是在 C99 中引入的,它是一个无符号整数类型,保证保存指针的表示.

A different implementation might use (uintptr_t)p2 < (uintptr_t)p1. Then the comparison < is a comparison between integers. The conversion to uintptr_t gives implementation-defined results. The type uintptr_t was introduced in C99 and is an unsigned integer type guaranteed to hold the representation of a pointer.

memmove() 的完全可移植实现可能会使用第三个缓冲区来保存中间副本,或者 使用 == 比较(在必须使用的上下文中给出指定的结果).

A completely portable implementation of memmove() might use a third buffer to hold an intermediate copy, or use == comparison (which gives specified results in the context in which it would have to be used).

这篇关于关于memmo的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆