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

Bad Reference?

Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
delete i;
printf("%d\n",badref);
//This reference is bad coz "i" is deleted
badref = 70;
printf("%d\n",badref);
return 0 ;
}

I compiled and ran this program on gcc 4.0 and it worked.
it prints:
50
50
70

In the above case I would call the reference "badref" a bad reference
because
it is alias to var that is deleted.
My question is:
Do we check if the reference is bad, before using it?
If so, how?

Regards,
Yogesh Kini

Jan 30 '06 #1
6 1889
Yogesh wrote:
Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
delete i;
printf("%d\n",badref);
//This reference is bad coz "i" is deleted
Yes. Using it invokes undefined behavior.
badref = 70;
printf("%d\n",badref);
return 0 ;
}

I compiled and ran this program on gcc 4.0 and it worked.
That's one possible result of undefined behavior.
it prints:
50
50
70

In the above case I would call the reference "badref" a bad reference
because it is alias to var that is deleted.
That's right.
My question is:
Do we check if the reference is bad, before using it?
If so, how?


There is no way.

Jan 30 '06 #2
In article <11**********************@g44g2000cwa.googlegroups .com>,
"Yogesh" <yo*********@gmail.com> wrote:
Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
delete i;
printf("%d\n",badref);
//This reference is bad coz "i" is deleted
badref = 70;
printf("%d\n",badref);
return 0 ;
}

I compiled and ran this program on gcc 4.0 and it worked.
it prints:
50
50
70

In the above case I would call the reference "badref" a bad reference
because
it is alias to var that is deleted.
My question is:
Do we check if the reference is bad, before using it?
If so, how?


You can't check if the reference is bad. You also can't check if a
pointer is bad (ie after the delete, there is no way to determine that
dereferencing 'i' is undefined.)

That's just the way it is in c++.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 30 '06 #3
I think C++ don't do this complex check.
becaue the lifetime of an object is a runtime property of the object,
so I think it's too complex to implement such check in compile-time
check. and C++ compiler doesn't generate some extra code to enable
run-time for such thing.
refer to ISO C++ 14882 chapter 3.8 the seventh item:
if after the lifetime of an object has ended and before the storage
which the object occupied is reused or released, a new object is
created at the storage location which the original object occupied, a
pointer that pointed to the original object, a reference that referred
to the original object, or the name of original object will
automatically refer to the new object and, once the lifetime of the new
object has started, can be used to manipulate the new object...
if between the delete expression and the assignment expression there's
another new expression that actually the same location which is
occupied by the original object then the assignment expression is
really right.
so it's impossible to do this check during compile-time and it's
improper to do this check in run-time due to too much burden on
run-time efficiency(think there's always any some other code around the
delete expression just for this check), so I guess c++ implementation
just let it pass.

Jan 30 '06 #4
Yogesh wrote:
Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
delete i;
printf("%d\n",badref);
//This reference is bad coz "i" is deleted
badref = 70;
printf("%d\n",badref);
return 0 ;
}


It's not C++, but tools like valgrind can catch many of these errors. For
example:

g++ -Wall badref.cpp -g -o badref
valgrind badref

==11523== Memcheck, a memory error detector for x86-linux.
==11523== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==11523== Using valgrind-2.4.0, a program supervision framework for
x86-linux.
==11523== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==11523== For more details, rerun with: -v
==11523==
50
==11523== Mismatched free() / delete / delete []
==11523== at 0x1B9098CF: operator delete(void*) (vg_replace_malloc.c:155)
==11523== by 0x8048513: main (badref.cpp:10)
==11523== Address 0x1B92F028 is 0 bytes inside a block of size 4 alloc'd
==11523== at 0x1B909222: malloc (vg_replace_malloc.c:130)
==11523== by 0x80484DD: main (badref.cpp:6)
==11523==
==11523== Invalid read of size 4
==11523== at 0x804851A: main (badref.cpp:11)
==11523== Address 0x1B92F028 is 0 bytes inside a block of size 4 free'd
==11523== at 0x1B9098CF: operator delete(void*) (vg_replace_malloc.c:155)
==11523== by 0x8048513: main (badref.cpp:10)
50
==11523==
==11523== Invalid write of size 4
==11523== at 0x8048530: main (badref.cpp:13)
==11523== Address 0x1B92F028 is 0 bytes inside a block of size 4 free'd
==11523== at 0x1B9098CF: operator delete(void*) (vg_replace_malloc.c:155)
==11523== by 0x8048513: main (badref.cpp:10)
==11523==
==11523== Invalid read of size 4
==11523== at 0x8048539: main (badref.cpp:14)
==11523== Address 0x1B92F028 is 0 bytes inside a block of size 4 free'd
==11523== at 0x1B9098CF: operator delete(void*) (vg_replace_malloc.c:155)
==11523== by 0x8048513: main (badref.cpp:10)
70
==11523==
==11523== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 16 from 1)
==11523== malloc/free: in use at exit: 0 bytes in 0 blocks.
==11523== malloc/free: 1 allocs, 1 frees, 4 bytes allocated.
==11523== For counts of detected errors, rerun with: -v
==11523== No malloc'd blocks -- no leaks are possible.

Such tools can't catch everything (like casting a pointer as a pointer to an
unrelated type), but they're better than nothing.

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Jan 30 '06 #5

"Yogesh" <yo*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
delete i;


In addition to the comments others have made, I'd point out that if you used
"malloc", you need to use "free" to free the memory. You use "delete" when
you allocate the memory with "new", not "malloc".

-Howard

Jan 30 '06 #6
Yogesh wrote:
Hi all,
Please look at the code below:

int main (int argc, char * const argv[])
{
int * i = (int *)malloc(sizeof(int));
*i = 50;
int &badref = *i;
printf("%d\n",badref);
your undefined behavior starts at this line: delete i; delete-ing a pointer not allocated with new. Your initialization
of i should be: int* i = new int;
Then this delete would be valid.

printf("%d\n",badref);
//This reference is bad coz "i" is deleted Correct. You have undefined behavior (of course everything *after*
delete is undefined, the way the code is written).

badref = 70;
printf("%d\n",badref);
return 0 ;
}

I compiled and ran this program on gcc 4.0 and it worked.
it prints:
50
50
70

In the above case I would call the reference "badref" a bad reference
because
it is alias to var that is deleted.
My question is:
Do we check if the reference is bad, before using it?
If so, how?

Regards,
Yogesh Kini

Jan 31 '06 #7

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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: 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...
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
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.