|
-
June 20th, 2009, 08:30 AM
#16
Re: read only alias to a pointer
Firstly thanks to all of you for coming up with a lot work around methods.
JVene - I am having a look at your implementation, give me some time, I will come back on that, thanks.
Artella, thanks for that idea, it seems like a possibility as well, just feel it is a little risky (I could be completely wrong, correct me if I am wrong)
The idea of casting a "const int*" to a "int *" seems a little risky and wrong to me, again I could be wrong.
The reason I feel that way is because, const int* is not supposed to change the value of the variable it points to. So attempting to do so doesn't seem correct.
I know your original program has managed to do this successfully.
However I feel if you change the variable v1 to a const int variable, it might present a problem
Changes to your program
-------------------------------
I had done some changes to your program to test it further, changes are mentioned below:
1) to include a more detailed cout statement (to include address of variables and pointers)
2) change the declaration of the variable v1 to "const int".
Code:
#include <iostream>
using namespace std;
int main(void)
{
system("clear");
const int v1 = 10;
int v2 = 20;
cout << "&v1 = " << &v1 << "\tv1 = " << v1 << endl;
cout << "&v2 = " << &v2 << "\tv2 = " << v2 << endl;
cout << "\n\n------------\n\n";
const int* ptr = &v1;
cout << "&ptr = " << &ptr << "\tptr = " << ptr << "\t*ptr = " << *ptr << endl;
cout << "\n\n------------\n\n";
*const_cast<int *>(ptr) = v2;
//*ptr = v2;//This is NOT ALLOWED
cout << "&ptr = " << &ptr << "\tptr = " << ptr << "\t*ptr = " << *ptr << endl;
cout << "\n\n------------\n\n";
cout << "&v1 = " << &v1 << "\tv1 = " << v1 << endl;
cout << "&v2 = " << &v2 << "\tv2 = " << v2 << endl;
cout << "\n\n------------\n\n";
return 0;
}
If you see the output of the program, you will realize, finally *ptr shows the value 20, however the value of the value of the constant v1 remains as 10.
And ptr points to v1.
output (removed some blank lines from the output)
Code:
&v1 = 0xbfffd17c v1 = 10
&v2 = 0xbfffd178 v2 = 20
-----------
&ptr = 0xbfffd174 ptr = 0xbfffd17c *ptr = 10
------------
&ptr = 0xbfffd174 ptr = 0xbfffd17c *ptr = 20
------------
&v1 = 0xbfffd17c v1 = 10
&v2 = 0xbfffd178 v2 = 20
------------
Last edited by Muthuveerappan; June 20th, 2009 at 08:45 AM.
-
June 21st, 2009, 10:44 PM
#17
Re: read only alias to a pointer
Thanks all for all your valuable inputs, it has helped me understand better.
Thanks JVene, I was able to understand what your program was doing.
It was an interesting idea, and I certainly didn't think from that angle.
In the process I learned the conversion operator, never knew that existed. Thanks.
Just one point, actually with the program (constalias 2nd version), we wouldn't be able to "declare" a read only alias.
We would be stuck at the same point because we were attempting the following :
The conversion operator returns the following type:
const int* const &
This is done is by returning a variable of the type int*
so effectively it is equivalent to:
Code:
int* p1;
const int* const & p2 = p1;
This is exactly where the gcc compiler seems to differ from MSVC.
And therefore the conversion operator actually returns a temporary address and not the way we intended.
Hence we get that warning "warning: returning reference to temporary" while compiling.
The more I look at it the more it seems like a certain compiler bug.
The reason I say that is because, "const int* const &" is meant to be an alias (besides other restrictions it is supposed to impose such as not allowing int* and int** to be modified). But in gcc it simply isn't an alias !
the following would work, till gcc bug is reported and gets fixed:
Code:
#include<iostream>
using namespace std;
int main()
{
system("clear");
int v1 = 10;
int* p1 = &v1;
const int* const * pt = &p1;
const int* const & pa = *pt;
cout << "&v1 = " << &v1 << "\tv1 = " << v1 << endl;
cout << "&p1 = " << &p1 << "\tp1 = " << p1 << "\t*p1 = " << *p1 << endl;
cout << "&pa = " << &pa << "\tpa = " << pa << "\t*pa = " << *pa << endl;
return(0);
}
As you suggested the same could be implemented in the form of a template class to cater to a generic solution.
Thanks again, for all your thoughts, much appreciated !!!
Last edited by Muthuveerappan; June 21st, 2009 at 10:48 PM.
-
June 21st, 2009, 11:07 PM
#18
Re: read only alias to a pointer
Well, surprise. MSVC was the better of the two, it seems. There definitely should not be a temporary returned from the conversion function. You've found what should at least be termed an anomaly.
These do exist between compilers, and among versions. It would be very helpful to the community at large if you could say what version of GCC (and which platform might also be helpful, Windows?).
I'm quite confident in template class solutions to these puzzles. The two I listed were terse, suggestive hints that I hoped you'd formalize into a solution you'd prefer, and that expanded into a solution which works around the quirk found in GCC.
Boost has a couple of library components which deal with quirks among compilers, something along the lines of what you've analyzed here. I've not used this one, but (the exact name escapes me) there's a library that deals with initialization. The subject is not related to yours, but apparently:
someobject a = someobect();
Isn't consistent across all compilers, or so the library informs. They have template class solutions to deal with compiler ambiguities so application code can be more portable among them, and more reliable.
So the solution, that of a template class, is in good company.
You've created an interesting thread. It started out seeming an over-focus on a trivial and seldom used idiom (pardon my initial reaction), but it transformed into what amounts to a deeper discussion about how to deal with such issues, and, ultimately, of a the difference we see between compilers when using this language. That's an issue that strikes at all of us.
It's also impressive, now after 17 posts over several days, that you stuck with it until you've reached the beginning of a real resolution. Students, intermediates and visitors should benefit from that example of tenacity.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
June 22nd, 2009, 05:26 AM
#19
Re: read only alias to a pointer
The version details are mentioned below:
Compiler - gcc version 4.0.1 (Apple Inc. build 5488)
OS - Mac OS X version 10.5.7
Thanks again to all, for patiently answering the questions, was certainly a big learning for me.
Thanks,
Muthu
-
June 22nd, 2009, 12:48 PM
#20
Re: read only alias to a pointer
Ouch! I use that compiler 
It occurred to me that you might be able to use a reinterpret_cast in the return from the conversion and assignment operators, unfortunate as that is, but it would be common among compilers if it appeases GCC.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|