Hi All,
I plan on using the following C++ code
to create nodes with unlimited children:
// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet.
struct NodeT { Lnk_T Lnk ; };
So I have to declare NodeT like this instead:
struct NodeT {
struct { NodeT * * B, * * E, * * Room ; } Lnk ; };
typedef NodeT * Link ;
typedef Link * Link_P ;
// B is the Beginning of an array of pointers.
// E is the End of an array of pointers that are in use.
// Room the end of an array of all pointers, used or not.
struct Lnk_T { Link_P B, E, Room ; };
Lnk_T Lnk ;
enum { Chunk = 4,
Sz_Ptr = sizeof Link, Sz_Node = sizeof NodeT };
GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link_P ) realloc( Lnk.B, Room * Sz_Ptr );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Ptr ); }
// Below is an example of is how the above might be used,
// I know that it works.
__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {
GrowList ( Lnk ); // Lnk is a global, so it's initialized.
Link & P = * Lnk.E ++ ;
P = ( Link ) realloc( P, Sz_Node );
memset ( P, 0, Sz_Node );
GrowList ( ( Lnk_T & ) P->Lnk );
{ Link Passed = P, & P = * Passed->Lnk.E ++ ;
P = ( Link ) realloc( P, Sz_Node );
memset ( P, 0, Sz_Node );
GrowList ( ( Lnk_T & ) P->Lnk );
// etc.
}
But is there any way to declare NodeT so that
I don't have to use that ( Lnk_T & ) cast ?
I plan on using the following C++ code
to create nodes with unlimited children:
// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet.
struct NodeT { Lnk_T Lnk ; };
So I have to declare NodeT like this instead:
struct NodeT {
struct { NodeT * * B, * * E, * * Room ; } Lnk ; };
typedef NodeT * Link ;
typedef Link * Link_P ;
// B is the Beginning of an array of pointers.
// E is the End of an array of pointers that are in use.
// Room the end of an array of all pointers, used or not.
struct Lnk_T { Link_P B, E, Room ; };
Lnk_T Lnk ;
enum { Chunk = 4,
Sz_Ptr = sizeof Link, Sz_Node = sizeof NodeT };
GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link_P ) realloc( Lnk.B, Room * Sz_Ptr );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Ptr ); }
// Below is an example of is how the above might be used,
// I know that it works.
__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {
GrowList ( Lnk ); // Lnk is a global, so it's initialized.
Link & P = * Lnk.E ++ ;
P = ( Link ) realloc( P, Sz_Node );
memset ( P, 0, Sz_Node );
GrowList ( ( Lnk_T & ) P->Lnk );
{ Link Passed = P, & P = * Passed->Lnk.E ++ ;
P = ( Link ) realloc( P, Sz_Node );
memset ( P, 0, Sz_Node );
GrowList ( ( Lnk_T & ) P->Lnk );
// etc.
}
But is there any way to declare NodeT so that
I don't have to use that ( Lnk_T & ) cast ?
Comment