473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why there is no memory access violation?

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.

Nov 22 '05 #1
12 2879
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

Nov 22 '05 #2
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.
Nov 22 '05 #3
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]

Nov 22 '05 #4
Why the result of &((struc*)0)->member2 is a offset instead of address?

Why the two results of following code is different, and one is memory
address, the other is offset?

struct struc {
int member1;
int member2;
} astruc, * p_struc=&astruc, *p_null=0;
printf("&p_struc->member2=%p\n", &p_struc->member2); // this result is
a memory address
printf("&p_null->member2=%p\n", &p_null->member2); // this result is
offset of member2

Nov 22 '05 #5
Now I understand. The result of &((struc*)0)->member2 is not offset, it
is a memory address, though it can be used as one offset.

Nov 22 '05 #6
aling wrote:
Now I understand. The result of &((struc*)0)->member2 is not offset, it
is a memory address, though it can be used as one offset.


The code is not legal, despite the fact that it works. If you need to do
this sort of thing you should use the offsetof macro defined in
<stdlib.h>. Most likely the offsetof macro will do exactly what you are
doing above, but the offsetof macro is guaranteed to work (on C style
structs).

john
Nov 22 '05 #7
> defined in
<stdlib.h>.


I meant <stddef.h>

john
Nov 22 '05 #8
aling wrote:
Why the result of &((struc*)0)->member2 is a offset instead of address?

Why the two results of following code is different, and one is memory
address, the other is offset?

struct struc {
int member1;
int member2;
} astruc, * p_struc=&astruc, *p_null=0;
printf("&p_struc->member2=%p\n", &p_struc->member2); // this result is
a memory address
printf("&p_null->member2=%p\n", &p_null->member2); // this result is
offset of member2


The program is calculating the address of the member as if the object
itself were located at memory address 0. So addresses are identical to
offsets since they are both being measured from the same reference
point. Note that the memory address 0 as used in this program is not
necessarily the address that the NULL pointer references. And it is
even conceivable that memory address 0 could be a valid memory location
on a system somewhere. For that reason, the null pointer "0" is, in
C++, a symbolic representation of an invalid object address. The actual
null pointer address used when the code is compiled can be different
and would depend on the implementation.

And as alarming as this program appears, it is nonetheless performing
only pure computation and does not access these hypothetical memory
addresses; so despite appearances, it remains a valid program. Of
course, it's probably still not a good idea to work with pointers in
this way since it is all to easy to make a mistake.

Greg

Nov 22 '05 #9
In article <11**********************@g43g2000cwa.googlegroups .com>,
aling <li*********@126.com> wrote:
Now I understand. The result of &((struc*)0)->member2 is not offset, it
is a memory address, though it can be used as one offset.


Right, because structs have subobjects, and so -> effectively moves
to the subobject that is in the position where the respective memory
of the right type starts. IOWs, where that object's offsetof() is.
But, as mentioned, since there really is no such object at 0,
the code is undefined. Better to use offsetof() (although oddly
you may find it using the same implementation, but then at least
you'll know it works, and you'll be using offsetof() anyway
which will be portable so that if you use offsetof on an
implmentation where the undefined behavior is not doing the
obvious things, the vendor will provide for you the right definition
for that platform).
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #10
In article <11**********************@g49g2000cwa.googlegroups .com>,
Greg <gr****@pacbell.net> wrote:
aling wrote:
Why the result of &((struc*)0)->member2 is a offset instead of address?

Why the two results of following code is different, and one is memory
address, the other is offset?

struct struc {
int member1;
int member2;
} astruc, * p_struc=&astruc, *p_null=0;
printf("&p_struc->member2=%p\n", &p_struc->member2); // this result is
a memory address
printf("&p_null->member2=%p\n", &p_null->member2); // this result is
offset of member2
The program is calculating the address of the member as if the object
itself were located at memory address 0. So addresses are identical to
offsets since they are both being measured from the same reference
point. Note that the memory address 0 as used in this program is not
necessarily the address that the NULL pointer references. And it is
even conceivable that memory address 0 could be a valid memory location
on a system somewhere. For that reason, the null pointer "0" is, in
C++, a symbolic representation of an invalid object address. The actual
null pointer address used when the code is compiled can be different
and would depend on the implementation.


This I believe is all correct.
And as alarming as this program appears, it is nonetheless performing
only pure computation and does not access these hypothetical memory
addresses; so despite appearances, it remains a valid program. Of
course, it's probably still not a good idea to work with pointers in
this way since it is all to easy to make a mistake.


The problem though as I recall it is that _the struct_ is not
at address 0. I know both the C and C++ committee looked at this
and recall that it remained undefined behavior. Am I not recalling
a change that was made upon looking at it?
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #11
Greg Comeau wrote:

Better to use offsetof() (although oddly
you may find it using the same implementation, but then at least
you'll know it works


Indeed. It's not odd, because offsetof is in the standard library. The
standard library is part of the implementation, and the only requirement
is that its components do what they're supposed to do. They can, and
often do, rely on "undefined behavior" which sometimes is quite well
defined for a particular compiler.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Nov 22 '05 #12
In article <Mu********************@rcn.net>,
Pete Becker <pe********@acm.org> wrote:
Greg Comeau wrote:
Better to use offsetof() (although oddly
you may find it using the same implementation, but then at least
you'll know it works


Indeed. It's not odd, because offsetof is in the standard library. The
standard library is part of the implementation, and the only requirement
is that its components do what they're supposed to do. They can, and
often do, rely on "undefined behavior" which sometimes is quite well
defined for a particular compiler.


I was trying to say that a newbie may find it odd that a header
file can do something but that they are not necessarily supposed
to do it that same way, and it's worth emphasizing what you say again:
what you sometimes see in headers is for implementors so newbies
before using something that "looks cool" look it up or pass it by this NG.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 0))...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
0
by: techie | last post by:
Hi, I've created a COM object in VC++ that I call from XMetal. I pass the COM object (via a XMetal macro) my XMetal Application object by a put_ method. In my put_ method I call QueryInterface...
3
by: zombek | last post by:
Hi. I'm quite a begginer. I wanted to learn the usage of tinyxml library so I wrote a little program which compiles on g++ 4.1.1 with -O2 -Wall, but when I run it a get a message "memory access...
1
by: zombek | last post by:
Hi. When I comipile my program (source at the bottom) with : g++ -O2 -static it gives me 'memory access violation' warning and when I dont use -O2 it doesn't. I need to use -O2 because my...
4
by: tuxman | last post by:
Hi all. First of all let me apologize by my english. I've googled a lot about my problem, but I had not find anything conclusive. I have the following piece of code: vector < map < int , char *...
6
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.