是的memcpy指针​​一样分配? [英] Is memcpy of a pointer the same as assignment?

查看:139
本文介绍了是的memcpy指针​​一样分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

造成很大的混乱的另外一个问题,这里是关于指针语义问题,希望能明确的东西:

Following another question which caused much confusion, here is question about pointer semantics that will hopefully clear things up:

这是计划在所有情况下有效吗?唯一有趣的部分是在PA1 == PB分支。

Is this program valid in all cases? The only interesting part is in the "pa1 == pb" branch.

#include <stdio.h>
#include <string.h>

int main() {
    int a[1] = { 0 }, *pa1 = &a[0] + 1, b = 1, *pb = &b;
    if (memcmp (&pa1, &pb, sizeof pa1) == 0) {
        int *p;
        printf ("pa1 == pb\n"); // interesting part
        memcpy (&p, &pa1, sizeof p); // make a copy of the representation
        memcpy (&pa1, &p, sizeof p); // pa1 is a copy of the bytes of pa1 now
        // and the bytes of pa1 happens to be the bytes of pb 
        *pa1 = 2; // does pa1 legally point to b?
    }
    else {
        printf ("pa1 != pb\n"); // failed experiment, nothing to see
        pa1 = &a[0]; // ensure well defined behavior in printf
    }
    printf ("b = %d *pa1 = %d\n", b, *pa1);
    return 0;
 }

我想基于标准的报价答复。

I would like an answer based on standard quotes.

修改

应广大用户要求,这里是我想知道的:

By popular demand, here is what I want to know:


  • 是一个指针的语义价值(根据规范其行为)仅通过其数值(它包含的数字地址)来确定,对于给定类型的指针?

  • 如果不是,有可能只复制中包含一个指向物理地址而留下了相关的语义?

  • is a pointer's semantic "value" (its behavior according to the specification) determined only by its numerical value (the numerical address it contains), for a pointer of a given type?
  • if not, it is possible to copy only the physical address contained in a pointer while leaving out the associated semantic?

下面让我们说,有一个人过去到底发生了指针意外地指向另一个对象;我怎么可以用这样一款过去的结束指针访问其他对象?

Here let's say that some one past the end pointer happens to accidentally point to another object; how can I use such one past the end pointer to access the other object?

我做任何事,除了使用其他对象的地址的副本。 (这是一场游戏,了解C指针。)

I have the right to do anything, except use a copy of the address of the other object. (It's a game to understand pointers in C.)

IOW,我尽量回收脏钱就像黑手党。但我回收提取其重新presentation值一个肮脏的指针。,然后它看起来像干净的钱,我的意思是指针。没有人能看出其中的差别,不是吗?

IOW, I try to recycle dirty money just like the mafia. But I recycle a dirty pointer by extracting its value representation. Then it looks like the clean money, I mean pointer. Nobody can tell the difference, no?

推荐答案

一个指针只是一个无符号整数,它的值是内存中的某个位置的地址。覆盖的指针变量的内容没有不是覆盖正常的内容不同 INT 变量。

A pointer is simply an unsigned integer whose value is the address of some location in memory. Overwriting the contents of a pointer variable is no different than overwriting the contents of normal int variable.

所以,是的,例如做的memcpy(放P,放大器; PA1,sizeof的P)相当于赋值 P = PA1 ,但可能效率较低。

So yes, doing e.g. memcpy (&p, &pa1, sizeof p) is equivalent of the assignment p = pa1, but might be less efficient.

让我们尝试有点不同,而不是:

Lets try it a bit differently instead:

您有 PA1 指向某个对象(或者更确切地说,一举超越了一些对象),那么你有指针&放大器; PA1 指向变量 PA1 (即其中的变量 PA1 位于内存)。

You have pa1 which points to some object (or rather, one beyond some object), then you have the pointer &pa1 which points to the variable pa1 (i.e. the where the variable pa1 is located in memory).

图形它会是这个样子:


+------+     +-----+     +-------+
| &pa1 | --> | pa1 | --> | &a[1] |
+------+     +-----+     +-------+

<子> [注:&放大器;一个[0] + 1 相同&放大器;一个[1] ]

[Note: &a[0] + 1 is the same as &a[1]]

这篇关于是的memcpy指针​​一样分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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