473,383 Members | 1,716 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,383 software developers and data experts.

why is this still working??

This is my code:

int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;

// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don't I get an error and why are the values of a->integer en x correct??
It's this a dangling reference fault??

bizar i think
Jul 22 '05 #1
10 1122
On Wed, 20 Oct 2004 10:47:21 -0700, Hans Van den Eynden wrote:
This is my code:

int& dangling_reference(); Why don't I get an error and why are the values of a->integer en x correct??
It's this a dangling reference fault??

Most likely cause nothing messes with the location on the stack where
x is stored.

Jul 22 '05 #2
Hans Van den Eynden wrote:
This is my code:

int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
WTF is 'Integer'?
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;
That's not a dangling reference. That's accessing members of
an object whose lifetime has ended. Undefined behaviour occurs.

// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don't I get an error and why are the values of a->integer en x correct??
What error would you like to get? The behaviour is undefined. Your
implementation may (has all rights to) create the code that will give
you some kind of error. Or it may (has all rights to) create the code
that will format the hard drive of your computer. The Standard makes
no requirement here.
It's this a dangling reference fault??


No, it's your fault. You wrote the code that has undefined behaviour.

V
Jul 22 '05 #3
Hans Van den Eynden wrote:

Why don't I get an error and why are the values of a->integer en x correct??
It's this a dangling reference fault??

There's no such thing as a "dangling reference fault." Both cases are
undefined behavior. There is no requirement that the compiler detect
or otherwise have any specific behavior when you break the rules like
that.
Jul 22 '05 #4
on******@hotmail.com (Hans Van den Eynden) wrote in
news:1c**************************@posting.google.c om:
This is my code:
[snip standard example of returning a reference to a local variable]
Why don't I get an error and why are the values of a->integer en x
correct?? It's this a dangling reference fault??


This invokes what's called "Undefined Behaviour". The compiler/computer is
free to do whatever it wishes, including continuing to run as if there is
no problem (or alternately, make the appropriate calls to your video card
to attempt to drive your monitor at 2.4 GHz horizontal refresh rate,
causing your monitor to explode).

Jul 22 '05 #5
"Hans Van den Eynden" <on******@hotmail.com> schrieb im Newsbeitrag news:1c**************************@posting.google.c om...
This is my code:

int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;

// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don't I get an error and why are the values of a->integer en x correct??
It's this a dangling reference fault??

bizar i think


Not bizar - just undefined.

Heinz
Jul 22 '05 #6
int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;

Just for clarity: Both the pointers "a" and "b" point to an object which no
longer exists.

Undefined Behaviour.


// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

Here your copy constructing an object of type "int" from an object which no
longer exists.

Undefined Behaviour.


cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don't I get an error and why are the values of a->integer en x
correct?? It's this a dangling reference fault??

Your program is not ill-formed, though it does produce undefined behaviour.

If you were to run it on WinXP, you'd probably get a runtime error for
accessing memory that isn't yours.
-JKop
Jul 22 '05 #7

"JKop" <NU**@NULL.NULL> wrote in message
news:tH*******************@news.indigo.ie...
int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;

Just for clarity: Both the pointers "a" and "b" point to an object which
no
longer exists.

Undefined Behaviour.


// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

Here your copy constructing an object of type "int" from an object which
no
longer exists.

Undefined Behaviour.


cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don't I get an error and why are the values of a->integer en x
correct?? It's this a dangling reference fault??

Your program is not ill-formed, though it does produce undefined
behaviour.

If you were to run it on WinXP, you'd probably get a runtime error for
accessing memory that isn't yours.


Actually, in WinXP you will get undefined behavior, too. If you're (still)
lucky, it might work. The deallocated memory is still reserved by the
process (and might still contain the object state before delete, if it
wasn't overwritten by other allocations).

Br/
Catalin
Jul 22 '05 #8
Catalin Pitis wrote:

Actually, in WinXP you will get undefined behavior, too. If you're (still)
lucky, it might work.


Other way round:
If you are lucky, it crashes immediatly.
If it appears wo work, you were unlucky. Unlucky in the sense
that it seems to work in your test environment, while it immediatly
crashes when your customer tries the program the first time leading
him to believe you made a bad job (which you did) and thus he refuses
to pay your bill.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Catalin Pitis wrote:

Actually, in WinXP you will get undefined behavior, too. If you're
(still)
lucky, it might work.


Other way round:
If you are lucky, it crashes immediatly.
If it appears wo work, you were unlucky. Unlucky in the sense
that it seems to work in your test environment, while it immediatly
crashes when your customer tries the program the first time leading
him to believe you made a bad job (which you did) and thus he refuses
to pay your bill.


You're right. I was too optimistic :).
Sorry for that :)
Jul 22 '05 #10
You're right. I was too optimistic :).
Sorry for that :)

That's the compiler's job!
-JKop
Jul 22 '05 #11

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

Similar topics

4
by: zalekbloom | last post by:
I noticed on my PC with win98/IE 6.028 applets are not working. Applets are working when I use Netscape 7.1. When I open the DOS win and I check for a Java version I am getting: C:\WINDOWS>java...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
3
by: S. van Beek | last post by:
Dear reader, To work with linked tables is a professional method of working with a frond end and back end mdb. Working with linked tables on a local pc gives an acceptable performance but...
6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
0
by: Mark B | last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a scheduled hard-boot occurred during a ms-security patch install overnight. They couldn't get the server working again so they...
1
by: Larry Bud | last post by:
Wondering what I should be looking at when looking for a memory leak.. Using Process Explorer by SysInternals, and they have a Working Set and a Virtual Size memory column. Which one should I be...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
0
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.