473,387 Members | 1,637 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.

address of references to pointers

Is this legal C++?
or am I dereferencing posssibly invalid addresses here?

#include <vector>
struct C{};

int main () {
C* c;
C* a;

c = &a[0];

std::vector<C> v;
c = &*v.begin();

C& r = v.front();
c = &r;
}
--
Thanks.
Stein
Feb 16 '06 #1
7 2337
"Stein Gulbrandsen" <sg*****@online.no> wrote in message
news:43******@news.broadpark.no
Is this legal C++?
or am I dereferencing posssibly invalid addresses here?

#include <vector>
struct C{};

int main () {
C* c;
C* a;

c = &a[0];
Illegal. a contains random junk; it does not point to valid memory.
std::vector<C> v;
c = &*v.begin();
Likewise illegal. v.begin() doesn't point to anything valid so you shouldn't
dereference it.
C& r = v.front();
Illegal.
c = &r;
}
--
Thanks.
Stein

--
John Carson
Feb 16 '06 #2
Stein Gulbrandsen wrote:
Is this legal C++?
or am I dereferencing posssibly invalid addresses here?

#include <vector>
struct C{};

int main () {
C* c;
C* a;
// never leave pointers (or anything!) uninitialised.
c = &a[0];
// a is an uninitialised pointer to C, not an array of C.
// you're not allowed to dereference it.
// undefined behaviour follows.
std::vector<C> v;
c = &*v.begin();
// prefer std::vector<C>::iterator
C& r = v.front();
c = &r;
}

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 16 '06 #3
"John Carson" <jc****************@netspace.net.au> writes:
"Stein Gulbrandsen" <sg*****@online.no> wrote in message
news:43******@news.broadpark.no
Is this legal C++?
or am I dereferencing posssibly invalid addresses here?
c = &a[0];


Illegal. a contains random junk; it does not point to valid memory.
...
std::vector<C> v;
c = &*v.begin();


Likewise illegal. v.begin() doesn't point to anything valid so you shouldn't
dereference it.
...
C& r = v.front();


Illegal.


Thanks, that is what I would also think. So, if I understand things
right, all the 8 numbered lines below are illegal (undefined
behaviour). Nevertheless, it compiles silently (except for Sun
compiler warning about the reference to non-const at 8) and runs "OK"
(producing 8 zeroes on cout) on Sun WS C++, SGI Mipspro C++, Microsoft
Visual C++/.net, and g++.

It seems that no dereferencing is actually taking place.
Is this just by chance, because of some implementation details of the
compilers, or are any of these lines actuallly legal C++?

#include <vector>
#include <iostream>
class C{}; C* c = 0; // C is defined
class D; D* d = 0; // D is not defined
std::vector<int> v; // empty

int main () {
D& r = *d; // 1a
std::cout << &r << " " // 1b
<< &*static_cast<D*>(0) << " " // 2
<< &*d << " " // 3
<< &c[0] << " " // 4
<< &0[static_cast<C*>(0)] << " "// 5
<< &v[0] << " " // 6
<< &*v.begin() << " " // 7
<< &std::vector<C>().front() // 8
<< "\n";
}
--
stein
Feb 17 '06 #4
Stein Gulbrandsen <sg*****@online.nospam> wrote in
news:m3************@online.nospam:
"John Carson" <jc****************@netspace.net.au> writes:
"Stein Gulbrandsen" <sg*****@online.no> wrote in message
news:43******@news.broadpark.no
Is this legal C++?
or am I dereferencing posssibly invalid addresses here?
c = &a[0];
Illegal. a contains random junk; it does not point to valid memory.
...
std::vector<C> v;
c = &*v.begin();


Likewise illegal. v.begin() doesn't point to anything valid so you
shouldn't dereference it.
...
C& r = v.front();


Illegal.


Thanks, that is what I would also think. So, if I understand things
right, all the 8 numbered lines below are illegal (undefined
behaviour). Nevertheless, it compiles silently (except for Sun
compiler warning about the reference to non-const at 8) and runs "OK"
(producing 8 zeroes on cout) on Sun WS C++, SGI Mipspro C++, Microsoft
Visual C++/.net, and g++.

It seems that no dereferencing is actually taking place.
Is this just by chance, because of some implementation details of the
compilers, or are any of these lines actuallly legal C++?

#include <vector>
#include <iostream>
class C{}; C* c = 0; // C is defined
class D; D* d = 0; // D is not defined
std::vector<int> v; // empty

int main () {
D& r = *d; // 1a
std::cout << &r << " " // 1b
<< &*static_cast<D*>(0) << " " // 2
<< &*d << " " // 3
<< &c[0] << " " // 4


In other words, this is taking the address of a value obtained from
dereferencing a NULL pointer, just like

& * (c + 0), where c==0.

Mybe the compiler just "cancels out" the dereferencing with the address-
of operator. But is it allowed to do that ?
<< &0[static_cast<C*>(0)] << " "// 5
<< &v[0] << " " // 6
<< &*v.begin() << " " // 7
<< &std::vector<C>().front() // 8
<< "\n";
}


