为什么没有内存访问冲突? [英] Why there is no memory access violation?

查看:56
本文介绍了为什么没有内存访问冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下代码片段:


struct struc {

int member1;

int member2;

};

printf("&((struc *)0) - > member2 =%p \ n",&((struc *)0) - > member2);


在VC7.1中,输出为4,即构件中的member2的偏移量。


我想知道为什么没有内存访问违规

"((struc *)0) - > member2" ?

为什么输出是结构的偏移?


纯C编译器的输出是什么?我现在手头没有C编译器。

解决方案

aling写道:

有以下代码片段:

struct struc {
int member1;
int member2;
};
printf("&((struc *)0) - > ; member2 =%p \ n",&((struc *)0) - > member2);

在VC7.1中,输出为4,即memberc在struc中的偏移量。


这是明智的行为。

我想知道为什么没有内存访问违规


那个将是另一个合理的行为。

"((struc *)0) - > member2" ?
为什么输出是结构的偏移?

纯C编译器的输出是什么?我现在没有C编译器。




代码是非法的,它是未定义的行为。这意味着系统

可以做任何事情。不要取消引用空指针。在我的系统上,运行此程序的
会自动关闭我的网络浏览器并打开我的

文字处理器。因为我的RAM数量非常有限,所以这需要花费大约2个小时的时间。


Jonathan


Jonathan Mcdougall写道:

aling写道:

有以下代码片段:

struct struc {
int member1;
int member2;
};
printf("&((struc *)0) - > member2 =%p \ n",&( (struc *)0) - > member2);

在VC7.1中,输出为4,即memberc in struc的偏移量。



这是一种明智的行为。

我想知道为什么没有内存访问违规



这将是另一个合理的行为。

"((struc *)0) - > member2" ?
为什么输出是结构的偏移?

纯C编译器的输出是什么?我现在没有C编译器。



代码是非法的,它是未定义的行为。这意味着系统可以做任何事情。不要取消引用空指针。在我的系统上,运行此程序会自动关闭我的网络浏览器并打开我的
文字处理器。




我也是!除了我的,它还撰写并发布此回复。事实上,

我甚至都不知道我刚发布了这个。


aling写道:

有以下代码片段:

struct struc {
int member1;
int member2;
};
printf("&((struc *) )0) - > member2 =%p \ n",&((struc *)0) - > member2);

在VC7.1中,输出为4,偏移量结构中的member2。

我想知道为什么没有内存访问违反
"((struc *)0) - > member2" ?
为什么输出是struc的偏移?




没有内存访问冲突,因为没有内存访问。

你只是要求编译器计算成员的地址,

不读取成员。该表达式完全由

常量组成,结果在编译时计算。


输出为地址4,因为它是地址的偏移量0

你提供的。


-

Scott McPhillips [VC ++ MVP]


Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2);

In VC7.1, the output is 4, the offset of member2 in struc.

I wonder why there is no memory access violation for
"((struc*)0)->member2" ?
And why the output is the offset of struc?

what''s the output in pure C compiler? I have no C compiler at hand now.

解决方案

aling wrote:

Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2);

In VC7.1, the output is 4, the offset of member2 in struc.
That''s a sensible behavior.
I wonder why there is no memory access violation for
That would be another sensible behavior.
"((struc*)0)->member2" ?
And why the output is the offset of struc?

what''s the output in pure C compiler? I have no C compiler at hand now.



The code is illegal and it is undefined behavior. That means the system
could do anything. Don''t dereference a null pointer. On my system,
running this program automatically closes my web browser and opens my
word processor. Since I have very limited amount of RAM, this took
approximately 2 hours.

Jonathan


Jonathan Mcdougall wrote:

aling wrote:

Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2);

In VC7.1, the output is 4, the offset of member2 in struc.


That''s a sensible behavior.

I wonder why there is no memory access violation for


That would be another sensible behavior.

"((struc*)0)->member2" ?
And why the output is the offset of struc?

what''s the output in pure C compiler? I have no C compiler at hand now.


The code is illegal and it is undefined behavior. That means the system
could do anything. Don''t dereference a null pointer. On my system,
running this program automatically closes my web browser and opens my
word processor.



Me too! Except on mine it also composes and posts this reply. In fact,
I''m not even aware that I just posted this.


aling wrote:

Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2);

In VC7.1, the output is 4, the offset of member2 in struc.

I wonder why there is no memory access violation for
"((struc*)0)->member2" ?
And why the output is the offset of struc?



There is no memory access violation because there is no memory access.
You have simply asked the compiler to compute the address of the member,
not to read the member. The expression is composed entirely of
constants and the result is computed at compile time.

The output is address 4 because that is the offset from the address of 0
that you supplied.

--
Scott McPhillips [VC++ MVP]


这篇关于为什么没有内存访问冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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