使用指针交换对象 [英] Swapping objects using pointers

查看:72
本文介绍了使用指针交换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换为使用空指针交换对象的一门功课问题的对象。我的函数的声明必须是:

I'm trying to swap objects for a homework problem that uses void pointers to swap objects. The declaration of my function has to be:

void swap(void *a, void *b, size_t size);

我不是在寻找的code如何做到这一点,所以我可以通过我自己看着办吧,但我不知道如果我理解正确。我发现有一个问题是这样做:

I'm not looking for the exact code how to do it so I can figure it out by myself, but I'm not sure if I understand it correctly. I found that one problem is by doing:

void *temp;
temp = a;
a = b;
b = temp;

只改变什么指针指向。那是对的吗?如果它是正确的,为什么不换指针真正改变* a和* b的内容。因为如果你的指针指向不同的东西,你能不能解引用它,现在的对象会有所不同?

only changes what the pointers point to. Is that correct? If it is correct, why doesn't swapping pointers actually change the contents between *a and *b. Because if your pointer points to something different, couldn't you dereference it and the objects would now be different?

同样,在切换的值,如:

Similarly, just switching the values like:

void *temp;
*temp = *a;
*a = *b;
*b = *temp;

是不正确的或者,我不知道为什么。因为再次,在我看来,该内容被切换。

Is not correct either, which I'm not sure why. Because again, it seems to me that the content is switched.

交换是否意味着对象的内存和价值完全交换什么指针指向?

Does swapping objects mean complete swapping of memory and value of what a pointer points to?

所以好像我不得不使用malloc为我交换分配足够的空间。如果我对一个对象分配足够的内存,假设他们的大小相同,我实在不明白它是如何比上述其他两种方法的不同。

So it seems like I have to use malloc to allocate enough space for my swap. If I allocate enough memory for one object, assuming they are the same size, I don't really see how it is different than the other two methods above.

void *temp = malloc(sizeof(pa));
// check for null pointer
temp = a;
// do something I'm not sure of since I don't quite get how allocating space is any 
// different than the two above methods???

谢谢!

推荐答案

交换三分球不改变指向的值。如果那样,那会像在信封上移动我进入你的房子,你换地址标签了我的。

Swapping pointers does not change the pointed-to values. If it did, that would be like swapping address labels on envelopes moving me into your house and you into mine.

您几乎有:

void swap(void *a, void *b, size_t size) {
  char temp[size]; // C99, use malloc otherwise
  // char serves as the type for "generic" byte arrays

  memcpy(temp, b,    size);
  memcpy(b,    a,    size);
  memcpy(a,    temp, size);
}

借助的memcpy 功能拷贝内存,这是C.对象的定义(叫POD或纯醇在C ++中的数据,来比较),这样一来,memcpy的是你怎么做任务,而无需关心对象的类型,你甚至可以写其他任务的memcpy的,而不是:

The memcpy function copies memory, which is the definition of objects in C. (Called POD or plain ol' data in C++, to compare.) In this way, memcpy is how you do assignment without caring about the type of the object, and you could even write other assignments as memcpy instead:

int a = 42, b = 3, temp;

temp = b;
b    = a;
a    = temp;
// same as:
memcpy(&temp, &b,    sizeof a);
memcpy(&b,    &a,    sizeof a);
memcpy(&a,    &temp, sizeof a);

这正是上述功能做什么,因为你不能使用赋值当你不知道对象的类型,和无效的是,对未知矗立在类型。 (如函数的返回类型时,这也意味着无。)

This is exactly what the above function does, since you cannot use assignment when you do not know the type of the object, and void is the type that stands in for "unknown". (It also means "nothing" when used as function return type.)

作为一个好奇,另一个从而避免了常见的情况malloc和不使用C99的沃拉斯版本:

As a curiosity, another version which avoids malloc in common cases and doesn't use C99's VLAs:

void swap(void *a, void *b, size_t size) {
  enum { threshold = 100 };
  if (size <= threshold) {
    char temp[threshold];

    memcpy(temp, b,    size);
    memcpy(b,    a,    size);
    memcpy(a,    temp, size);
  }
  else {
    void* temp = malloc(size);
    assert(temp); // better error checking desired in non-example code

    memcpy(temp, b,    size);
    memcpy(b,    a,    size);
    memcpy(a,    temp, size);

    free(temp);
  }
}

这篇关于使用指针交换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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