473,403 Members | 2,359 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,403 software developers and data experts.

initializing references / null pointer

I do not really know why the following piece of code
works and what happens internally:

#include <iostream>

template <class T>
class ReferenceTest
{
public:
ReferenceTest()
: zero(0), value(*zero)
{
std::cout << "constructor works ?!?" << std::endl;
}

T *zero;
T &value;
};

int
main()
{
ReferenceTest<double> r;
return 0;
}
I would expect a segmentation fault when value gets initialized
in the constructor since *zero (which is *0 IMO) tries to access
a value at address 0. Where am I wrong

regards,
alex
Jul 19 '05 #1
1 3398
Alexander Stippler wrote in news:3f******@news.uni-ulm.de:
I do not really know why the following piece of code
works and what happens internally:

#include <iostream>

template <class T>
class ReferenceTest
{
public:
ReferenceTest()
: zero(0), value(*zero)
{
std::cout << "constructor works ?!?" <<
std::endl;
}

T *zero;
T &value;
};

int
main()
{
ReferenceTest<double> r;
return 0;
}
I would expect a segmentation fault when value gets initialized
in the constructor since *zero (which is *0 IMO) tries to access
a value at address 0. Where am I wrong


Nope, "segmentation fault" isn't defined by the standard. What
you have here is undefined behaviour, you broke the rules so
you shouldn't expect there to be any rules that say what your
code does.

Also there is no requirment that an "access at address ..." occurs
during initialization of a reference, though that doesn't mean a
conforming implementation *doesn't* access (or something similar) the
reference. It's all up to the implementation and it doesn't have to
do anything special at all (i.e. it can assume your not going to
write code that exibits undefined behaviour), further more it doesn't
have to document what it does (or doesn't) do.

If your implementation gives "segmentation fault's" or "core dumps"
then you could get one (if thats what you want) by adding
double d = r.value; to main().

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2

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

Similar topics

15
by: Web Developer | last post by:
Hi, Can someone provide a short and concise statement(s) on the difference between pointers and references. A graphical representation (via links?) of both would be much appreciated as well. ...
8
by: Roger Leigh | last post by:
Hello again, I have a bit of a strange problem here. This code works OK: class foo { public: foo(int **ref): m_ref(ref) {}
17
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
8
by: john townsley | last post by:
are there any differences when using pointers or passing by reference when using 1) basic variables 2)complex variable types like arrays,structures it seems easier to simply pass by reference...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
1
by: sip.address | last post by:
Hi everyone, A while ago I asked for help about using smart pointers (ie. shared_ptr) in cyclic situations. Weak ptrs popped out and I gave it a try, but to be honest, I don't feel very...
29
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...
0
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...

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.