after printing typeids, looking at ptr address in debugger both these (structure in heap, stack) appear similar. ie. each p_ is in an offset of 4 (checked this just in case they overlapped)
all these proved that the structure on heap cannot be changed. ie. the p_ ptrs cannot be assigned can gcc be doing something wrong here?? --- In [email protected], "Indika Bandara Udagedara" <[EMAIL PROTECTED]> wrote: > > Hi, here is the code, pls see the Blob type below and it behaves > strangely when created on heap and stack > > > // ==================================================== > > #include <stdio.h> > > struct NullType > {}; > > template <class T1, class T2> > struct TypeList > { > typedef T1 head; > typedef T2 tail; > }; > > > template <class T1> > struct Blob > { > Blob(){ p_ = NULL; printf("Blob() child\n"); } > T1* p_; > > }; > > > > template <> > struct Blob<NullType> > {}; > > template <class T1, class T2> > struct Blob<TypeList<T1, T2> > : > public Blob<T1>, > public Blob<T2> > { > Blob(){ printf("Blob() typelist\n"); } > }; > > > > > struct A{}; > struct B{}; > struct C{}; > > typedef Blob< TypeList <A, TypeList<B, TypeList<C, NullType> > > > myBlob; > > typedef TypeList <A, TypeList<B, TypeList<C, NullType> > > myTypeList; > > int main() > { > > printf("1 ====\n"); > myBlob m; > (static_cast< Blob<A> >(m)).p_ = new A; > (static_cast< Blob<B> >(m)).p_ = NULL; > (static_cast< Blob<C> >(m)).p_ = NULL; > > > if ((static_cast< Blob<A> >(m)).p_ ) > printf("A ok\n"); > > if ((static_cast< Blob<B> >(m)).p_ ) > printf("B ok\n"); > > printf("2 ====\n"); > myBlob* p = new myBlob; > (static_cast< Blob<A>* >(p))->p_ = new A; > (static_cast< Blob<B>* >(p))->p_ = NULL; > (static_cast< Blob<C>* >(p))->p_ = NULL; > if ((static_cast< Blob<A>* >(p))->p_ ) > printf("A ok\n"); > > if ((static_cast< Blob<B>* >(p))->p_ ) > printf("B ok\n"); > > return 0; > > } > > > > // ==================================================== > > > what is done is > 1) create myBlob on stack and assign its data members to NULL, set A > ptr to a new obj > > 2) create myBlob on heap and assign its data members to NULL, set A > ptr to a new obj > > But when the assigned values are inspected, only 2) indicates that > values were assigned. > > > This is pretty strange. Can u pls point out whats wrong here? > (compiler gcc) > > As some background, the Blob creates 'p_' ptrs of all types in > myTypeList by inheriting from each. Data member is created in a > specialization. > > > Thanks > > Indika >
