c++ pointers heap to Ref allocation\assignment int & rMyInt = * ( new int( 5 ) )
the c++ style reference rMyInt is declared and assigned , directly from the heap.
Just to say, pointers are NOT trivial. So why do the books treat it as such ?
Koenig "C traps and pit falls" 1989 ISBN 0-201-17928-8 Addison-Wesley
reprinted with corrections November 1988
uses almost 3 pages to analyze this:
( * ( void( * ) ( ) ) 0 ) ( ) ;
I give you my quick analysis.
( void( * ) ( ) ) 0................this inner part cast 0 as a "void function pointer"
............................................a function points to address 0
then the outer most * uses the function pointer to make the call\jump to address 0
one graphic page only . view at 720 full screen to read the text.
I downloaded it on youtube and text reads ok.
A book suggested the free store allocation to Ref assignment, but never showed it.
Being new and unsure myself, I always needed more indepth ways to
read and write c++ pointer statements. So I share my analysis approach .
I would not write everything out , but I would still use the simple memory
maps and "step by step" evaluations to aid in a difficult pointer statement .
How does Bjarne Stroustrup do it ? He wrote the damn thing . I even
looked at some of his books, but he never gives a conceptual pleasing way
to do it either.
the use of simple memory maps and the "step by step" evaluation ,
reading pointer statements from right to left , inside to outside, and
determining what is a valid argument\operand to * and & operators , seems
to give me all the analysis power I need to analyze and debug even the most
crazy pointer statements , that may involve pointers of iterator objects ,
void pointers and more.
my step by step approach is:
* pMyInt
* 1000.....pMyInt self referential evaluated\returned it's content of 1000
5..........* used 1000 as an address to evaluate 1000's memory space
.................and returned it's content of 5 . stuff like that .
one more note:
* 25 ;.....will not compile, though it is a good start
...............literal address 25 is valid , but the compiler still needs to know 2 things:
1. how may bytes to read , starting at address 25 .
2. after the data is accessed, how should that data get interperted.
a cast( reinterpret ) takes care of both issues.
* ( doulbe * ) 25 ; tells the compiler to read 8 bytes starting at address 25
and to interpret the 8 bytes as a double float.
The parenthesis holds a "C" style cast.