STLPort with _STLP_DEBUG doesn't agree with line 6, throwing a debug
assert. In fact, if you replace &v[0] with &v.at(0), it'll throw a
std::out_of_range. So Line 6 is clearly not kosher, neither is 7 nor 8.

The program runs "OK" because all the STL implementations probably use a
pointer for the iterator of vector. In the Dinkumware STL supplied with
VC 7.1 (.NET 2003) , vector is implemented using three pointers _Myfirst,
_Mylast, _Myend. These are all NULL in a default-constructed vector.

operator[] is *( begin() + offset ), and begin() is just _Mybegin. The
compiler probably "sees through" that the result of dereferencing a NULL
pointer (ouch) is passed to the address-of operator.

--
Life is complex, with real and imaginary parts.
Feb 18 '06 #5
>> c = &a[0];

Illegal. a contains random junk; it does not point to valid memory.


I thought that statement doesn't dereference anything at all...merely
assigning the same random junk from a to c...just equivalent to

c = a + 0;

or just

c = a;

Regards,
Ben
Feb 18 '06 #6
benben wrote:
c = &a[0];


Illegal. a contains random junk; it does not point to valid memory.


I thought that statement doesn't dereference anything at all...merely
assigning the same random junk from a to c...just equivalent to

c = a + 0;

or just

c = a;

Regards,
Ben


Well, thinking more, I might have mistaken...

&a[0] is equivalent to &(*(a + 0)) which is equivalent to &*a so there
is a dereference if the compiler doesn't optimize off the operator pair...

I think the result is UB...

Regards,
Ben
Feb 18 '06 #7
"Stein Gulbrandsen" <sg*****@online.nospam> wrote in message
news:m3************@online.nospam
"John Carson" <jc****************@netspace.net.au> writes:
"Stein Gulbrandsen" <sg*****@online.no> wrote in message
news:43******@news.broadpark.no
Is this legal C++?
or am I dereferencing posssibly invalid addresses here?
c = &a[0];
Illegal. a contains random junk; it does not point to valid memory.
...
std::vector<C> v;
c = &*v.begin();


Likewise illegal. v.begin() doesn't point to anything valid so you
shouldn't dereference it.
...
C& r = v.front();


Illegal.


Thanks, that is what I would also think.


If you run it in Debug mode in VC++ 8.0, then

c = &a[0];

produces

"Run-time Check Failure #3 - the variable 'a' is being used without being
defined."

If this line is commented out, then

c = &*v.begin();

produces

"Debug Assertion Failed!" following by "Expression: vector iterator is not
dereferencable"

If this line is commented out, then

C& r = v.front();

produces the same "Expression: vector iterator is not dereferencable" error.

In release mode,

c = &a[0];

"works" but

c = &*v.begin();

and

C& r = v.front();

crash the program (running on XP Home, SP2).

So, if I understand things
right, all the 8 numbered lines below are illegal (undefined
behaviour).
Yes.
Nevertheless, it compiles silently (except for Sun
compiler warning about the reference to non-const at 8) and runs "OK"
(producing 8 zeroes on cout) on Sun WS C++, SGI Mipspro C++, Microsoft
Visual C++/.net, and g++.

#include <vector>
#include <iostream>
class C{}; C* c = 0; // C is defined
class D; D* d = 0; // D is not defined
std::vector<int> v; // empty

int main () {
D& r = *d; // 1a
std::cout << &r << " " // 1b
<< &*static_cast<D*>(0) << " " // 2
<< &*d << " " // 3
<< &c[0] << " " // 4
<< &0[static_cast<C*>(0)] << " "// 5
<< &v[0] << " " // 6
<< &*v.begin() << " " // 7
<< &std::vector<C>().front() // 8
<< "\n";
}


Each of 6-8 crashes in Release mode with VC++ 8 and Windows XP Home SP2.
--
John Carson
Feb 18 '06 #8

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

Similar topics

4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
7
by: Howard | last post by:
Hi, in one of the recent posts, I saw someone pass two variables of built-in type (int and double), by reference, to a member function of a class. That function then took the addresses of those...
3
by: Nadav | last post by:
Hi I wonder... Is it possible to define the address to which shared memory will be mapped In other words is it possible to apriory define the address MapViewOfFile returns Dlls are being loaded...
10
by: Rob Nicholson | last post by:
I've got a suspicion that two user (and therefore threads) in an ASP.NET application are sharing the same pointers - which they shouldn't be! The easiest way for me to possibly debug this is to...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
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...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
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,...

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.