指针是否存储为整数? [英] Are pointers stored as integers?

查看:184
本文介绍了指针是否存储为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C/C ++(或与此相关的任何语言)中的指针是整数吗?指针保存一个内存地址,范围从0到内存的上限.因此,用数学术语来说,指针可以视为非负整数.

Are pointers in C/C++ (or any language for that matter), integers? A pointer holds a memory address, from 0 to the upper limit of your memory. So in math terms, a pointer could be considered a non-negative integer.

如何将指针存储在C/C ++中?还有其他流行语言吗?

How are pointers stored in C/C++? And in other popular languages?

推荐答案

这里有两个隐式问题,所以让我们考虑我所理解的每个问题.

There's a couple of implicit questions here, so let's take each one that I understand is present.

首先,C/C ++中的指针仅仅是伪装的整数吗?

First, are pointers in C/C++ just integeres with a disguise?

是的,不是,不是.首先,在实现方面,最常见的实现点的方法是为平台选择正确的整数大小(32位或64位),并将其用作可寻址内存空间中的绝对地址值.

Yes, they are, and no, they're not. First, implementation-wise, the most common way to implement points is to just pick the right size of integer for the platform (32-bit or 64-bit) and use that as an absolute address value into addressable memory space.

在这种情况下,您可以说指针只是整数.

In this context, you could say that pointers are just integers.

但是,C/C ++中的指针始终被定义为黑盒数据类型.您不应该将它们用作整数,因为它们很可能没有您所说的有意义的数值"上下文.

However, pointers in C/C++ has always been defined as black box data types. You're not supposed to use them as integers since they could very well not have what you would call a "numerical value" context that makes sense.

当然,它们是 look 数字,您可以向它们添加整数以越过它们,依此类推,但是所有这些都是特定于实现的,未记录的(有目的的),因此并不是很可靠.我在这里所说的可靠"是指,如果您将指针解释为整数,则不应依赖于指针与整数之间存在任何有意义的关系.

Certainly they look numerical, you can add integers to them to move past them and so on, but all of that is implementation specific, undocumented (on purpose), and thus not really reliable. What I mean by "reliable" here is that if you interpret the pointer as an integer, you should not rely on there being any meaningful relationship between the pointer and the integer.

对于专用平台,编译器,运行时等,您可能会发现它除了被视为可寻址空间中特定字节的数值外,没有任何特殊含义,但是我仍然不会那样想.指针是一个黑匣子.

For a specific platform, compiler, runtime, etc. you might find evidence that it doesn't have any special meaning beyond being treated as a numerical value that addresses a particular byte in addressable space, but I would still refrain from thinking of it that way. A pointer is a black box.

因此,下一个问题是,是否可以将保存指针的内存重新解释为整数.好吧,是的,您当然可以这样做,但是再次,您应该认为该整数的实际数值是没有意义的.如果您具有32位指针,则可以将其存储在32位整数中,反之亦然,但是该整数的数值意味着什么,我不会在上面放置任何值.

So the next question is if you can reinterpret the memory that holds a pointer as an integer. Well, yes, you can certainly do that, but again, you should consider the actual numerical value of that integer as meaningless. If you have a 32-bit pointer, you can store it in a 32-bit integer, and vice versa, but what that numerical value of the integer means, that I would not place any value on.

一个特定的实现可以很好地做到这一点:

A particular implementation could very well take this:

p++;

表示这样的意思:

i -= sizeof(element);

即.反转的内存空间.我不知道有这样的环境,计算机,运行时,编译器或诸如此类的东西,但是文档允许.

ie. reversed memory space. I don't know of any such environment, computer, runtime, compiler, or whatnot that does this, but the documentation allows for it.

这就是你应该关心的.

这篇关于指针是否存储为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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