473,651 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why there is no memory access violation?

Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((stru c*)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 2892
aling wrote:
Have following code snip:

struct struc {
int member1;
int member2;
} ;
printf("&((stru c*)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("&((st ruc*)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("&((stru c*)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_stru c->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_stru c->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************ **********@g43g 2000cwa.googleg roups.com>,
aling <li*********@12 6.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

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

Similar topics

2
2502
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)) 3600.0 >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 1)) 0.0 >>> time.mktime((1969, 12, 31, 16, 0, 0, 0, 0, 1))
0
2718
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 violations aren't part of the standard C++ >> exception handling support. On Windows, a particular MSVC compiler >> option enables Microsoft's Structured Exception Handling (SEH) in C++ >> EH so that a catch (...) will catch an access violation. ...
3
6801
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 a member function of the same class I get a memory access violation. For example: // this is fine, no error void CBase::FuncA( void ) { plugin_info_t *plugin_info = new plugin_info_t;
1
2459
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 expected. My question relates to the memory access violation, specfically, what should happen? My guess is that since the good ole new operator allocates the memory we get an SEH memory access violation exception. Given that this can occur in my...
0
1127
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 for the _Application interface and create an instance of a class called CXMetalApp: STDMETHODIMP CXMLEditorInterface::put_Application(LPDISPATCH newVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState())
3
3610
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 violation". Source: #include "tinyxml.h" #include <vector> #include <string> #include <iostream> using namespace std;
1
1774
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 professor will use it (automated tests). The error looks like I went out of array bounds but I don't. How to solve it? Szymon --------------------------CODE-----------------------
4
2260
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 * > > bank; bank . reserve ( 10 ); if ( bank == "a" ) //Memory access error (Access violation at address #blabla) which give me the error indicated in comment.
6
5565
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 particular crash so I don't think anyone else is experiencing it but I know other users are doing exactly what she is doing because it is our most popular module. I have analyzed the dmp files from several of this user's crashes using windbg...
0
8357
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8803
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.