S****z 发帖数: 666 | 1 很简单 int a[10];
cout << (&a[3] - &a[0]) << endl;
它结果为3
但是我把地址打出来明明一个是38,一个是2C
那不应该是(3-0)*sizeof(int)? |
a**a 发帖数: 416 | 2 When you do pointer operation, the result depends on the type. Try following
on your codes:
cout << ((char *)&a[3] - (char *)&a[0]) |
S****z 发帖数: 666 | 3 我没有do pointer operation吧,就两个地址减了一下而已啊
你说的pointer operation, the result depends on the type
到底想说啥啊?
然后你那行代码输出是12,就是3*sizeof(int)
请详细解析一下吧
following
【在 a**a 的大作中提到】 : When you do pointer operation, the result depends on the type. Try following : on your codes: : cout << ((char *)&a[3] - (char *)&a[0])
|
t****t 发帖数: 6806 | 4 两个指针减一下不是pointer operation吗?
【在 S****z 的大作中提到】 : 我没有do pointer operation吧,就两个地址减了一下而已啊 : 你说的pointer operation, the result depends on the type : 到底想说啥啊? : 然后你那行代码输出是12,就是3*sizeof(int) : 请详细解析一下吧 : : following
|
S****z 发帖数: 666 | 5 那两个&a[i]是指针?
晕,我以为就是两个表示地址的常数而已啊
要是指针的话,大胆请教指针类型, int *吗?
【在 t****t 的大作中提到】 : 两个指针减一下不是pointer operation吗?
|
a**a 发帖数: 416 | 6 Think about this logic:
Support we have T *a, *b, and somewhere we assign b = a + 4. What do you
think (b - a) should be? |
M**u 发帖数: 10158 | 7 你cout (unsigned long)&a[3] - (unsigned long)&a[0]
你这个就是简单的pointer substract,取决于数组的类型
【在 S****z 的大作中提到】 : 很简单 int a[10]; : cout << (&a[3] - &a[0]) << endl; : 它结果为3 : 但是我把地址打出来明明一个是38,一个是2C : 那不应该是(3-0)*sizeof(int)?
|
S****z 发帖数: 666 | 8 4?
跟T无关?
那你前面又说跟指针类型有关?
【在 a**a 的大作中提到】 : Think about this logic: : Support we have T *a, *b, and somewhere we assign b = a + 4. What do you : think (b - a) should be?
|
M**u 发帖数: 10158 | 9 nod,和T无关
【在 S****z 的大作中提到】 : 4? : 跟T无关? : 那你前面又说跟指针类型有关?
|
a**a 发帖数: 416 | 10 You did not get me. What I mean is, the (b-a) is irrelevant to T,
your ORIGINAL question about the physical address difference depends
on the T type. When you are thinking about PHYSICAL address,
you have throw away the T. It is like you are doing (void *) conversion.
When you are talking about (T*), you are in the domain of
logical address.
【在 S****z 的大作中提到】 : 4? : 跟T无关? : 那你前面又说跟指针类型有关?
|
|
|
S****z 发帖数: 666 | 11 那大家能不能讲清楚点
一时有关一时无关,那不跟那边墙头草的书一样吗?
you
【在 M**u 的大作中提到】 : nod,和T无关
|
a**a 发帖数: 416 | 12 Then understand at first what is PHYSICAL ADDRESS and what is pointer.
Otherwise you can not get what other people are talking about.
【在 S****z 的大作中提到】 : 那大家能不能讲清楚点 : 一时有关一时无关,那不跟那边墙头草的书一样吗? : : you
|
S****z 发帖数: 666 | 13 那请你顺带把(void*) conversion给讲了吧,那又是神马玩意?
现在在物理地址上我看到一个38,一个2C,差了12
怎么conversion一下就变逻辑地址缩水四分之一了?
【在 a**a 的大作中提到】 : You did not get me. What I mean is, the (b-a) is irrelevant to T, : your ORIGINAL question about the physical address difference depends : on the T type. When you are thinking about PHYSICAL address, : you have throw away the T. It is like you are doing (void *) conversion. : When you are talking about (T*), you are in the domain of : logical address.
|
S****z 发帖数: 666 | 14 物理地址我知道,
pointer我没用,我就取了它们地址而已
然后前面有人说取地址就是pointer,我暂时忍了
【在 a**a 的大作中提到】 : Then understand at first what is PHYSICAL ADDRESS and what is pointer. : Otherwise you can not get what other people are talking about.
|
a**a 发帖数: 416 | 15 OK. Let's talk about PHYSICAL ADDRESS. What is a PHYSICAL ADDRESS? It is a
reference to some place in the memory. That is all. It is like the GPS
location to your house/apartment. It is accurate, but also very limited,
since it won't logically lead to the definition of the place it refer to. The
LOGICAL ADDRESS is a logical piece of information about how this place is
used. In computer, this local piece of information about how the data lived
in the memory began with that address is called TYPE. Not only it defines
its beginning place, and it defines how far it should extend.So it is
vaguely like the mailing address to your house/apartment. Now I am living in
an apartment number 1001, next to another apartment number 1000. So
logically the difference is (1001 - 1000) = 1, but the physical distance
between us could be 100 feet.
So when you are peeking the address, you are looking at a physical address.
When I am writing this, suddenly I realized that you might have
misunderstood the memory model. Please tell us you do understand the
underlying memory model of the computer. |
S****z 发帖数: 666 | 16 那我看地址的时候看到的38,2C是不是物理地址?
你意思是我不应该看这些物理地址,应该看它的逻辑地址?
那怎么把GPS LOCATION转成门牌号啊?sigh,真是麻烦,所见还非所得了
计算机内存模型那块我大概知道它用一个叫什么虚拟内存的概念,
然后来来去去几个页面调度算法,再复杂的我就不太清楚了
不过貌似这跟我取数组元素地址减一下没啥关系吧
The
lived
in
.
【在 a**a 的大作中提到】 : OK. Let's talk about PHYSICAL ADDRESS. What is a PHYSICAL ADDRESS? It is a : reference to some place in the memory. That is all. It is like the GPS : location to your house/apartment. It is accurate, but also very limited, : since it won't logically lead to the definition of the place it refer to. The : LOGICAL ADDRESS is a logical piece of information about how this place is : used. In computer, this local piece of information about how the data lived : in the memory began with that address is called TYPE. Not only it defines : its beginning place, and it defines how far it should extend.So it is : vaguely like the mailing address to your house/apartment. Now I am living in : an apartment number 1001, next to another apartment number 1000. So
|
M**u 发帖数: 10158 | 17 不是物理地址
是virtual address
【在 S****z 的大作中提到】 : 那请你顺带把(void*) conversion给讲了吧,那又是神马玩意? : 现在在物理地址上我看到一个38,一个2C,差了12 : 怎么conversion一下就变逻辑地址缩水四分之一了?
|
g**w 发帖数: 969 | 18 有这么难理解吗?本质上指针就是地址,但是C语言毕竟是个高级语言,有个类型的概念
int* a;
a++ // ++ 是偏移了一个int,而不是一个byte
所以,(&a[3] - &a[0]) 相当于
int* a1 = &a[3]
int* a2 = &a[0]
a1-a2 = 3
转换成char*,就不一样了
((char*)&a[3] - (char*)&a[0]) = 12 |
t****t 发帖数: 6806 | 19 他就是挖坑的, 表理他就好了.
The
lived
in
【在 a**a 的大作中提到】 : OK. Let's talk about PHYSICAL ADDRESS. What is a PHYSICAL ADDRESS? It is a : reference to some place in the memory. That is all. It is like the GPS : location to your house/apartment. It is accurate, but also very limited, : since it won't logically lead to the definition of the place it refer to. The : LOGICAL ADDRESS is a logical piece of information about how this place is : used. In computer, this local piece of information about how the data lived : in the memory began with that address is called TYPE. Not only it defines : its beginning place, and it defines how far it should extend.So it is : vaguely like the mailing address to your house/apartment. Now I am living in : an apartment number 1001, next to another apartment number 1000. So
|
S****z 发帖数: 666 | 20 现在貌似只能这样理解了
概念
【在 g**w 的大作中提到】 : 有这么难理解吗?本质上指针就是地址,但是C语言毕竟是个高级语言,有个类型的概念 : int* a; : a++ // ++ 是偏移了一个int,而不是一个byte : 所以,(&a[3] - &a[0]) 相当于 : int* a1 = &a[3] : int* a2 = &a[0] : a1-a2 = 3 : 转换成char*,就不一样了 : ((char*)&a[3] - (char*)&a[0]) = 12
|
|
|
S****z 发帖数: 666 | 21 我敢对灯火发誓,绝对没有挖坑!!
我是真的不懂的时候才上来请教大家的
a
.
is
defines
living
【在 t****t 的大作中提到】 : 他就是挖坑的, 表理他就好了. : : The : lived : in
|
M**u 发帖数: 10158 | 22 。。。看着就像挖坑的
is
limited,
to
【在 S****z 的大作中提到】 : 我敢对灯火发誓,绝对没有挖坑!! : 我是真的不懂的时候才上来请教大家的 : : a : . : is : defines : living
|
S****z 发帖数: 666 | 23 晕,原来大家都这样看我?
sigh,可能是你们觉得问题太简单了所以觉得我在灌水吧
It
GPS
refer
place
data
is
【在 M**u 的大作中提到】 : 。。。看着就像挖坑的 : : is : limited, : to
|
t****t 发帖数: 6806 | 24 the questions are fine. but you argue too much without apparent reason. this
is typical hole-digger's behaviour.
【在 S****z 的大作中提到】 : 晕,原来大家都这样看我? : sigh,可能是你们觉得问题太简单了所以觉得我在灌水吧 : : It : GPS : refer : place : data : is
|
S****z 发帖数: 666 | 25 好,以后我会注意的!
this
【在 t****t 的大作中提到】 : the questions are fine. but you argue too much without apparent reason. this : is typical hole-digger's behaviour.
|
t****t 发帖数: 6806 | 26 too late. this ma-jia is marked as hole-digger type for indefinite length.
use another ma-jia next time.
【在 S****z 的大作中提到】 : 好,以后我会注意的! : : this
|
c****p 发帖数: 6474 | 27 物理地址和类型无关,但是同类型指针的差和类型有关。
【在 S****z 的大作中提到】 : 那我看地址的时候看到的38,2C是不是物理地址? : 你意思是我不应该看这些物理地址,应该看它的逻辑地址? : 那怎么把GPS LOCATION转成门牌号啊?sigh,真是麻烦,所见还非所得了 : 计算机内存模型那块我大概知道它用一个叫什么虚拟内存的概念, : 然后来来去去几个页面调度算法,再复杂的我就不太清楚了 : 不过貌似这跟我取数组元素地址减一下没啥关系吧 : : The : lived : in
|