English 中文(简体)
C++中 in化者和点人之间的种姓
原标题:Casting between integers and pointers in C++
  • 时间:2010-01-03 06:35:49
  •  标签:
  • c++
  • pointers
#include<iostream>
using namespace std;

int main()
{
  int *p,*c;
  p=(int*)10;
  c=(int*)20;
  cout<<(int)p<<(int)c;
}

有些人问我,“上述法典有什么错误?”,我怎么说。 有些人请帮助我。

最佳回答

有些人希望从C++标准中引用(如果评论格式受到严格限制,则在答复中说明这一点),这里是1999年两种:

5.2.10/3

由<代码>reinterpret_cast绘制的地图界定了实施情况。

5.2.10/5

A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if ant such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined.

我认为,没有任何人要求,这种执行界定的制图必须为所有投入提供有效代表。 否则,在建立有地址登记册的架构方面,如果执行的话,就会非常容易陷入困境。

p = (int*)10;

如果绘图当时不提供有效代表(即点人的有效代表可能取决于时间)。 例如,delete可能使被删除的点名的表述无效。

问题回答

根据C++标准,<编码>int和点数据类型不必有相同数目的比照,这是一件事——这意味着你可能失去准确性。

此外,将<代码>int投到>int上,然后又重新贴上sil。 为什么不要把它作为<代码>int?

我实际上在did试图将这一项目列入议程,并做的是罚款,但事故可能比良好设计多。

假设我对应该做些什么是正确的,那么它就应当这样看待:

int main()
{
  int *p, *c;
  // Something that creates whatever p and c point to goes here, a trivial example would be.
  int pValue, cValue;
  p = &pValue;
  c = &cValue;
  // The & operator retrieves the memory address of pValue and cValue. 

  *p = 10;
  *c = 20;
  cout << *p << *c;
}

为了给点人参照的变量分配或检索价值,您需要rel=“nofollow noreferer”>。 它。

你们的法典正在把10个点推到暗中(即实际住处的记忆地址)。

页: 1

如果你试图在这些地址读取数据,你将价值(10和20)重新分配给显然是一个潜在问题的点。 给愤怒的指点也确实是令人怀疑的。 你的主要职能没有回报说明。 这只是几个方面。

与此相比,情况有:

int *p,*c;
p=(int*)10;
c=(int*)20;
  • afterwards p is pointing to memory address 10
  • afterwards c is pointing to memory address 20

这并不十分故意。

而且,我相信,整个方案只会失事。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签